4#include "internal/gc.h"
5#include "internal/struct.h"
7typedef uint8_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
12STATIC_ASSERT(shape_id_num_bits, SHAPE_ID_NUM_BITS ==
sizeof(shape_id_t) * CHAR_BIT);
14#define SHAPE_BUFFER_SIZE (1 << SHAPE_ID_OFFSET_NUM_BITS)
15#define SHAPE_ID_OFFSET_MASK (SHAPE_BUFFER_SIZE - 1)
17#define SHAPE_ID_HEAP_INDEX_BITS 4
18#define SHAPE_ID_HEAP_INDEX_MAX (((attr_index_t)1 << SHAPE_ID_HEAP_INDEX_BITS) - 1)
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)
37enum shape_id_fl_type {
38#define RBIMPL_SHAPE_ID_FL(n) (1<<(SHAPE_ID_FL_USHIFT+n))
40 SHAPE_ID_HEAP_INDEX_MASK = ((1 << SHAPE_ID_HEAP_INDEX_BITS) - 1) << SHAPE_ID_HEAP_INDEX_OFFSET,
42 SHAPE_ID_FL_COMPLEX = RBIMPL_SHAPE_ID_FL(0),
43 SHAPE_ID_FL_FROZEN = RBIMPL_SHAPE_ID_FL(1),
44 SHAPE_ID_FL_HAS_OBJECT_ID = RBIMPL_SHAPE_ID_FL(2),
46 SHAPE_ID_FL_NON_CANONICAL_MASK = SHAPE_ID_FL_FROZEN | SHAPE_ID_FL_HAS_OBJECT_ID,
47 SHAPE_ID_FLAGS_MASK = SHAPE_ID_HEAP_INDEX_MASK | SHAPE_ID_FL_NON_CANONICAL_MASK | SHAPE_ID_FL_COMPLEX,
49#undef RBIMPL_SHAPE_ID_FL
55 SHAPE_ID_HAS_IVAR_MASK = SHAPE_ID_FL_COMPLEX | (SHAPE_ID_OFFSET_MASK - 1),
61#define SHAPE_ID_READ_ONLY_MASK (~(SHAPE_ID_FL_FROZEN | SHAPE_ID_HEAP_INDEX_MASK | SHAPE_ID_FL_HAS_OBJECT_ID))
63#define SHAPE_ID_WRITE_MASK (~(SHAPE_ID_HEAP_INDEX_MASK | SHAPE_ID_FL_HAS_OBJECT_ID))
65typedef uint32_t redblack_id_t;
67#define SHAPE_FLAG_SHIFT ((SIZEOF_VALUE * CHAR_BIT) - SHAPE_ID_NUM_BITS)
68#define SHAPE_FLAG_MASK (((VALUE)-1) >> SHAPE_ID_NUM_BITS)
70#define SHAPE_MAX_VARIATIONS 8
72#define INVALID_SHAPE_ID (SHAPE_BUFFER_SIZE - 1)
73#define ATTR_INDEX_NOT_SET ((attr_index_t)-1)
75#define ROOT_SHAPE_ID 0x0
76#define ROOT_SHAPE_WITH_OBJ_ID 0x1
77#define ROOT_COMPLEX_SHAPE_ID (ROOT_SHAPE_ID | SHAPE_ID_FL_COMPLEX)
78#define ROOT_COMPLEX_WITH_OBJ_ID (ROOT_SHAPE_WITH_OBJ_ID | SHAPE_ID_FL_COMPLEX | SHAPE_ID_FL_HAS_OBJECT_ID)
89 redblack_id_t ancestor_index;
90 shape_id_t parent_offset;
91 attr_index_t next_field_index;
92 attr_index_t capacity;
93 enum shape_type type : 8;
99 SHAPE_FL_FROZEN = 1 << 0,
100 SHAPE_FL_HAS_OBJECT_ID = 1 << 1,
101 SHAPE_FL_COMPLEX = 1 << 2,
103 SHAPE_FL_NON_CANONICAL_MASK = SHAPE_FL_FROZEN | SHAPE_FL_HAS_OBJECT_ID,
108 attr_index_t max_capacity;
109 attr_index_t heaps_count;
110 attr_index_t capacities[SHAPE_ID_HEAP_INDEX_MAX];
113RUBY_SYMBOL_EXPORT_BEGIN
115RUBY_SYMBOL_EXPORT_END
117size_t rb_shapes_cache_size(
void);
118size_t rb_shapes_count(
void);
120static inline shape_id_t
121RBASIC_SHAPE_ID(
VALUE obj)
125#if RBASIC_SHAPE_ID_FIELD
126 return (shape_id_t)((
RBASIC(obj)->shape_id));
128 return (shape_id_t)((
RBASIC(obj)->flags) >> SHAPE_FLAG_SHIFT);
134static inline shape_id_t
135RBASIC_SHAPE_ID_FOR_READ(
VALUE obj)
137 return RBASIC_SHAPE_ID(obj) & SHAPE_ID_READ_ONLY_MASK;
141bool rb_shape_verify_consistency(
VALUE obj, shape_id_t shape_id);
145RBASIC_SET_SHAPE_ID_NO_CHECKS(
VALUE obj, shape_id_t shape_id)
147#if RBASIC_SHAPE_ID_FIELD
151 RBASIC(obj)->flags &= SHAPE_FLAG_MASK;
152 RBASIC(obj)->flags |= ((
VALUE)(shape_id) << SHAPE_FLAG_SHIFT);
157RBASIC_SET_SHAPE_ID(
VALUE obj, shape_id_t shape_id)
162 RBASIC_SET_SHAPE_ID_NO_CHECKS(obj, shape_id);
164 RUBY_ASSERT(rb_shape_verify_consistency(obj, shape_id));
167static inline shape_id_t
168RSHAPE_FLAGS(shape_id_t shape_id)
170 return shape_id & SHAPE_ID_FLAGS_MASK;
173static inline shape_id_t
174RSHAPE_OFFSET(shape_id_t shape_id)
176 return shape_id & SHAPE_ID_OFFSET_MASK;
180RSHAPE(shape_id_t shape_id)
182 shape_id_t offset = RSHAPE_OFFSET(shape_id);
184 return &rb_shape_tree.shape_list[offset];
187int32_t rb_shape_id_offset(
void);
189RUBY_FUNC_EXPORTED shape_id_t rb_obj_shape_id(
VALUE obj);
190shape_id_t rb_shape_get_next_iv_shape(shape_id_t shape_id,
ID id);
191bool rb_shape_get_iv_index(shape_id_t shape_id,
ID id, attr_index_t *value);
192bool rb_shape_get_iv_index_with_hint(shape_id_t shape_id,
ID id, attr_index_t *value, shape_id_t *shape_id_hint);
193bool rb_shape_find_ivar(shape_id_t shape_id,
ID id, shape_id_t *ivar_shape);
195typedef int rb_shape_foreach_transition_callback(shape_id_t shape_id,
void *data);
196bool rb_shape_foreach_field(shape_id_t shape_id, rb_shape_foreach_transition_callback func,
void *data);
198shape_id_t rb_shape_transition_add_ivar_no_warnings(shape_id_t shape_id,
ID id,
VALUE klass);
200shape_id_t rb_shape_object_id(shape_id_t original_shape_id);
201shape_id_t rb_shape_rebuild(shape_id_t initial_shape_id, shape_id_t dest_shape_id);
202void rb_shape_copy_fields(
VALUE dest,
VALUE *dest_buf, shape_id_t dest_shape_id,
VALUE *src_buf, shape_id_t src_shape_id);
203void rb_shape_copy_complex_ivars(
VALUE dest,
VALUE obj, shape_id_t src_shape_id,
st_table *fields_table);
206rb_shape_frozen_p(shape_id_t shape_id)
208 return shape_id & SHAPE_ID_FL_FROZEN;
212rb_shape_complex_p(shape_id_t shape_id)
214 return shape_id & SHAPE_ID_FL_COMPLEX;
218rb_obj_shape_complex_p(
VALUE obj)
224rb_shape_has_object_id(shape_id_t shape_id)
226 return shape_id & SHAPE_ID_FL_HAS_OBJECT_ID;
230rb_shape_canonical_p(shape_id_t shape_id)
232 return !(shape_id & SHAPE_ID_FL_NON_CANONICAL_MASK);
236rb_shape_heap_index(shape_id_t shape_id)
238 return (uint8_t)((shape_id & SHAPE_ID_HEAP_INDEX_MASK) >> SHAPE_ID_HEAP_INDEX_OFFSET);
241static inline shape_id_t
242rb_shape_root(
size_t heap_id)
244 shape_id_t heap_index = (shape_id_t)(heap_id + 1);
245 shape_id_t heap_flags = heap_index << SHAPE_ID_HEAP_INDEX_OFFSET;
247 RUBY_ASSERT((heap_flags & SHAPE_ID_HEAP_INDEX_MASK) == heap_flags);
248 RUBY_ASSERT(rb_shape_heap_index(heap_flags) == heap_index);
250 return ROOT_SHAPE_ID | heap_flags;
253static inline shape_id_t
254RSHAPE_PARENT_OFFSET(shape_id_t shape_id)
256 return RSHAPE(shape_id)->parent_offset;
260RSHAPE_DIRECT_CHILD_P(shape_id_t parent_offset, shape_id_t child_id)
262 return RSHAPE_PARENT_OFFSET(child_id) == RSHAPE_OFFSET(parent_offset);
265static inline enum shape_type
266RSHAPE_TYPE(shape_id_t shape_id)
268 return RSHAPE(shape_id)->type;
272RSHAPE_TYPE_P(shape_id_t shape_id,
enum shape_type
type)
274 return RSHAPE_TYPE(shape_id) ==
type;
277static inline attr_index_t
278RSHAPE_EMBEDDED_CAPACITY(shape_id_t shape_id)
280 uint8_t heap_index = rb_shape_heap_index(shape_id);
282 return rb_shape_tree.capacities[heap_index - 1];
287static inline attr_index_t
288RSHAPE_CAPACITY(shape_id_t shape_id)
290 attr_index_t embedded_capacity = RSHAPE_EMBEDDED_CAPACITY(shape_id);
292 if (embedded_capacity > RSHAPE(shape_id)->capacity) {
293 return embedded_capacity;
296 return RSHAPE(shape_id)->capacity;
300static inline attr_index_t
301RSHAPE_LEN(shape_id_t shape_id)
303 return RSHAPE(shape_id)->next_field_index;
306static inline attr_index_t
307RSHAPE_INDEX(shape_id_t shape_id)
310 return RSHAPE_LEN(shape_id) - 1;
314RSHAPE_EDGE_NAME(shape_id_t shape_id)
316 return RSHAPE(shape_id)->edge_name;
319static inline uint32_t
320ROBJECT_FIELDS_CAPACITY(
VALUE obj)
326 return RSHAPE_CAPACITY(RBASIC_SHAPE_ID(obj));
330ROBJECT_FIELDS_HASH(
VALUE obj)
349static inline uint32_t
350ROBJECT_FIELDS_COUNT_COMPLEX(
VALUE obj)
352 return (uint32_t)rb_st_table_size(ROBJECT_FIELDS_HASH(obj));
355static inline uint32_t
356ROBJECT_FIELDS_COUNT_NOT_COMPLEX(
VALUE obj)
360 return RSHAPE(RBASIC_SHAPE_ID(obj))->next_field_index;
363static inline uint32_t
364ROBJECT_FIELDS_COUNT(
VALUE obj)
366 if (rb_obj_shape_complex_p(obj)) {
367 return ROBJECT_FIELDS_COUNT_COMPLEX(obj);
370 return ROBJECT_FIELDS_COUNT_NOT_COMPLEX(obj);
374static inline uint32_t
375RBASIC_FIELDS_COUNT(
VALUE obj)
377 return RSHAPE(RBASIC_SHAPE_ID(obj))->next_field_index;
381rb_obj_shape_has_id(
VALUE obj)
383 return rb_shape_has_object_id(RBASIC_SHAPE_ID(obj));
387rb_shape_has_ivars(shape_id_t shape_id)
389 return shape_id & SHAPE_ID_HAS_IVAR_MASK;
393rb_obj_shape_has_ivars(
VALUE obj)
395 return rb_shape_has_ivars(RBASIC_SHAPE_ID(obj));
399rb_shape_has_fields(shape_id_t shape_id)
401 return shape_id & (SHAPE_ID_OFFSET_MASK | SHAPE_ID_FL_COMPLEX);
405rb_obj_shape_has_fields(
VALUE obj)
407 return rb_shape_has_fields(RBASIC_SHAPE_ID(obj));
411rb_obj_gen_fields_p(
VALUE obj)
423 return rb_obj_shape_has_fields(obj);
427rb_obj_using_gen_fields_table_p(
VALUE obj)
435 if (!
FL_TEST_RAW(obj, RSTRUCT_GEN_FIELDS))
return false;
442 return rb_obj_gen_fields_p(obj);
445static inline shape_id_t
446rb_shape_transition_frozen(shape_id_t shape_id)
448 return shape_id | SHAPE_ID_FL_FROZEN;
451static inline shape_id_t
452rb_shape_transition_complex(shape_id_t shape_id)
454 shape_id_t next_shape_id = ROOT_COMPLEX_SHAPE_ID;
456 if (rb_shape_has_object_id(shape_id)) {
457 next_shape_id = ROOT_COMPLEX_WITH_OBJ_ID;
460 uint8_t heap_index = rb_shape_heap_index(shape_id);
462 next_shape_id |= rb_shape_root(heap_index - 1);
465 RUBY_ASSERT(rb_shape_has_object_id(shape_id) == rb_shape_has_object_id(next_shape_id));
467 return next_shape_id;
470static inline shape_id_t
471rb_shape_transition_offset(shape_id_t shape_id, shape_id_t offset)
473 offset = RSHAPE_OFFSET(offset);
474 RUBY_ASSERT(RSHAPE_OFFSET(shape_id) == offset || RSHAPE_DIRECT_CHILD_P(shape_id, offset));
475 return RSHAPE_FLAGS(shape_id) | offset;
478static inline shape_id_t
479rb_shape_transition_heap(shape_id_t shape_id,
size_t heap_index)
481 return (shape_id & (~SHAPE_ID_HEAP_INDEX_MASK)) | rb_shape_root(heap_index);
484shape_id_t rb_shape_transition_object_id(shape_id_t shape_id);
486static inline shape_id_t
487rb_obj_shape_transition_frozen(
VALUE obj)
490 return rb_shape_transition_frozen(RBASIC_SHAPE_ID(obj));
493static inline shape_id_t
494rb_obj_shape_transition_complex(
VALUE obj)
496 return rb_shape_transition_complex(RBASIC_SHAPE_ID(obj));
499static inline shape_id_t
500rb_obj_shape_transition_heap(
VALUE obj,
size_t heap_index)
502 return rb_shape_transition_heap(RBASIC_SHAPE_ID(obj), heap_index);
505static inline shape_id_t
506rb_obj_shape_transition_object_id(
VALUE obj)
508 return rb_shape_transition_object_id(RBASIC_SHAPE_ID(obj));
511shape_id_t rb_obj_shape_transition_remove_ivar(
VALUE obj,
ID id, shape_id_t *removed_shape_id);
512shape_id_t rb_obj_shape_transition_add_ivar(
VALUE obj,
ID id);
515RUBY_SYMBOL_EXPORT_BEGIN
516typedef void each_shape_callback(shape_id_t shape_id,
void *data);
517void rb_shape_each_shape_id(each_shape_callback callback,
void *data);
518size_t rb_shape_memsize(shape_id_t shape);
519size_t rb_shape_edges_count(shape_id_t shape_id);
520size_t rb_shape_depth(shape_id_t shape_id);
521RUBY_SYMBOL_EXPORT_END
527 shape_id_t shape_offset;
534STATIC_ASSERT(rb_getivar_cache_size,
sizeof(
union rb_getivar_cache) <=
sizeof(uint64_t));
536#define IVAR_CACHE_INIT ((uint64_t)-1)
537#define ATTR_INDEX_T_NUM_BITS (sizeof(attr_index_t) * CHAR_BIT)
540rb_getivar_cache_unpack(uint64_t packed)
548 cache.unpack.shape_offset &= SHAPE_ID_OFFSET_MASK;
552static inline uint64_t
553rb_getivar_cache_pack(shape_id_t shape_offset, attr_index_t index)
555 RUBY_ASSERT(shape_offset == RSHAPE_OFFSET(shape_offset));
560 .shape_offset = shape_offset,
569 shape_id_t source_shape_offset;
570 shape_id_t dest_shape_offset;
574rb_setivar_cache_unpack(uint64_t packed)
577 .index = (attr_index_t)packed,
578 .source_shape_offset = RSHAPE_OFFSET((shape_id_t)(packed >> ATTR_INDEX_T_NUM_BITS)),
579 .dest_shape_offset = RSHAPE_OFFSET((shape_id_t)(packed >> (ATTR_INDEX_T_NUM_BITS + SHAPE_ID_OFFSET_NUM_BITS))),
584static inline uint64_t
585rb_setivar_cache_pack(shape_id_t shape_offset, shape_id_t dest_shape_offset, attr_index_t index)
587 RUBY_ASSERT(shape_offset == RSHAPE_OFFSET(shape_offset));
588 RUBY_ASSERT(dest_shape_offset == RSHAPE_OFFSET(dest_shape_offset));
589 RUBY_ASSERT(shape_offset == dest_shape_offset || RSHAPE_DIRECT_CHILD_P(shape_offset, dest_shape_offset));
591 uint64_t packed_cache = (uint64_t)dest_shape_offset << (ATTR_INDEX_T_NUM_BITS + SHAPE_ID_OFFSET_NUM_BITS);
592 packed_cache |= (uint64_t)shape_offset << ATTR_INDEX_T_NUM_BITS;
593 packed_cache |= (uint64_t)index;
598ALWAYS_INLINE(
static shape_id_t rb_setivar_cache_revalidate(shape_id_t shape_id,
rb_setivar_cache cache));
603 RUBY_ASSERT(cache.dest_shape_offset == INVALID_SHAPE_ID || cache.dest_shape_offset == RSHAPE_OFFSET(cache.dest_shape_offset));
605 shape_id_t normalized_shape_id = shape_id & SHAPE_ID_WRITE_MASK;
606 if (UNLIKELY(normalized_shape_id != cache.source_shape_offset)) {
607 return INVALID_SHAPE_ID;
610 if (UNLIKELY(cache.index >= RSHAPE_CAPACITY(shape_id))) {
613 return INVALID_SHAPE_ID;
617 RUBY_ASSERT(cache.source_shape_offset == cache.dest_shape_offset || RSHAPE_DIRECT_CHILD_P(shape_id, cache.dest_shape_offset));
618 RUBY_ASSERT(cache.index < RSHAPE_CAPACITY(shape_id));
623 return rb_shape_transition_offset(shape_id, cache.dest_shape_offset);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_EXTERN
Declaration of externally visible global variables.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
#define TYPE(_)
Old name of rb_type.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define T_DATA
Old name of RUBY_T_DATA.
#define T_NONE
Old name of RUBY_T_NONE.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBASIC(obj)
Convenient casting macro.
#define ROBJECT(obj)
Convenient casting macro.
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.