Skip to content

Commit 689ec51

Browse files
committed
Replicate heap_index in shape_id flags.
This is preparation to getting rid of `T_OBJECT` transitions. By first only replicating the information it's easier to ensure consistency.
1 parent 42cf301 commit 689ec51

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

object.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,15 @@ rb_obj_copy_ivar(VALUE dest, VALUE obj)
339339
shape_id_t dest_shape_id = src_shape_id;
340340
shape_id_t initial_shape_id = RBASIC_SHAPE_ID(dest);
341341

342-
if (RSHAPE(initial_shape_id)->heap_index != RSHAPE(src_shape_id)->heap_index || !rb_shape_canonical_p(src_shape_id)) {
343-
RUBY_ASSERT(RSHAPE(initial_shape_id)->type == SHAPE_T_OBJECT);
342+
RUBY_ASSERT(RSHAPE(initial_shape_id)->type == SHAPE_T_OBJECT);
344343

345-
dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
346-
if (UNLIKELY(rb_shape_too_complex_p(dest_shape_id))) {
347-
st_table *table = rb_st_init_numtable_with_size(src_num_ivs);
348-
rb_obj_copy_ivs_to_hash_table(obj, table);
349-
rb_obj_init_too_complex(dest, table);
344+
dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
345+
if (UNLIKELY(rb_shape_too_complex_p(dest_shape_id))) {
346+
st_table *table = rb_st_init_numtable_with_size(src_num_ivs);
347+
rb_obj_copy_ivs_to_hash_table(obj, table);
348+
rb_obj_init_too_complex(dest, table);
350349

351-
return;
352-
}
350+
return;
353351
}
354352

355353
VALUE *src_buf = ROBJECT_FIELDS(obj);

shape.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,10 @@ rb_shape_traverse_from_new_root(shape_id_t initial_shape_id, shape_id_t dest_sha
10921092
{
10931093
rb_shape_t *initial_shape = RSHAPE(initial_shape_id);
10941094
rb_shape_t *dest_shape = RSHAPE(dest_shape_id);
1095-
return shape_id(shape_traverse_from_new_root(initial_shape, dest_shape), dest_shape_id);
1095+
1096+
// Keep all dest_shape_id flags except for the heap_index.
1097+
shape_id_t dest_flags = (dest_shape_id & ~SHAPE_ID_HEAP_INDEX_MASK) | (initial_shape_id & SHAPE_ID_HEAP_INDEX_MASK);
1098+
return shape_id(shape_traverse_from_new_root(initial_shape, dest_shape), dest_flags);
10961099
}
10971100

10981101
// Rebuild a similar shape with the same ivars but starting from
@@ -1136,7 +1139,7 @@ rb_shape_rebuild(shape_id_t initial_shape_id, shape_id_t dest_shape_id)
11361139
RUBY_ASSERT(!rb_shape_too_complex_p(initial_shape_id));
11371140
RUBY_ASSERT(!rb_shape_too_complex_p(dest_shape_id));
11381141

1139-
return raw_shape_id(shape_rebuild(RSHAPE(initial_shape_id), RSHAPE(dest_shape_id)));
1142+
return shape_id(shape_rebuild(RSHAPE(initial_shape_id), RSHAPE(dest_shape_id)), initial_shape_id);
11401143
}
11411144

11421145
void
@@ -1238,6 +1241,14 @@ rb_shape_verify_consistency(VALUE obj, shape_id_t shape_id)
12381241
}
12391242
}
12401243

1244+
// All complex shape are in heap_index=0, it's a limitation
1245+
if (!rb_shape_too_complex_p(shape_id)) {
1246+
uint8_t flags_heap_index = rb_shape_heap_index(shape_id);
1247+
if (flags_heap_index != shape->heap_index) {
1248+
rb_bug("shape_id heap_index flags mismatch: flags=%u, transition=%u\n", flags_heap_index, shape->heap_index);
1249+
}
1250+
}
1251+
12411252
return true;
12421253
}
12431254
#endif
@@ -1288,6 +1299,7 @@ shape_id_t_to_rb_cShape(shape_id_t shape_id)
12881299

