Ruby 4.1.0dev (2026-03-12 revision 235195f80b17243f348344bb698564386131068e)
shape.h (235195f80b17243f348344bb698564386131068e)
1#ifndef RUBY_SHAPE_H
2#define RUBY_SHAPE_H
3
4#include "internal/gc.h"
5#include "internal/struct.h"
6
7typedef uint16_t attr_index_t;
8typedef uint32_t shape_id_t;
9#define SHAPE_ID_NUM_BITS 32
10#define SHAPE_ID_OFFSET_NUM_BITS 19
11
12STATIC_ASSERT(shape_id_num_bits, SHAPE_ID_NUM_BITS == sizeof(shape_id_t) * CHAR_BIT);
13
14#define SHAPE_BUFFER_SIZE (1 << SHAPE_ID_OFFSET_NUM_BITS)
15#define SHAPE_ID_OFFSET_MASK (SHAPE_BUFFER_SIZE - 1)
16
17#define SHAPE_ID_HEAP_INDEX_BITS 5
18#define SHAPE_ID_HEAP_INDEX_MAX ((1 << SHAPE_ID_HEAP_INDEX_BITS) - 1)
19
20#define SHAPE_ID_HEAP_INDEX_OFFSET SHAPE_ID_OFFSET_NUM_BITS
21#define SHAPE_ID_FL_USHIFT (SHAPE_ID_OFFSET_NUM_BITS + SHAPE_ID_HEAP_INDEX_BITS)
22
23// shape_id_t bits:
24// 0-18 SHAPE_ID_OFFSET_MASK
25// index in rb_shape_tree.shape_list. Allow to access `rb_shape_t *`.
26// 19-23 SHAPE_ID_HEAP_INDEX_MASK
27// index in rb_shape_tree.capacities. Allow to access slot size.
28// Always 0 except for T_OBJECT.
29// 24 SHAPE_ID_FL_FROZEN
30// Whether the object is frozen or not.
31// 25 SHAPE_ID_FL_HAS_OBJECT_ID
32// Whether the object has an `SHAPE_OBJ_ID` transition.
33// 26 SHAPE_ID_FL_TOO_COMPLEX
34// The object is backed by a `st_table`.
35
36enum shape_id_fl_type {
37#define RBIMPL_SHAPE_ID_FL(n) (1<<(SHAPE_ID_FL_USHIFT+n))
38
39 SHAPE_ID_HEAP_INDEX_MASK = ((1 << SHAPE_ID_HEAP_INDEX_BITS) - 1) << SHAPE_ID_HEAP_INDEX_OFFSET,
40
41 SHAPE_ID_FL_FROZEN = RBIMPL_SHAPE_ID_FL(0),
42 SHAPE_ID_FL_HAS_OBJECT_ID = RBIMPL_SHAPE_ID_FL(1),
43 SHAPE_ID_FL_TOO_COMPLEX = RBIMPL_SHAPE_ID_FL(2),
44
45 SHAPE_ID_FL_NON_CANONICAL_MASK = SHAPE_ID_FL_FROZEN | SHAPE_ID_FL_HAS_OBJECT_ID,
46 SHAPE_ID_FLAGS_MASK = SHAPE_ID_HEAP_INDEX_MASK | SHAPE_ID_FL_NON_CANONICAL_MASK | SHAPE_ID_FL_TOO_COMPLEX,
47
48#undef RBIMPL_SHAPE_ID_FL
49};
50
51// This mask allows to check if a shape_id contains any ivar.
52// It relies on ROOT_SHAPE_WITH_OBJ_ID==1.
53enum shape_id_mask {
54 SHAPE_ID_HAS_IVAR_MASK = SHAPE_ID_FL_TOO_COMPLEX | (SHAPE_ID_OFFSET_MASK - 1),
55};
56
57// The interpreter doesn't care about frozen status or slot size when reading ivars.
58// So we normalize shape_id by clearing these bits to improve cache hits.
59// JITs however might care about it.
60#define SHAPE_ID_READ_ONLY_MASK (~(SHAPE_ID_FL_FROZEN | SHAPE_ID_HEAP_INDEX_MASK))
61
62typedef uint32_t redblack_id_t;
63
64#define SHAPE_MAX_FIELDS (attr_index_t)(-1)
65#define SHAPE_FLAG_SHIFT ((SIZEOF_VALUE * CHAR_BIT) - SHAPE_ID_NUM_BITS)
66#define SHAPE_FLAG_MASK (((VALUE)-1) >> SHAPE_ID_NUM_BITS)
67
68#define SHAPE_MAX_VARIATIONS 8
69
70#define INVALID_SHAPE_ID ((shape_id_t)-1)
71#define ATTR_INDEX_NOT_SET ((attr_index_t)-1)
72
73#define ROOT_SHAPE_ID 0x0
74#define ROOT_SHAPE_WITH_OBJ_ID 0x1
75#define ROOT_TOO_COMPLEX_SHAPE_ID (ROOT_SHAPE_ID | SHAPE_ID_FL_TOO_COMPLEX)
76#define ROOT_TOO_COMPLEX_WITH_OBJ_ID (ROOT_SHAPE_WITH_OBJ_ID | SHAPE_ID_FL_TOO_COMPLEX | SHAPE_ID_FL_HAS_OBJECT_ID)
77
78typedef struct redblack_node redblack_node_t;
79
80struct rb_shape {
81 VALUE edges; // id_table from ID (ivar) to next shape
82 ID edge_name; // ID (ivar) for transition from parent to rb_shape
83 redblack_node_t *ancestor_index;
84 shape_id_t parent_id;
85 attr_index_t next_field_index; // Fields are either ivars or internal properties like `object_id`
86 attr_index_t capacity; // Total capacity of the object with this shape
87 uint8_t type;
88};
89
90typedef struct rb_shape rb_shape_t;
91
93 ID key;
94 rb_shape_t *value;
95 redblack_id_t l;
96 redblack_id_t r;
97};
98
99enum shape_type {
100 SHAPE_ROOT,
101 SHAPE_IVAR,
102 SHAPE_OBJ_ID,
103};
104
105enum shape_flags {
106 SHAPE_FL_FROZEN = 1 << 0,
107 SHAPE_FL_HAS_OBJECT_ID = 1 << 1,
108 SHAPE_FL_TOO_COMPLEX = 1 << 2,
109
110 SHAPE_FL_NON_CANONICAL_MASK = SHAPE_FL_FROZEN | SHAPE_FL_HAS_OBJECT_ID,
111};
112
113typedef struct {
114 /* object shapes */
115 rb_shape_t *shape_list;
116 rb_shape_t *root_shape;
117 const attr_index_t *capacities;
118 rb_atomic_t next_shape_id;
119
120 redblack_node_t *shape_cache;
121 unsigned int cache_size;
123
124RUBY_SYMBOL_EXPORT_BEGIN
125RUBY_EXTERN rb_shape_tree_t rb_shape_tree;
126RUBY_SYMBOL_EXPORT_END
127
128static inline shape_id_t
129rb_shapes_count(void)
130{
131 return (shape_id_t)RUBY_ATOMIC_LOAD(rb_shape_tree.next_shape_id);
132}
133
135 uint64_t pack;
136 struct {
137 shape_id_t shape_id;
138 attr_index_t index;
139 } unpack;
140};
141
142static inline shape_id_t
143RBASIC_SHAPE_ID(VALUE obj)
144{
146 RUBY_ASSERT(!RB_TYPE_P(obj, T_IMEMO) || IMEMO_TYPE_P(obj, imemo_fields));
147#if RBASIC_SHAPE_ID_FIELD
148 return (shape_id_t)((RBASIC(obj)->shape_id));
149#else
150 return (shape_id_t)((RBASIC(obj)->flags) >> SHAPE_FLAG_SHIFT);
151#endif
152}
153
154// Same as RBASIC_SHAPE_ID but with flags that have no impact
155// on reads removed. e.g. Remove FL_FROZEN.
156static inline shape_id_t
157RBASIC_SHAPE_ID_FOR_READ(VALUE obj)
158{
159 return RBASIC_SHAPE_ID(obj) & SHAPE_ID_READ_ONLY_MASK;
160}
161
162#if RUBY_DEBUG
163bool rb_shape_verify_consistency(VALUE obj, shape_id_t shape_id);
164#endif
165
166static inline void
167RBASIC_SET_SHAPE_ID_NO_CHECKS(VALUE obj, shape_id_t shape_id)
168{
169#if RBASIC_SHAPE_ID_FIELD
170 RBASIC(obj)->shape_id = (VALUE)shape_id;
171#else
172 // Object shapes are occupying top bits
173 RBASIC(obj)->flags &= SHAPE_FLAG_MASK;
174 RBASIC(obj)->flags |= ((VALUE)(shape_id) << SHAPE_FLAG_SHIFT);
175#endif
176}
177
178static inline void
179RBASIC_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
180{
182 RUBY_ASSERT(!RB_TYPE_P(obj, T_IMEMO) || IMEMO_TYPE_P(obj, imemo_fields));
183
184 RBASIC_SET_SHAPE_ID_NO_CHECKS(obj, shape_id);
185
186 RUBY_ASSERT(rb_shape_verify_consistency(obj, shape_id));
187}
188
189void rb_set_boxed_class_shape_id(VALUE obj, shape_id_t shape_id);
190
191static inline void
192RB_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
193{
194 switch (BUILTIN_TYPE(obj)) {
195 case T_CLASS:
196 case T_MODULE:
197 rb_set_boxed_class_shape_id(obj, shape_id);
198 break;
199 default:
200 RBASIC_SET_SHAPE_ID(obj, shape_id);
201 break;
202 }
203}
204
205static inline rb_shape_t *
206RSHAPE(shape_id_t shape_id)
207{
208 uint32_t offset = (shape_id & SHAPE_ID_OFFSET_MASK);
209 RUBY_ASSERT(offset != INVALID_SHAPE_ID);
210
211 return &rb_shape_tree.shape_list[offset];
212}
213
214int32_t rb_shape_id_offset(void);
215
216RUBY_FUNC_EXPORTED shape_id_t rb_obj_shape_id(VALUE obj);
217shape_id_t rb_shape_get_next_iv_shape(shape_id_t shape_id, ID id);
218bool rb_shape_get_iv_index(shape_id_t shape_id, ID id, attr_index_t *value);
219bool rb_shape_get_iv_index_with_hint(shape_id_t shape_id, ID id, attr_index_t *value, shape_id_t *shape_id_hint);
220bool rb_shape_find_ivar(shape_id_t shape_id, ID id, shape_id_t *ivar_shape);
221
222typedef int rb_shape_foreach_transition_callback(shape_id_t shape_id, void *data);
223bool rb_shape_foreach_field(shape_id_t shape_id, rb_shape_foreach_transition_callback func, void *data);
224
225shape_id_t rb_shape_transition_frozen(VALUE obj);
226shape_id_t rb_shape_transition_complex(VALUE obj);
227shape_id_t rb_shape_transition_remove_ivar(VALUE obj, ID id, shape_id_t *removed_shape_id);
228shape_id_t rb_shape_transition_add_ivar(VALUE obj, ID id);
229shape_id_t rb_shape_transition_add_ivar_no_warnings(VALUE klass, shape_id_t original_shape_id, ID id);
230shape_id_t rb_shape_transition_object_id(VALUE obj);
231shape_id_t rb_shape_transition_heap(VALUE obj, size_t heap_index);
232shape_id_t rb_shape_object_id(shape_id_t original_shape_id);
233
234void rb_shape_free_all(void);
235
236shape_id_t rb_shape_rebuild(shape_id_t initial_shape_id, shape_id_t dest_shape_id);
237void rb_shape_copy_fields(VALUE dest, VALUE *dest_buf, shape_id_t dest_shape_id, VALUE *src_buf, shape_id_t src_shape_id);
238void rb_shape_copy_complex_ivars(VALUE dest, VALUE obj, shape_id_t src_shape_id, st_table *fields_table);
239
240static inline bool
241rb_shape_too_complex_p(shape_id_t shape_id)
242{
243 return shape_id & SHAPE_ID_FL_TOO_COMPLEX;
244}
245
246static inline bool
247rb_shape_obj_too_complex_p(VALUE obj)
248{
249 return !RB_SPECIAL_CONST_P(obj) && rb_shape_too_complex_p(RBASIC_SHAPE_ID(obj));
250}
251
252static inline bool
253rb_shape_has_object_id(shape_id_t shape_id)
254{
255 return shape_id & SHAPE_ID_FL_HAS_OBJECT_ID;
256}
257
258static inline bool
259rb_shape_canonical_p(shape_id_t shape_id)
260{
261 return !(shape_id & SHAPE_ID_FL_NON_CANONICAL_MASK);
262}
263
264static inline uint8_t
265rb_shape_heap_index(shape_id_t shape_id)
266{
267 return (uint8_t)((shape_id & SHAPE_ID_HEAP_INDEX_MASK) >> SHAPE_ID_HEAP_INDEX_OFFSET);
268}
269
270static inline shape_id_t
271rb_shape_root(size_t heap_id)
272{
273 shape_id_t heap_index = (shape_id_t)(heap_id + 1);
274 shape_id_t heap_flags = heap_index << SHAPE_ID_HEAP_INDEX_OFFSET;
275
276 RUBY_ASSERT((heap_flags & SHAPE_ID_HEAP_INDEX_MASK) == heap_flags);
277 RUBY_ASSERT(rb_shape_heap_index(heap_flags) == heap_index);
278
279 return ROOT_SHAPE_ID | heap_flags;
280}
281
282static inline shape_id_t
283RSHAPE_PARENT_RAW_ID(shape_id_t shape_id)
284{
285 return RSHAPE(shape_id)->parent_id;
286}
287
288static inline bool
289RSHAPE_DIRECT_CHILD_P(shape_id_t parent_id, shape_id_t child_id)
290{
291 return (parent_id & SHAPE_ID_FLAGS_MASK) == (child_id & SHAPE_ID_FLAGS_MASK) &&
292 RSHAPE(child_id)->parent_id == (parent_id & SHAPE_ID_OFFSET_MASK);
293}
294
295static inline enum shape_type
296RSHAPE_TYPE(shape_id_t shape_id)
297{
298 return RSHAPE(shape_id)->type;
299}
300
301static inline bool
302RSHAPE_TYPE_P(shape_id_t shape_id, enum shape_type type)
303{
304 return RSHAPE_TYPE(shape_id) == type;
305}
306
307static inline attr_index_t
308RSHAPE_EMBEDDED_CAPACITY(shape_id_t shape_id)
309{
310 uint8_t heap_index = rb_shape_heap_index(shape_id);
311 if (heap_index) {
312 return rb_shape_tree.capacities[heap_index - 1];
313 }
314 return 0;
315}
316
317static inline attr_index_t
318RSHAPE_CAPACITY(shape_id_t shape_id)
319{
320 attr_index_t embedded_capacity = RSHAPE_EMBEDDED_CAPACITY(shape_id);
321
322 if (embedded_capacity > RSHAPE(shape_id)->capacity) {
323 return embedded_capacity;
324 }
325 else {
326 return RSHAPE(shape_id)->capacity;
327 }
328}
329
330static inline attr_index_t
331RSHAPE_LEN(shape_id_t shape_id)
332{
333 return RSHAPE(shape_id)->next_field_index;
334}
335
336static inline attr_index_t
337RSHAPE_INDEX(shape_id_t shape_id)
338{
339 RUBY_ASSERT(RSHAPE_LEN(shape_id) > 0);
340 return RSHAPE_LEN(shape_id) - 1;
341}
342
343static inline ID
344RSHAPE_EDGE_NAME(shape_id_t shape_id)
345{
346 return RSHAPE(shape_id)->edge_name;
347}
348
349static inline uint32_t
350ROBJECT_FIELDS_CAPACITY(VALUE obj)
351{
352 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
353 // Asking for capacity doesn't make sense when the object is using
354 // a hash table for storing instance variables
355 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
356 return RSHAPE_CAPACITY(RBASIC_SHAPE_ID(obj));
357}
358
359static inline st_table *
360ROBJECT_FIELDS_HASH(VALUE obj)
361{
362 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
363 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
364 RUBY_ASSERT(FL_TEST_RAW(obj, ROBJECT_HEAP));
365
366 return (st_table *)ROBJECT(obj)->as.heap.fields;
367}
368
369static inline void
370ROBJECT_SET_FIELDS_HASH(VALUE obj, const st_table *tbl)
371{
372 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
373 RUBY_ASSERT(rb_shape_obj_too_complex_p(obj));
374 RUBY_ASSERT(FL_TEST_RAW(obj, ROBJECT_HEAP));
375
376 ROBJECT(obj)->as.heap.fields = (VALUE *)tbl;
377}
378
379static inline uint32_t
380ROBJECT_FIELDS_COUNT_COMPLEX(VALUE obj)
381{
382 return (uint32_t)rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
383}
384
385static inline uint32_t
386ROBJECT_FIELDS_COUNT_NOT_COMPLEX(VALUE obj)
387{
388 RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
389 RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
390 return RSHAPE(RBASIC_SHAPE_ID(obj))->next_field_index;
391}
392
393static inline uint32_t
394ROBJECT_FIELDS_COUNT(VALUE obj)
395{
396 if (rb_shape_obj_too_complex_p(obj)) {
397 return ROBJECT_FIELDS_COUNT_COMPLEX(obj);
398 }
399 else {
400 return ROBJECT_FIELDS_COUNT_NOT_COMPLEX(obj);
401 }
402}
403
404static inline uint32_t
405RBASIC_FIELDS_COUNT(VALUE obj)
406{
407 return RSHAPE(RBASIC_SHAPE_ID(obj))->next_field_index;
408}
409
410static inline bool
411rb_shape_obj_has_id(VALUE obj)
412{
413 return rb_shape_has_object_id(RBASIC_SHAPE_ID(obj));
414}
415
416static inline bool
417rb_shape_has_ivars(shape_id_t shape_id)
418{
419 return shape_id & SHAPE_ID_HAS_IVAR_MASK;
420}
421
422static inline bool
423rb_shape_obj_has_ivars(VALUE obj)
424{
425 return rb_shape_has_ivars(RBASIC_SHAPE_ID(obj));
426}
427
428static inline bool
429rb_shape_has_fields(shape_id_t shape_id)
430{
431 return shape_id & (SHAPE_ID_OFFSET_MASK | SHAPE_ID_FL_TOO_COMPLEX);
432}
433
434static inline bool
435rb_shape_obj_has_fields(VALUE obj)
436{
437 return rb_shape_has_fields(RBASIC_SHAPE_ID(obj));
438}
439
440static inline bool
441rb_obj_gen_fields_p(VALUE obj)
442{
443 switch (TYPE(obj)) {
444 case T_NONE:
445 case T_OBJECT:
446 case T_CLASS:
447 case T_MODULE:
448 case T_IMEMO:
449 return false;
450 default:
451 break;
452 }
453 return rb_shape_obj_has_fields(obj);
454}
455
456static inline bool
457rb_obj_using_gen_fields_table_p(VALUE obj)
458{
459 switch (BUILTIN_TYPE(obj)) {
460 case T_DATA:
461 if (RTYPEDDATA_P(obj)) return false;
462 break;
463
464 case T_STRUCT:
465 if (!FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS)) return false;
466 break;
467
468 default:
469 break;
470 }
471
472 return rb_obj_gen_fields_p(obj);
473}
474
475// For ext/objspace
476RUBY_SYMBOL_EXPORT_BEGIN
477typedef void each_shape_callback(shape_id_t shape_id, void *data);
478void rb_shape_each_shape_id(each_shape_callback callback, void *data);
479size_t rb_shape_memsize(shape_id_t shape);
480size_t rb_shape_edges_count(shape_id_t shape_id);
481size_t rb_shape_depth(shape_id_t shape_id);
482RUBY_SYMBOL_EXPORT_END
483
484#endif
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
Definition atomic.h:175
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
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 RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:652
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
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376
@ RUBY_T_OBJECT
Definition value_type.h:116