5#include "internal/class.h"
6#include "internal/error.h"
7#include "internal/gc.h"
8#include "internal/object.h"
9#include "internal/symbol.h"
10#include "internal/variable.h"
20#define SHAPE_DEBUG (VM_CHECK_MODE > 0)
23#define REDBLACK_CACHE_SIZE (SHAPE_BUFFER_SIZE * 32)
27#define SINGLE_CHILD_TAG 0x1
28#define TAG_SINGLE_CHILD(x) (VALUE)((uintptr_t)(x) | SINGLE_CHILD_TAG)
29#define SINGLE_CHILD_MASK (~((uintptr_t)SINGLE_CHILD_TAG))
30#define SINGLE_CHILD_P(x) ((uintptr_t)(x) & SINGLE_CHILD_TAG)
31#define SINGLE_CHILD(x) (rb_shape_t *)((uintptr_t)(x) & SINGLE_CHILD_MASK)
32#define ANCESTOR_CACHE_THRESHOLD 10
33#define MAX_SHAPE_ID (INVALID_SHAPE_ID - 1)
34#define ANCESTOR_SEARCH_MAX_DEPTH 2
45typedef struct redblack_node redblack_node_t;
47static redblack_node_t *redblack_cache;
51#define RED (1<<(SHAPE_ID_FL_USHIFT))
53static inline redblack_node_t *
54redblack_node(redblack_id_t
id)
56 return id ? &redblack_cache[
id - 1] : LEAF;
59static redblack_node_t *
60redblack_left(redblack_node_t *node)
62 if (node->l == LEAF) {
67 redblack_node_t *left = redblack_node(node->l);
72static redblack_node_t *
73redblack_right(redblack_node_t *node)
75 if (node->r == LEAF) {
80 redblack_node_t *right = redblack_node(node->r);
85static redblack_node_t *
86redblack_find0(redblack_node_t *tree,
ID key)
92 RUBY_ASSERT(redblack_left(tree) == LEAF || redblack_left(tree)->key < tree->key);
93 RUBY_ASSERT(redblack_right(tree) == LEAF || redblack_right(tree)->key > tree->key);
95 if (tree->key == key) {
99 if (key < tree->key) {
100 return redblack_find0(redblack_left(tree), key);
103 return redblack_find0(redblack_right(tree), key);
109static redblack_node_t *
110redblack_find(redblack_id_t tree_id,
ID key)
112 return redblack_find0(redblack_node(tree_id), key);
115static inline shape_id_t
116redblack_value(redblack_node_t *node)
120 return node->value & ~RED;
124static inline shape_id_t
125redblack_color(redblack_node_t *node)
128 return node->value & RED;
134redblack_red_p(redblack_node_t *node)
136 return redblack_color(node) == RED;
140redblack_id_for(redblack_node_t *node)
147 redblack_node_t *redblack_nodes = redblack_cache;
148 redblack_id_t
id = (redblack_id_t)(node - redblack_nodes);
153static redblack_node_t *
154redblack_new(shape_id_t color, rb_id_serial_t key, shape_id_t value, redblack_node_t *left, redblack_node_t *right)
156 if (redblack_cache_size + 1 >= REDBLACK_CACHE_SIZE) {
164 redblack_node_t *redblack_nodes = redblack_cache;
167 node->value = value | color;
168 node->l = redblack_id_for(left);
169 node->r = redblack_id_for(right);
173static redblack_node_t *
174redblack_balance(shape_id_t color, rb_id_serial_t key, shape_id_t value, redblack_node_t *left, redblack_node_t *right)
176 if (color == BLACK) {
177 rb_id_serial_t new_key, new_left_key, new_right_key;
178 shape_id_t new_value, new_left_value, new_right_value;
179 redblack_node_t *new_left_left, *new_left_right, *new_right_left, *new_right_right;
181 if (redblack_red_p(left) && redblack_red_p(redblack_left(left))) {
183 new_right_value = value;
184 new_right_right = right;
187 new_value = redblack_value(left);
188 new_right_left = redblack_right(left);
190 new_left_key = redblack_left(left)->key;
191 new_left_value = redblack_value(redblack_left(left));
193 new_left_left = redblack_left(redblack_left(left));
194 new_left_right = redblack_right(redblack_left(left));
196 else if (redblack_red_p(left) && redblack_red_p(redblack_right(left))) {
198 new_right_value = value;
199 new_right_right = right;
201 new_left_key = left->key;
202 new_left_value = redblack_value(left);
203 new_left_left = redblack_left(left);
205 new_key = redblack_right(left)->key;
206 new_value = redblack_value(redblack_right(left));
207 new_left_right = redblack_left(redblack_right(left));
208 new_right_left = redblack_right(redblack_right(left));
210 else if (redblack_red_p(right) && redblack_red_p(redblack_left(right))) {
212 new_left_value = value;
213 new_left_left = left;
215 new_right_key = right->key;
216 new_right_value = redblack_value(right);
217 new_right_right = redblack_right(right);
219 new_key = redblack_left(right)->key;
220 new_value = redblack_value(redblack_left(right));
221 new_left_right = redblack_left(redblack_left(right));
222 new_right_left = redblack_right(redblack_left(right));
224 else if (redblack_red_p(right) && redblack_red_p(redblack_right(right))) {
226 new_left_value = value;
227 new_left_left = left;
229 new_key = right->key;
230 new_value = redblack_value(right);
231 new_left_right = redblack_left(right);
233 new_right_key = redblack_right(right)->key;
234 new_right_value = redblack_value(redblack_right(right));
235 new_right_left = redblack_left(redblack_right(right));
236 new_right_right = redblack_right(redblack_right(right));
239 return redblack_new(color, key, value, left, right);
244 RUBY_ASSERT(new_left_left == LEAF || new_left_left->key < new_left_key);
245 RUBY_ASSERT(new_left_right == LEAF || new_left_right->key > new_left_key);
246 RUBY_ASSERT(new_left_right == LEAF || new_left_right->key < new_key);
247 RUBY_ASSERT(new_right_left == LEAF || new_right_left->key < new_right_key);
248 RUBY_ASSERT(new_right_left == LEAF || new_right_left->key > new_key);
249 RUBY_ASSERT(new_right_right == LEAF || new_right_right->key > new_right_key);
252 RED, new_key, new_value,
253 redblack_new(BLACK, new_left_key, new_left_value, new_left_left, new_left_right),
254 redblack_new(BLACK, new_right_key, new_right_value, new_right_left, new_right_right));
257 return redblack_new(color, key, value, left, right);
260static redblack_node_t *
261redblack_insert_aux(redblack_node_t *tree, rb_id_serial_t key, shape_id_t value)
264 return redblack_new(RED, key, value, LEAF, LEAF);
267 redblack_node_t *left, *right;
268 if (key < tree->key) {
269 left = redblack_insert_aux(redblack_left(tree), key, value);
271 right = redblack_right(tree);
272 RUBY_ASSERT(right == LEAF || right->key > tree->key);
274 else if (key > tree->key) {
275 left = redblack_left(tree);
276 RUBY_ASSERT(left == LEAF || left->key < tree->key);
277 right = redblack_insert_aux(redblack_right(tree), key, value);
284 return redblack_balance(
285 redblack_color(tree),
287 redblack_value(tree),
294static redblack_node_t *
295redblack_force_black(redblack_node_t *node)
297 node->value = redblack_value(node);
302redblack_insert(redblack_node_t *tree, rb_id_serial_t key, shape_id_t value)
304 redblack_node_t *root = redblack_insert_aux(tree, key, value);
306 if (redblack_red_p(root)) {
307 return redblack_id_for(redblack_force_black(root));
310 return redblack_id_for(root);
322rb_shape_get_root_shape(
void)
324 return rb_shape_tree.shape_list;
328shape_tree_mark_and_move(
void *data)
330 rb_shape_t *cursor = rb_shape_get_root_shape();
331 rb_shape_t *end = RSHAPE(shape_next_id - 1);
332 while (cursor <= end) {
333 if (cursor->edges && !SINGLE_CHILD_P(cursor->edges)) {
334 rb_gc_mark_and_move(&cursor->edges);
341rb_shapes_cache_size(
void)
343 return redblack_cache ? redblack_cache_size : 0;
353shape_tree_memsize(
const void *data)
355 if (redblack_cache) {
356 return redblack_cache_size *
sizeof(redblack_node_t);
362 .wrap_struct_name =
"VM/shape_tree",
364 .dmark = shape_tree_mark_and_move,
366 .dsize = shape_tree_memsize,
367 .dcompact = shape_tree_mark_and_move,
369 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
377static inline shape_id_t
378SHAPE_OFFSET(rb_shape_t *shape)
381 return (shape_id_t)(shape - rb_shape_tree.shape_list);
384static inline shape_id_t
385SHAPE_ID(rb_shape_t *shape, shape_id_t previous_shape_id)
388 return SHAPE_OFFSET(shape) | RSHAPE_FLAGS(previous_shape_id);
392rb_shape_each_shape_id(each_shape_callback callback,
void *data)
394 rb_shape_t *start = rb_shape_get_root_shape();
395 rb_shape_t *cursor = start;
397 while (cursor < end) {
398 callback((shape_id_t)(cursor - start), data);
403RUBY_FUNC_EXPORTED shape_id_t
404rb_obj_shape_id(
VALUE obj)
407 rb_bug(
"rb_obj_shape_id: called on a special constant");
411 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
412 shape_id_t base = ROOT_SHAPE_ID;
417 base = RBASIC_SHAPE_ID(fields_obj) & ~(SHAPE_ID_LAYOUT_MASK | SHAPE_ID_CAPACITY_MASK);
419 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
420 return rb_shape_layout(shape_id) | (shape_id & SHAPE_ID_CAPACITY_MASK) | base;
422 return RBASIC_SHAPE_ID(obj);
426rb_shape_depth(shape_id_t shape_id)
429 rb_shape_t *shape = RSHAPE(shape_id);
431 while (shape->parent_offset != INVALID_SHAPE_ID) {
433 shape = RSHAPE(shape->parent_offset);
442 shape_id_t current, new_id;
446 if (current > MAX_SHAPE_ID) {
449 new_id = current + 1;
452 return &rb_shape_tree.shape_list[current];
456rb_shape_alloc_with_parent_offset(rb_id_serial_t edge_serial, shape_id_t parent_offset)
458 rb_shape_t *shape = shape_alloc();
459 if (!shape)
return NULL;
461 shape->edge_serial = edge_serial;
462 shape->next_field_index = 0;
463 shape->parent_offset = parent_offset;
470rb_shape_alloc(rb_id_serial_t edge_serial, rb_shape_t *parent,
enum shape_type
type)
472 rb_shape_t *shape = rb_shape_alloc_with_parent_offset(edge_serial, SHAPE_OFFSET(parent));
473 if (!shape)
return NULL;
475 shape->type = (uint8_t)
type;
476 shape->capacity = parent->capacity;
482static redblack_node_t *
483redblack_cache_ancestors(rb_shape_t *shape)
485 if (!(shape->ancestor_index || shape->parent_offset == INVALID_SHAPE_ID)) {
486 redblack_node_t *parent_index_node = redblack_cache_ancestors(RSHAPE(shape->parent_offset));
488 if (shape->type == SHAPE_IVAR) {
489 shape->ancestor_index = redblack_insert(parent_index_node, shape->edge_serial, SHAPE_OFFSET(shape));
492 if (shape->ancestor_index) {
493 redblack_node_t *inserted_node = redblack_find(shape->ancestor_index, shape->edge_serial);
495 RUBY_ASSERT(redblack_value(inserted_node) == SHAPE_OFFSET(shape));
500 shape->ancestor_index = redblack_id_for(parent_index_node);
504 return redblack_node(shape->ancestor_index);
507static redblack_node_t *
508redblack_cache_ancestors(rb_shape_t *shape)
515shape_grow_capa(attr_index_t current_capa)
517 size_t next_size = rb_obj_embedded_size(current_capa + 1);
518 if (UNLIKELY(!rb_gc_size_allocatable_p(next_size))) {
519 return rb_shape_max_capacity();
522 attr_index_t next_capa = rb_shape_capacity_for_slot_size(rb_gc_size_slot_size(next_size));
528rb_shape_alloc_new_child(rb_id_serial_t serial, rb_shape_t *shape,
enum shape_type shape_type)
530 rb_shape_t *new_shape = rb_shape_alloc(serial, shape, shape_type);
531 if (!new_shape)
return NULL;
533 switch (shape_type) {
536 if (UNLIKELY(shape->next_field_index >= shape->capacity)) {
537 RUBY_ASSERT(shape->next_field_index == shape->capacity);
538 new_shape->capacity = shape_grow_capa(shape->capacity);
541 RUBY_ASSERT(new_shape->capacity > shape->next_field_index);
542 new_shape->next_field_index = shape->next_field_index + 1;
543 if (new_shape->next_field_index > ANCESTOR_CACHE_THRESHOLD) {
545 redblack_cache_ancestors(new_shape);
550 rb_bug(
"Unreachable");
558get_next_shape_internal_atomic(rb_shape_t *shape, rb_id_serial_t serial,
enum shape_type shape_type,
bool *variation_created,
bool new_variations_allowed)
560 rb_shape_t *res = NULL;
562 *variation_created =
false;
566 edges_table = RUBY_ATOMIC_VALUE_LOAD(shape->edges);
571 if (SINGLE_CHILD_P(edges_table)) {
572 rb_shape_t *child = SINGLE_CHILD(edges_table);
575 if (child->edge_serial == serial) {
582 if (rb_managed_id_table_lookup_serial(edges_table, serial, &lookup_result)) {
583 res = (rb_shape_t *)lookup_result;
589 if (!res && new_variations_allowed) {
592 rb_shape_t *new_shape = rb_shape_alloc_new_child(serial, shape, shape_type);
598 new_edges = TAG_SINGLE_CHILD(new_shape);
602 if (SINGLE_CHILD_P(edges_table)) {
603 rb_shape_t *old_child = SINGLE_CHILD(edges_table);
604 new_edges = rb_managed_id_table_new(2);
605 rb_managed_id_table_insert(new_edges, rb_id_serial_to_id(old_child->edge_serial), (
VALUE)old_child);
608 new_edges = rb_managed_id_table_dup(edges_table);
611 rb_managed_id_table_insert(new_edges, rb_id_serial_to_id(new_shape->edge_serial), (
VALUE)new_shape);
612 *variation_created =
true;
629get_next_shape_internal(rb_shape_t *shape, rb_id_serial_t serial,
enum shape_type shape_type,
bool *variation_created,
bool new_variations_allowed)
631 if (rb_multi_ractor_p()) {
632 return get_next_shape_internal_atomic(shape, serial, shape_type, variation_created, new_variations_allowed);
635 rb_shape_t *res = NULL;
636 *variation_created =
false;
638 VALUE edges_table = shape->edges;
643 if (SINGLE_CHILD_P(edges_table)) {
644 rb_shape_t *child = SINGLE_CHILD(edges_table);
647 if (child->edge_serial == serial) {
654 if (rb_managed_id_table_lookup_serial(edges_table, serial, &lookup_result)) {
655 res = (rb_shape_t *)lookup_result;
664 if (!new_variations_allowed || rb_shapes_count() > MAX_SHAPE_ID) {
668 rb_shape_t *new_shape = rb_shape_alloc_new_child(serial, shape, shape_type);
672 shape->edges = TAG_SINGLE_CHILD(new_shape);
676 if (SINGLE_CHILD_P(edges_table)) {
677 rb_shape_t *old_child = SINGLE_CHILD(edges_table);
678 VALUE new_edges = rb_managed_id_table_new(2);
679 rb_managed_id_table_insert_serial(new_edges, old_child->edge_serial, (
VALUE)old_child);
683 rb_managed_id_table_insert_serial(shape->edges, new_shape->edge_serial, (
VALUE)new_shape);
684 *variation_created =
true;
695rb_shape_transition_object_id(shape_id_t original_shape_id)
697 RUBY_ASSERT(!rb_shape_has_object_id(original_shape_id));
699 rb_shape_t *original_shape = RSHAPE(original_shape_id);
702 rb_shape_t *shape = NULL;
703 if (LIKELY(original_shape->next_field_index < rb_shape_max_capacity())) {
704 shape = get_next_shape_internal(original_shape, rb_shape_tree.serial_object_id, SHAPE_OBJ_ID, &dont_care,
true);
707 return rb_shape_layout(original_shape_id) | ROOT_COMPLEX_WITH_OBJ_ID | RSHAPE_FLAGS(original_shape_id);
711 return SHAPE_ID(shape, original_shape_id) | SHAPE_ID_FL_HAS_OBJECT_ID;
715rb_shape_object_id_child_exists_p(shape_id_t shape_id)
720 return get_next_shape_internal(RSHAPE(shape_id), rb_shape_tree.serial_object_id, SHAPE_OBJ_ID, &dont_care,
false) != NULL;
724rb_shape_object_id(shape_id_t original_shape_id)
726 RUBY_ASSERT(rb_shape_has_object_id(original_shape_id));
728 rb_shape_t *shape = RSHAPE(original_shape_id);
729 while (shape->type != SHAPE_OBJ_ID) {
730 if (UNLIKELY(shape->parent_offset == INVALID_SHAPE_ID)) {
731 rb_bug(
"Missing object_id in shape tree");
733 shape = RSHAPE(shape->parent_offset);
736 return SHAPE_ID(shape, original_shape_id) | SHAPE_ID_FL_HAS_OBJECT_ID;
741shape_get_iv_index(rb_shape_t *shape, rb_id_serial_t serial, attr_index_t *value)
743 while (shape->parent_offset != INVALID_SHAPE_ID) {
744 if (shape->edge_serial == serial) {
745 enum shape_type shape_type;
746 shape_type = (
enum shape_type)shape->type;
748 switch (shape_type) {
751 *value = shape->next_field_index - 1;
756 rb_bug(
"Ivar should not exist on transition");
760 shape = RSHAPE(shape->parent_offset);
767static inline rb_shape_t *
768shape_get_next(rb_shape_t *shape,
enum shape_type shape_type,
VALUE klass, rb_id_serial_t serial,
bool emit_warnings)
774 if (shape_get_iv_index(shape, serial, &index)) {
775 rb_bug(
"rb_shape_get_next: trying to create ivar that already exists at index %u", index);
781 if (UNLIKELY(shape->next_field_index >= rb_shape_max_capacity())) {
785 bool allow_new_shape = RCLASS_VARIATION_COUNT(klass) < SHAPE_MAX_VARIATIONS;
786 bool variation_created =
false;
787 rb_shape_t *new_shape = get_next_shape_internal(shape, serial, shape_type, &variation_created, allow_new_shape);
795 if (new_shape->next_field_index > RCLASS_MAX_IV_COUNT(klass) && !RCLASS_EXPECT_NO_IVAR(klass)) {
796 RCLASS_SET_MAX_IV_COUNT(klass, new_shape->next_field_index);
799 if (variation_created) {
800 RCLASS_VARIATION_COUNT(klass)++;
803 if (RCLASS_VARIATION_COUNT(klass) >= SHAPE_MAX_VARIATIONS) {
806 "The class %"PRIsVALUE
" reached %d shape variations, instance variables accesses will be slower and memory usage increased.\n"
807 "It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method.",
819obj_get_owner_class(
VALUE obj)
822 if (IMEMO_TYPE_P(obj, imemo_fields)) {
823 VALUE owner = rb_imemo_fields_owner(obj);
841remove_shape_recursive(
VALUE obj, rb_shape_t *shape, rb_id_serial_t serial, rb_shape_t **removed_shape)
843 if (shape->parent_offset == INVALID_SHAPE_ID) {
846 *removed_shape = NULL;
850 if (shape->type == SHAPE_IVAR && shape->edge_serial == serial) {
851 *removed_shape = shape;
853 return RSHAPE(shape->parent_offset);
857 rb_shape_t *new_parent = remove_shape_recursive(obj, RSHAPE(shape->parent_offset), serial, removed_shape);
862 VALUE klass = obj_get_owner_class(obj);
863 rb_shape_t *new_child = shape_get_next(new_parent, shape->type, klass, shape->edge_serial,
true);
864 RUBY_ASSERT(!new_child || new_child->capacity <= shape->capacity);
877rb_obj_shape_transition_remove_ivar(
VALUE obj,
ID id, shape_id_t *removed_shape_id)
879 shape_id_t original_shape_id = RBASIC_SHAPE_ID(obj);
880 RUBY_ASSERT(!rb_shape_frozen_p(original_shape_id));
882 if (rb_shape_complex_p(original_shape_id)) {
883 return original_shape_id;
886 rb_shape_t *removed_shape = NULL;
887 rb_shape_t *new_shape = remove_shape_recursive(obj, RSHAPE(original_shape_id), rb_id_to_serial(
id), &removed_shape);
890 *removed_shape_id = SHAPE_OFFSET(removed_shape);
894 return SHAPE_ID(new_shape, original_shape_id);
896 else if (removed_shape) {
899 shape_id_t next_shape_id = rb_shape_transition_complex(original_shape_id);
900 RUBY_ASSERT(rb_shape_has_object_id(next_shape_id) == rb_shape_has_object_id(original_shape_id));
901 return next_shape_id;
903 return original_shape_id;
907rb_obj_shape_transition_add_ivar(
VALUE obj,
ID id)
909 shape_id_t original_shape_id = RBASIC_SHAPE_ID(obj);
910 RUBY_ASSERT(!rb_shape_frozen_p(original_shape_id));
912 VALUE klass = obj_get_owner_class(obj);
913 rb_shape_t *next_shape = shape_get_next(RSHAPE(original_shape_id), SHAPE_IVAR, klass, rb_id_to_serial(
id),
true);
915 return SHAPE_ID(next_shape, original_shape_id);
918 return rb_shape_transition_complex(original_shape_id);
923rb_shape_transition_add_ivar_no_warnings(shape_id_t original_shape_id,
ID id,
VALUE klass)
925 RUBY_ASSERT(!rb_shape_frozen_p(original_shape_id));
927 rb_shape_t *next_shape = shape_get_next(RSHAPE(original_shape_id), SHAPE_IVAR, klass, rb_id_to_serial(
id),
false);
929 return SHAPE_ID(next_shape, original_shape_id);
932 return rb_shape_transition_complex(original_shape_id);
939rb_shape_get_iv_index_with_hint(shape_id_t shape_id,
ID id, attr_index_t *value, shape_id_t *shape_id_hint)
941 attr_index_t index_hint = *value;
943 if (*shape_id_hint == INVALID_SHAPE_ID) {
944 *shape_id_hint = shape_id;
945 return rb_shape_get_iv_index(shape_id,
id, value);
948 rb_shape_t *shape = RSHAPE(shape_id);
949 rb_shape_t *initial_shape = shape;
950 rb_shape_t *shape_hint = RSHAPE(*shape_id_hint);
957 if (shape->ancestor_index && shape->next_field_index >= ANCESTOR_CACHE_THRESHOLD) {
958 depth = ANCESTOR_SEARCH_MAX_DEPTH;
961 rb_id_serial_t serial = rb_id_to_serial(
id);
962 while (depth > 0 && shape->next_field_index > index_hint) {
963 while (shape_hint->next_field_index > shape->next_field_index) {
964 shape_hint = RSHAPE(shape_hint->parent_offset);
967 if (shape_hint == shape) {
970 *shape_id_hint = SHAPE_OFFSET(shape);
973 if (shape->edge_serial == serial) {
975 *value = shape->next_field_index - 1;
976 *shape_id_hint = SHAPE_OFFSET(shape);
980 shape = RSHAPE(shape->parent_offset);
986 if (!shape->ancestor_index && initial_shape->ancestor_index) {
987 shape = initial_shape;
989 *shape_id_hint = shape_id;
990 return rb_shape_get_iv_index(SHAPE_ID(shape, shape_id),
id, value);
994shape_cache_find_ivar(rb_shape_t *shape, rb_id_serial_t serial, rb_shape_t **ivar_shape)
996 if (shape->ancestor_index && shape->next_field_index >= ANCESTOR_CACHE_THRESHOLD) {
997 redblack_node_t *node = redblack_find(shape->ancestor_index, serial);
999 *ivar_shape = RSHAPE(redblack_value(node));
1009shape_find_ivar(rb_shape_t *shape, rb_id_serial_t serial, rb_shape_t **ivar_shape)
1011 while (shape->parent_offset != INVALID_SHAPE_ID) {
1012 if (shape->edge_serial == serial) {
1014 *ivar_shape = shape;
1018 shape = RSHAPE(shape->parent_offset);
1025rb_shape_find_ivar(shape_id_t current_shape_id,
ID id, shape_id_t *ivar_shape_id)
1027 RUBY_ASSERT(!rb_shape_complex_p(current_shape_id));
1029 rb_shape_t *shape = RSHAPE(current_shape_id);
1030 rb_shape_t *ivar_shape;
1031 rb_id_serial_t serial = rb_id_to_serial(
id);
1033 if (!shape_cache_find_ivar(shape, serial, &ivar_shape)) {
1035 if (shape->ancestor_index && shape->next_field_index >= ANCESTOR_CACHE_THRESHOLD) {
1039 if (!shape_find_ivar(shape, serial, &ivar_shape)) {
1045 *ivar_shape_id = SHAPE_ID(ivar_shape, current_shape_id);
1051rb_shape_get_iv_index(shape_id_t shape_id,
ID id, attr_index_t *value)
1057 shape_id_t ivar_shape_id;
1058 if (rb_shape_find_ivar(shape_id,
id, &ivar_shape_id)) {
1059 *value = RSHAPE_INDEX(ivar_shape_id);
1066rb_shape_id_offset(
void)
1068 return sizeof(uintptr_t) - SHAPE_ID_NUM_BITS /
sizeof(uintptr_t);
1074shape_rebuild(rb_shape_t *initial_shape, rb_shape_t *dest_shape)
1076 rb_shape_t *midway_shape;
1080 if (dest_shape->type != initial_shape->type) {
1081 midway_shape = shape_rebuild(initial_shape, RSHAPE(dest_shape->parent_offset));
1082 if (UNLIKELY(!midway_shape)) {
1087 midway_shape = initial_shape;
1090 switch ((
enum shape_type)dest_shape->type) {
1093 midway_shape = get_next_shape_internal(midway_shape, dest_shape->edge_serial, SHAPE_IVAR, &dont_care,
true);
1101 return midway_shape;
1107rb_shape_rebuild(shape_id_t initial_shape_id, shape_id_t dest_shape_id)
1109 RUBY_ASSERT(RSHAPE_TYPE_P(initial_shape_id, SHAPE_ROOT));
1111 if (RB_UNLIKELY(rb_shape_complex_p(initial_shape_id))) {
1113 return initial_shape_id;
1116 if (RB_UNLIKELY(rb_shape_complex_p(dest_shape_id))) {
1117 return rb_shape_transition_complex(initial_shape_id);
1120 shape_id_t next_shape_id;
1122 if (dest_shape_id & SHAPE_ID_FL_HAS_OBJECT_ID) {
1123 rb_shape_t *next_shape = shape_rebuild(RSHAPE(initial_shape_id), RSHAPE(dest_shape_id));
1125 next_shape_id = SHAPE_ID(next_shape, initial_shape_id & ~SHAPE_ID_FL_NON_CANONICAL_MASK);
1128 return rb_shape_transition_complex(initial_shape_id | (dest_shape_id & ~SHAPE_ID_FL_NON_CANONICAL_MASK));
1133 next_shape_id = RSHAPE_OFFSET(dest_shape_id) | RSHAPE_FLAGS(initial_shape_id);
1135 return next_shape_id;
1139rb_shape_copy_fields(
VALUE dest,
VALUE *dest_buf, shape_id_t dest_shape_id,
VALUE *src_buf, shape_id_t src_shape_id)
1141 rb_shape_t *dest_shape = RSHAPE(dest_shape_id);
1142 rb_shape_t *src_shape = RSHAPE(src_shape_id);
1144 if (src_shape->next_field_index == dest_shape->next_field_index) {
1146 MEMCPY(dest_buf, src_buf,
VALUE, dest_shape->next_field_index);
1149 for (uint32_t i = 0; i < dest_shape->next_field_index; i++) {
1154 while (src_shape->parent_offset != INVALID_SHAPE_ID) {
1155 if (src_shape->type == SHAPE_IVAR) {
1156 while (dest_shape->edge_serial != src_shape->edge_serial) {
1157 if (UNLIKELY(dest_shape->parent_offset == INVALID_SHAPE_ID)) {
1158 rb_bug(
"Lost field %s", rb_id2name(rb_id_serial_to_id(src_shape->edge_serial)));
1160 dest_shape = RSHAPE(dest_shape->parent_offset);
1163 RB_OBJ_WRITE(dest, &dest_buf[dest_shape->next_field_index - 1], src_buf[src_shape->next_field_index - 1]);
1165 src_shape = RSHAPE(src_shape->parent_offset);
1171rb_shape_edges_count(shape_id_t shape_id)
1173 rb_shape_t *shape = RSHAPE(shape_id);
1175 if (SINGLE_CHILD_P(shape->edges)) {
1179 return rb_managed_id_table_size(shape->edges);
1186rb_shape_memsize(shape_id_t shape_id)
1188 rb_shape_t *shape = RSHAPE(shape_id);
1190 size_t memsize =
sizeof(rb_shape_t);
1191 if (shape->edges && !SINGLE_CHILD_P(shape->edges)) {
1192 memsize += rb_managed_id_table_size(shape->edges);
1198rb_shape_foreach_field(shape_id_t initial_shape_id, rb_shape_foreach_transition_callback func,
void *data)
1200 RUBY_ASSERT(!rb_shape_complex_p(initial_shape_id));
1202 rb_shape_t *shape = RSHAPE(initial_shape_id);
1203 if (shape->type == SHAPE_ROOT) {
1207 shape_id_t parent_offset = SHAPE_ID(RSHAPE(shape->parent_offset), initial_shape_id);
1208 if (rb_shape_foreach_field(parent_offset, func, data)) {
1209 switch (func(SHAPE_ID(shape, initial_shape_id), data)) {
1216 rb_bug(
"unreachable");
1231rb_shape_expected_layout(
VALUE obj)
1235 return SHAPE_ID_LAYOUT_ROBJECT;
1241 return SHAPE_ID_LAYOUT_OTHER;
1243 return SHAPE_ID_LAYOUT_RCLASS;
1247 return SHAPE_ID_LAYOUT_EXTENDED;
1250 if (IMEMO_TYPE_P(obj, imemo_fields)) {
1251 return SHAPE_ID_LAYOUT_ROBJECT;
1253 return SHAPE_ID_LAYOUT_OTHER;
1256 return SHAPE_ID_LAYOUT_OTHER;
1261shape_layout_name(shape_id_t shape_id)
1263 switch (rb_shape_layout(shape_id)) {
1264 case SHAPE_ID_LAYOUT_ROBJECT:
1266 case SHAPE_ID_LAYOUT_RCLASS:
1268 case SHAPE_ID_LAYOUT_EXTENDED:
1269 return "extended (or RData)";
1270 case SHAPE_ID_LAYOUT_OTHER:
1278rb_shape_verify_capacity_consistency_p(
VALUE obj)
1282 return IMEMO_TYPE_P(obj, imemo_fields);
1293rb_shape_verify_consistency(
VALUE obj, shape_id_t shape_id)
1295 if (shape_id == INVALID_SHAPE_ID) {
1296 rb_bug(
"Can't set INVALID_SHAPE_ID on an object");
1299 shape_id_t actual_layout = rb_shape_layout(rb_obj_shape_id(obj));
1300 shape_id_t expected_layout = rb_shape_expected_layout(obj);
1301 if (actual_layout != expected_layout) {
1303 rb_bug(
"shape_id layout mismatch: expected=%s actual=%s shape_id=%u obj=%s",
1304 shape_layout_name(expected_layout), shape_layout_name(actual_layout), shape_id, rb_obj_info(obj));
1308 if (shape_id == ROOT_SHAPE_ID) {
1312 rb_shape_t *shape = RSHAPE(shape_id);
1315 if (rb_shape_complex_p(shape_id)) {
1322 else if (IMEMO_TYPE_P(obj, imemo_fields)) {
1327 bool has_object_id =
false;
1328 while (shape->parent_offset != INVALID_SHAPE_ID) {
1329 if (shape->type == SHAPE_OBJ_ID) {
1330 has_object_id =
true;
1333 shape = RSHAPE(shape->parent_offset);
1336 if (rb_shape_has_object_id(shape_id)) {
1337 if (!has_object_id) {
1338 rb_bug(
"shape_id claim having obj_id but doesn't shape_id=%u, obj=%s", shape_id, rb_obj_info(obj));
1342 if (has_object_id) {
1343 rb_bug(
"shape_id claim not having obj_id but it does shape_id=%u, obj=%s", shape_id, rb_obj_info(obj));
1347 attr_index_t ivar_count = RSHAPE_LEN(shape_id);
1348 if (has_object_id) {
1355 RUBY_ASSERT(!(shape_id & SHAPE_ID_HAS_IVAR_MASK));
1359 if (rb_shape_verify_capacity_consistency_p(obj)) {
1360 attr_index_t shape_id_capacity = rb_shape_embedded_capacity(shape_id);
1362 size_t shape_id_slot_size = shape_id_capacity *
sizeof(
VALUE) +
sizeof(
struct RBasic);
1363 size_t actual_slot_size = rb_gc_obj_slot_size(obj);
1365 if (shape_id_capacity == SHAPE_ID_CAPACITY_MAX) {
1366 if (actual_slot_size < SHAPE_ID_CAPACITY_MAX) {
1367 rb_bug(
"shape_id_capacity is SHAPE_ID_CAPACITY_MAX, but actual slot size is only %zu", actual_slot_size);
1371 if (shape_id_slot_size != actual_slot_size) {
1372 rb_bug(
"shape_id capacity flags mismatch: shape_id_slot_size=%zu, gc_slot_size=%zu\n", shape_id_slot_size, actual_slot_size);
1388shape_complex(
VALUE self)
1391 return RBOOL(rb_shape_complex_p(shape_id));
1395shape_frozen(
VALUE self)
1398 return RBOOL(shape_id & SHAPE_ID_FL_FROZEN);
1402shape_has_object_id_p(
VALUE self)
1405 return RBOOL(rb_shape_has_object_id(shape_id));
1409shape_layout(
VALUE self)
1413 switch (rb_shape_layout(shape_id)) {
1414 case SHAPE_ID_LAYOUT_ROBJECT:
1415 return ID2SYM(rb_intern(
"robject"));
1416 case SHAPE_ID_LAYOUT_RCLASS:
1417 return ID2SYM(rb_intern(
"rclass"));
1418 case SHAPE_ID_LAYOUT_EXTENDED:
1419 return ID2SYM(rb_intern(
"extended_or_rdata"));
1420 case SHAPE_ID_LAYOUT_OTHER:
1421 return ID2SYM(rb_intern(
"other"));
1423 rb_bug(
"unknown shape layout: %u", rb_shape_layout(shape_id));
1430 if (is_instance_id(key)) {
1436static VALUE rb_shape_edge_name(rb_shape_t *shape);
1439shape_id_t_to_rb_cShape(shape_id_t shape_id)
1442 rb_shape_t *shape = RSHAPE(shape_id);
1446 INT2NUM(RSHAPE_OFFSET(shape_id)),
1447 INT2NUM(shape->parent_offset),
1448 rb_shape_edge_name(shape),
1449 INT2NUM(shape->next_field_index),
1450 INT2NUM(rb_shape_embedded_capacity(shape_id)),
1452 INT2NUM(RSHAPE_CAPACITY(shape_id)));
1457static enum rb_id_table_iterator_result
1458rb_edges_to_hash(
ID key,
VALUE value,
void *ref)
1460 rb_hash_aset(*(
VALUE *)ref, parse_key(key), shape_id_t_to_rb_cShape(SHAPE_OFFSET((rb_shape_t *)value)));
1461 return ID_TABLE_CONTINUE;
1465rb_shape_edges(
VALUE self)
1472 if (SINGLE_CHILD_P(shape->edges)) {
1473 rb_shape_t *child = SINGLE_CHILD(shape->edges);
1474 rb_edges_to_hash(rb_id_serial_to_id(child->edge_serial), (
VALUE)child, &hash);
1477 VALUE edges = shape->edges;
1478 rb_managed_id_table_foreach(edges, rb_edges_to_hash, &hash);
1487rb_shape_edge_name(rb_shape_t *shape)
1489 if (shape->edge_serial) {
1490 ID id = rb_id_serial_to_id(shape->edge_serial);
1491 if (is_instance_id(
id)) {
1494 return INT2NUM(shape->capacity);
1500rb_shape_export_depth(
VALUE self)
1503 return SIZET2NUM(rb_shape_depth(shape_id));
1507rb_shape_parent(
VALUE self)
1511 if (shape->parent_offset != INVALID_SHAPE_ID) {
1512 return shape_id_t_to_rb_cShape(shape->parent_offset);
1523 rb_raise(rb_eArgError,
"Can't get shape of special constant");
1525 return shape_id_t_to_rb_cShape(rb_obj_shape_id(obj));
1529rb_shape_root_shape(
VALUE self)
1531 return shape_id_t_to_rb_cShape(ROOT_SHAPE_ID);
1535rb_shape_shapes_available(
VALUE self)
1537 return ULL2NUM(MAX_SHAPE_ID - (rb_shapes_count() - 1));
1541rb_shape_exhaust(
int argc,
VALUE *argv,
VALUE self)
1544 int offset = argc == 1 ?
NUM2INT(argv[0]) : 0;
1550rb_shape_class_max_iv_count(
VALUE self,
VALUE klass)
1552 return INT2NUM(RCLASS_MAX_IV_COUNT(klass));
1555static VALUE shape_to_h(rb_shape_t *shape);
1557static enum rb_id_table_iterator_result collect_keys_and_values(
ID key,
VALUE value,
void *ref)
1559 rb_hash_aset(*(
VALUE *)ref, parse_key(key), shape_to_h((rb_shape_t *)value));
1560 return ID_TABLE_CONTINUE;
1567 if (SINGLE_CHILD_P(edges)) {
1568 rb_shape_t *child = SINGLE_CHILD(edges);
1569 collect_keys_and_values(rb_id_serial_to_id(child->edge_serial), (
VALUE)child, &hash);
1572 rb_managed_id_table_foreach(edges, collect_keys_and_values, &hash);
1579shape_to_h(rb_shape_t *shape)
1584 rb_hash_aset(
rb_shape,
ID2SYM(rb_intern(
"edges")), edges(shape->edges));
1586 if (shape == rb_shape_get_root_shape()) {
1593 rb_hash_aset(
rb_shape,
ID2SYM(rb_intern(
"edge_name")), rb_id2str(rb_id_serial_to_id(shape->edge_serial)));
1598shape_transition_tree(
VALUE self)
1600 return shape_to_h(rb_shape_get_root_shape());
1606 shape_id_t shape_id =
NUM2UINT(
id);
1607 if (shape_id >= rb_shapes_count()) {
1608 rb_raise(rb_eArgError,
"Shape ID %d is out of bounds\n", shape_id);
1610 return shape_id_t_to_rb_cShape(shape_id);
1615#include <sys/mman.h>
1619Init_default_shapes(
void)
1621 attr_index_t max_capacity = (attr_index_t)((rb_gc_max_allocation_size() -
sizeof(
struct RBasic)) /
sizeof(
VALUE));
1622 if (max_capacity > SHAPE_ID_CAPACITY_MAX) max_capacity = SHAPE_ID_CAPACITY_MAX;
1623 rb_shape_tree.max_capacity = max_capacity;
1626 size_t shape_list_mmap_size = rb_size_mul_or_raise(SHAPE_BUFFER_SIZE,
sizeof(rb_shape_t),
rb_eRuntimeError);
1627 rb_shape_tree.shape_list = (rb_shape_t *)mmap(NULL, shape_list_mmap_size,
1628 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1629 if (rb_shape_tree.shape_list == MAP_FAILED) {
1630 rb_shape_tree.shape_list = 0;
1633 ruby_annotate_mmap(rb_shape_tree.shape_list, shape_list_mmap_size,
"Ruby:Init_default_shapes:shape_list");
1636 rb_shape_tree.shape_list =
xcalloc(SHAPE_BUFFER_SIZE,
sizeof(rb_shape_t));
1639 if (!rb_shape_tree.shape_list) {
1643 rb_shape_tree.id_object_id = rb_make_internal_id();
1644 rb_shape_tree.serial_object_id = rb_id_to_serial(rb_shape_tree.id_object_id);
1647 size_t shape_cache_mmap_size = rb_size_mul_or_raise(REDBLACK_CACHE_SIZE,
sizeof(redblack_node_t),
rb_eRuntimeError);
1648 redblack_cache = (redblack_node_t *)mmap(NULL, shape_cache_mmap_size,
1649 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1650 redblack_cache_size = 0;
1655 if (redblack_cache == MAP_FAILED) {
1656 redblack_cache = NULL;
1657 redblack_cache_size = REDBLACK_CACHE_SIZE;
1660 ruby_annotate_mmap(redblack_cache, shape_cache_mmap_size,
"Ruby:Init_default_shapes:shape_cache");
1664 rb_gc_register_address(&shape_tree_obj);
1672 rb_shape_t *root = rb_shape_alloc_with_parent_offset(0, INVALID_SHAPE_ID);
1674 root->type = SHAPE_ROOT;
1676 RUBY_ASSERT(!(SHAPE_OFFSET(root) & SHAPE_ID_HAS_IVAR_MASK));
1679 rb_shape_t *root_with_obj_id = get_next_shape_internal(root, rb_shape_tree.serial_object_id, SHAPE_OBJ_ID, &dontcare,
true);
1681 RUBY_ASSERT(SHAPE_OFFSET(root_with_obj_id) == ROOT_SHAPE_WITH_OBJ_ID);
1682 RUBY_ASSERT(root_with_obj_id->type == SHAPE_OBJ_ID);
1683 RUBY_ASSERT(root_with_obj_id->edge_serial == rb_shape_tree.serial_object_id);
1684 RUBY_ASSERT(root_with_obj_id->next_field_index == 1);
1685 RUBY_ASSERT(!(SHAPE_OFFSET(root_with_obj_id) & SHAPE_ID_HAS_IVAR_MASK));
1686 (void)root_with_obj_id;
1701 "embedded_capacity",
1714 rb_define_const(rb_cShape,
"SHAPE_ROOT",
INT2NUM(SHAPE_ROOT));
1715 rb_define_const(rb_cShape,
"SHAPE_IVAR",
INT2NUM(SHAPE_IVAR));
1716 rb_define_const(rb_cShape,
"SHAPE_ID_NUM_BITS",
INT2NUM(SHAPE_ID_NUM_BITS));
1717 rb_define_const(rb_cShape,
"SHAPE_FLAG_SHIFT",
INT2NUM(SHAPE_FLAG_SHIFT));
1718 rb_define_const(rb_cShape,
"SHAPE_MAX_VARIATIONS",
INT2NUM(SHAPE_MAX_VARIATIONS));
1719 rb_define_const(rb_cShape,
"SHAPE_MAX_FIELDS",
INT2NUM(rb_shape_max_capacity()));
1720 rb_define_const(rb_cShape,
"SIZEOF_RB_SHAPE_T",
INT2NUM(
sizeof(rb_shape_t)));
1721 rb_define_const(rb_cShape,
"SIZEOF_REDBLACK_NODE_T",
INT2NUM(
sizeof(redblack_node_t)));
1722 rb_define_const(rb_cShape,
"SHAPE_BUFFER_SIZE",
INT2NUM(
sizeof(rb_shape_t) * SHAPE_BUFFER_SIZE));
1723 rb_define_const(rb_cShape,
"REDBLACK_CACHE_SIZE",
INT2NUM(
sizeof(redblack_node_t) * REDBLACK_CACHE_SIZE));
#define RUBY_ATOMIC_VALUE_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
#define RUBY_ATOMIC_SET(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type.
#define RUBY_ALIGNAS
Wraps (or simulates) alignas.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define FL_USER19
Old name of RUBY_FL_USER19.
#define T_DATA
Old name of RUBY_T_DATA.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#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.
#define xcalloc
Old name of ruby_xcalloc.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eRuntimeError
RuntimeError exception.
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
void rb_memerror(void)
Triggers out-of-memory error.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_struct_define_under(VALUE space, const char *name,...)
Identical to rb_struct_define(), except it defines the class under the specified namespace instead of...
VALUE rb_struct_new(VALUE klass,...)
Creates an instance of the given struct.
VALUE rb_struct_getmember(VALUE self, ID key)
Identical to rb_struct_aref(), except it takes ID instead of VALUE.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
C99 shim for <stdbool.h>.
Ruby object's base components.
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.