Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)
shape.h
1#ifndef RUBY_SHAPE_H
2#define RUBY_SHAPE_H
3
4#include "internal/gc.h"
5
6#if (SIZEOF_UINT64_T <= SIZEOF_VALUE)
7
8#define SIZEOF_SHAPE_T 4
9#define SHAPE_IN_BASIC_FLAGS 1
10typedef uint32_t attr_index_t;
11typedef uint32_t shape_id_t;
12# define SHAPE_ID_NUM_BITS 32
13
14#else
15
16#define SIZEOF_SHAPE_T 2
17#define SHAPE_IN_BASIC_FLAGS 0
18typedef uint16_t attr_index_t;
19typedef uint16_t shape_id_t;
20# define SHAPE_ID_NUM_BITS 16
21
22#endif
23
24typedef uint32_t redblack_id_t;
25
26#define MAX_IVARS (attr_index_t)(-1)
27
28# define SHAPE_MASK (((uintptr_t)1 << SHAPE_ID_NUM_BITS) - 1)
29# define SHAPE_FLAG_MASK (((VALUE)-1) >> SHAPE_ID_NUM_BITS)
30
31# define SHAPE_FLAG_SHIFT ((SIZEOF_VALUE * 8) - SHAPE_ID_NUM_BITS)
32
33# define SHAPE_MAX_VARIATIONS 8
34
35# define INVALID_SHAPE_ID SHAPE_MASK
36# define ROOT_SHAPE_ID 0x0
37
38# define SPECIAL_CONST_SHAPE_ID (SIZE_POOL_COUNT * 2)
39# define OBJ_TOO_COMPLEX_SHAPE_ID (SPECIAL_CONST_SHAPE_ID + 1)
40
41typedef struct redblack_node redblack_node_t;
42
43struct rb_shape {
44 struct rb_id_table * edges; // id_table from ID (ivar) to next shape
45 ID edge_name; // ID (ivar) for transition from parent to rb_shape
46 attr_index_t next_iv_index;
47 uint32_t capacity; // Total capacity of the object with this shape
48 uint8_t type;
49 uint8_t size_pool_index;
50 shape_id_t parent_id;
51 redblack_node_t * ancestor_index;
52};
53
54typedef struct rb_shape rb_shape_t;
55
57 ID key;
58 rb_shape_t * value;
59 redblack_id_t l;
60 redblack_id_t r;
61};
62
63enum shape_type {
64 SHAPE_ROOT,
65 SHAPE_IVAR,
66 SHAPE_FROZEN,
67 SHAPE_T_OBJECT,
68 SHAPE_OBJ_TOO_COMPLEX,
69};
70
71typedef struct {
72 /* object shapes */
73 rb_shape_t *shape_list;
74 rb_shape_t *root_shape;
75 shape_id_t next_shape_id;
76
77 redblack_node_t *shape_cache;
78 unsigned int cache_size;
80RUBY_EXTERN rb_shape_tree_t *rb_shape_tree_ptr;
81
82static inline rb_shape_tree_t *
83rb_current_shape_tree(void)
84{
85 return rb_shape_tree_ptr;
86}
87#define GET_SHAPE_TREE() rb_current_shape_tree()
88
89static inline shape_id_t
90get_shape_id_from_flags(VALUE obj)
91{
93 return (shape_id_t)(SHAPE_MASK & ((RBASIC(obj)->flags) >> SHAPE_FLAG_SHIFT));
94}
95
96static inline void
97set_shape_id_in_flags(VALUE obj, shape_id_t shape_id)
98{
99 // Ractors are occupying the upper 32 bits of flags, but only in debug mode
100 // Object shapes are occupying top bits
101 RBASIC(obj)->flags &= SHAPE_FLAG_MASK;
102 RBASIC(obj)->flags |= ((VALUE)(shape_id) << SHAPE_FLAG_SHIFT);
103}
104
105
106#if SHAPE_IN_BASIC_FLAGS
107static inline shape_id_t
108RBASIC_SHAPE_ID(VALUE obj)
109{
110 return get_shape_id_from_flags(obj);
111}
112
113static inline void
114RBASIC_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
115{
116 set_shape_id_in_flags(obj, shape_id);
117}
118#endif
119
120static inline shape_id_t
121ROBJECT_SHAPE_ID(VALUE obj)
122{
123 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
124 return get_shape_id_from_flags(obj);
125}
126
127static inline void
128ROBJECT_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
129{
130 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
131 set_shape_id_in_flags(obj, shape_id);
132}
133
134static inline shape_id_t
135RCLASS_SHAPE_ID(VALUE obj)
136{
137 RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE));
138 return get_shape_id_from_flags(obj);
139}
140
141static inline void
142RCLASS_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
143{
144 RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE));
145 set_shape_id_in_flags(obj, shape_id);
146}
147
148rb_shape_t * rb_shape_get_root_shape(void);
149int32_t rb_shape_id_offset(void);
150
151rb_shape_t * rb_shape_get_parent(rb_shape_t * shape);
152
153rb_shape_t* rb_shape_get_shape_by_id(shape_id_t shape_id);
154shape_id_t rb_shape_get_shape_id(VALUE obj);
155rb_shape_t * rb_shape_get_next_iv_shape(rb_shape_t * shape, ID id);
156bool rb_shape_get_iv_index(rb_shape_t * shape, ID id, attr_index_t * value);
157bool rb_shape_get_iv_index_with_hint(shape_id_t shape_id, ID id, attr_index_t * value, shape_id_t *shape_id_hint);
158bool rb_shape_obj_too_complex(VALUE obj);
159
160void rb_shape_set_shape(VALUE obj, rb_shape_t* shape);
161rb_shape_t* rb_shape_get_shape(VALUE obj);
162int rb_shape_frozen_shape_p(rb_shape_t* shape);
163rb_shape_t* rb_shape_transition_shape_frozen(VALUE obj);
164bool rb_shape_transition_shape_remove_ivar(VALUE obj, ID id, rb_shape_t *shape, VALUE * removed);
165rb_shape_t* rb_shape_get_next(rb_shape_t* shape, VALUE obj, ID id);
166rb_shape_t* rb_shape_get_next_no_warnings(rb_shape_t* shape, VALUE obj, ID id);
167
168rb_shape_t * rb_shape_rebuild_shape(rb_shape_t * initial_shape, rb_shape_t * dest_shape);
169
170static inline uint32_t
171ROBJECT_IV_CAPACITY(VALUE obj)
172{
173 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
174 // Asking for capacity doesn't make sense when the object is using
175 // a hash table for storing instance variables
176 RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
177 return rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj))->capacity;
178}
179
180static inline st_table *
181ROBJECT_IV_HASH(VALUE obj)
182{
183 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
184 RUBY_ASSERT(rb_shape_obj_too_complex(obj));
185 return (st_table *)ROBJECT(obj)->as.heap.ivptr;
186}
187
188static inline void
189ROBJECT_SET_IV_HASH(VALUE obj, const st_table *tbl)
190{
191 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
192 RUBY_ASSERT(rb_shape_obj_too_complex(obj));
193 ROBJECT(obj)->as.heap.ivptr = (VALUE *)tbl;
194}
195
196size_t rb_id_table_size(const struct rb_id_table *tbl);
197
198static inline uint32_t
199ROBJECT_IV_COUNT(VALUE obj)
200{
201 if (rb_shape_obj_too_complex(obj)) {
202 return (uint32_t)rb_st_table_size(ROBJECT_IV_HASH(obj));
203 }
204 else {
205 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
206 RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
207 return rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj))->next_iv_index;
208 }
209}
210
211static inline uint32_t
212RBASIC_IV_COUNT(VALUE obj)
213{
214 return rb_shape_get_shape_by_id(rb_shape_get_shape_id(obj))->next_iv_index;
215}
216
217rb_shape_t *rb_shape_traverse_from_new_root(rb_shape_t *initial_shape, rb_shape_t *orig_shape);
218
219bool rb_shape_set_shape_id(VALUE obj, shape_id_t shape_id);
220
221VALUE rb_obj_debug_shape(VALUE self, VALUE obj);
222
223// For ext/objspace
224RUBY_SYMBOL_EXPORT_BEGIN
225typedef void each_shape_callback(rb_shape_t * shape, void *data);
226void rb_shape_each_shape(each_shape_callback callback, void *data);
227size_t rb_shape_memsize(rb_shape_t *shape);
228size_t rb_shape_edges_count(rb_shape_t *shape);
229size_t rb_shape_depth(rb_shape_t *shape);
230shape_id_t rb_shape_id(rb_shape_t * shape);
231RUBY_SYMBOL_EXPORT_END
232
233#endif
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
@ RUBY_T_OBJECT
Definition value_type.h:115