12891300
VALUE obj = rb_struct_new(rb_cShape,
12901301
INT2NUM(shape_id),
1302+
INT2NUM(shape_id & SHAPE_ID_OFFSET_MASK),
12911303
INT2NUM(shape->parent_id),
12921304
rb_shape_edge_name(shape),
12931305
INT2NUM(shape->next_field_index),
@@ -1528,7 +1540,7 @@ Init_default_shapes(void)
15281540
for (int i = 0; sizes[i] > 0; i++) {
15291541
rb_shape_t *t_object_shape = rb_shape_alloc_with_parent_id(0, INVALID_SHAPE_ID);
15301542
t_object_shape->type = SHAPE_T_OBJECT;
1531-
t_object_shape->heap_index = i;
1543+
t_object_shape->heap_index = i + 1;
15321544
t_object_shape->capacity = (uint32_t)((sizes[i] - offsetof(struct RObject, as.ary)) / sizeof(VALUE));
15331545
t_object_shape->edges = rb_managed_id_table_new(256);
15341546
t_object_shape->ancestor_index = LEAF;
@@ -1552,6 +1564,7 @@ Init_shape(void)
15521564
* :nodoc: */
15531565
VALUE rb_cShape = rb_struct_define_under(rb_cRubyVM, "Shape",
15541566
"id",
1567+
"raw_id",
15551568
"parent_id",
15561569
"edge_name",
15571570
"next_field_index",

shape.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@ STATIC_ASSERT(shape_id_num_bits, SHAPE_ID_NUM_BITS == sizeof(shape_id_t) * CHAR_
1616
#define SHAPE_ID_FL_FROZEN (SHAPE_FL_FROZEN << SHAPE_ID_OFFSET_NUM_BITS)
1717
#define SHAPE_ID_FL_HAS_OBJECT_ID (SHAPE_FL_HAS_OBJECT_ID << SHAPE_ID_OFFSET_NUM_BITS)
1818
#define SHAPE_ID_FL_TOO_COMPLEX (SHAPE_FL_TOO_COMPLEX << SHAPE_ID_OFFSET_NUM_BITS)
19+
#define SHAPE_ID_FL_EMBEDDED (SHAPE_FL_EMBEDDED << SHAPE_ID_OFFSET_NUM_BITS)
1920
#define SHAPE_ID_FL_NON_CANONICAL_MASK (SHAPE_FL_NON_CANONICAL_MASK << SHAPE_ID_OFFSET_NUM_BITS)
20-
#define SHAPE_ID_READ_ONLY_MASK (~SHAPE_ID_FL_FROZEN)
21+
22+
#define SHAPE_ID_HEAP_INDEX_BITS 3
23+
#define SHAPE_ID_HEAP_INDEX_OFFSET (SHAPE_ID_NUM_BITS - SHAPE_ID_HEAP_INDEX_BITS)
24+
#define SHAPE_ID_HEAP_INDEX_MAX ((1 << SHAPE_ID_HEAP_INDEX_BITS) - 1)
25+
#define SHAPE_ID_HEAP_INDEX_MASK (SHAPE_ID_HEAP_INDEX_MAX << SHAPE_ID_HEAP_INDEX_OFFSET)
26+
27+
// The interpreter doesn't care about embeded or frozen status when reading ivars.
28+
// So we normalize shape_id by clearing these bits to improve cache hits.
29+
// JITs however might care about it.
30+
#define SHAPE_ID_READ_ONLY_MASK (~(SHAPE_ID_FL_FROZEN | SHAPE_ID_FL_EMBEDDED | SHAPE_ID_HEAP_INDEX_MASK))
2131

2232
typedef uint32_t redblack_id_t;
2333

@@ -72,6 +82,7 @@ enum shape_flags {
7282
SHAPE_FL_FROZEN = 1 << 0,
7383
SHAPE_FL_HAS_OBJECT_ID = 1 << 1,
7484
SHAPE_FL_TOO_COMPLEX = 1 << 2,
85+
SHAPE_FL_EMBEDDED = 1 << 3,
7586

7687
SHAPE_FL_NON_CANONICAL_MASK = SHAPE_FL_FROZEN | SHAPE_FL_HAS_OBJECT_ID,
7788
};
@@ -189,10 +200,19 @@ rb_shape_canonical_p(shape_id_t shape_id)
189200
return !(shape_id & SHAPE_ID_FL_NON_CANONICAL_MASK);
190201
}
191202

203+
static inline uint8_t
204+
rb_shape_heap_index(shape_id_t shape_id)
205+
{
206+
return (uint8_t)((shape_id & SHAPE_ID_HEAP_INDEX_MASK) >> SHAPE_ID_HEAP_INDEX_OFFSET);
207+
}
208+
192209
static inline shape_id_t
193210
rb_shape_root(size_t heap_id)
194211
{
195-
return (shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID);
212+
shape_id_t heap_index = (shape_id_t)heap_id;
213+
214+
shape_id_t shape_id = (heap_index + FIRST_T_OBJECT_SHAPE_ID);
215+
return shape_id | ((heap_index + 1) << SHAPE_ID_HEAP_INDEX_OFFSET) | SHAPE_ID_FL_EMBEDDED;
196216
}
197217

198218
static inline bool

test/ruby/test_shapes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ def test_iv_index
976976
example.add_foo # makes a transition
977977
add_foo_shape = RubyVM::Shape.of(example)
978978
assert_equal([:@foo], example.instance_variables)
979-
assert_equal(initial_shape.id, add_foo_shape.parent.id)
979+
assert_equal(initial_shape.raw_id, add_foo_shape.parent.raw_id)
980980
assert_equal(1, add_foo_shape.next_field_index)
981981

982982
example.remove_foo # makes a transition
@@ -987,7 +987,7 @@ def test_iv_index
987987
example.add_bar # makes a transition
988988
bar_shape = RubyVM::Shape.of(example)
989989
assert_equal([:@bar], example.instance_variables)
990-
assert_equal(initial_shape.id, bar_shape.parent_id)
990+
assert_equal(initial_shape.raw_id, bar_shape.parent_id)
991991
assert_equal(1, bar_shape.next_field_index)
992992
end
993993

0 commit comments

Comments
 (0)