Skip to content

Commit a6435be

Browse files
committed
variable.c: Refactor rb_obj_field_* to take shape_id_t
1 parent fa2414f commit a6435be

File tree

6 files changed

+67
-67
lines changed

6 files changed

+67
-67
lines changed

gc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,24 +1865,24 @@ static VALUE
18651865
object_id(VALUE obj)
18661866
{
18671867
VALUE id = Qfalse;
1868-
rb_shape_t *shape = rb_obj_shape(obj);
18691868
unsigned int lock_lev;
18701869

18711870
// We could avoid locking if the object isn't shareable
18721871
// but we'll lock anyway to lookup the next shape, and
18731872
// we'd at least need to generate the object_id using atomics.
18741873
lock_lev = rb_gc_vm_lock();
18751874

1876-
if (rb_shape_has_object_id(shape)) {
1877-
rb_shape_t *object_id_shape = rb_shape_object_id_shape(obj);
1878-
id = rb_obj_field_get(obj, object_id_shape);
1875+
shape_id_t shape_id = rb_obj_shape_id(obj);
1876+
shape_id_t object_id_shape_id = rb_shape_transition_object_id(obj);
1877+
1878+
if (shape_id >= object_id_shape_id) {
1879+
id = rb_obj_field_get(obj, object_id_shape_id);
18791880
}
18801881
else {
18811882
id = ULL2NUM(next_object_id);
18821883
next_object_id += OBJ_ID_INCREMENT;
18831884

1884-
rb_shape_t *object_id_shape = rb_shape_object_id_shape(obj);
1885-
rb_obj_field_set(obj, object_id_shape, id);
1885+
rb_obj_field_set(obj, object_id_shape_id, id);
18861886
if (RB_UNLIKELY(id_to_obj_tbl)) {
18871887
st_insert(id_to_obj_tbl, (st_data_t)id, (st_data_t)obj);
18881888
}

internal/variable.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ void rb_obj_copy_ivs_to_hash_table(VALUE obj, st_table *table);
5353
void rb_obj_init_too_complex(VALUE obj, st_table *table);
5454
void rb_evict_ivars_to_hash(VALUE obj);
5555
void rb_evict_fields_to_hash(VALUE obj);
56-
VALUE rb_obj_field_get(VALUE obj, rb_shape_t *target_shape);
56+
VALUE rb_obj_field_get(VALUE obj, shape_id_t target_shape_id);
5757
void rb_ivar_set_internal(VALUE obj, ID id, VALUE val);
58-
void rb_obj_field_set(VALUE obj, rb_shape_t *target_shape, VALUE val);
58+
void rb_obj_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val);
5959

6060
RUBY_SYMBOL_EXPORT_BEGIN
6161
/* variable.c (export) */

shape.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@ rb_shape_has_object_id(rb_shape_t *shape)
745745
return shape->flags & SHAPE_FL_HAS_OBJECT_ID;
746746
}
747747

748-
rb_shape_t *
749-
rb_shape_object_id_shape(VALUE obj)
748+
shape_id_t
749+
rb_shape_transition_object_id(VALUE obj)
750750
{
751751
rb_shape_t* shape = rb_obj_shape(obj);
752752
RUBY_ASSERT(shape);
@@ -755,13 +755,13 @@ rb_shape_object_id_shape(VALUE obj)
755755
while (shape->type != SHAPE_OBJ_ID) {
756756
shape = RSHAPE(shape->parent_id);
757757
}
758-
return shape;
759758
}
760-
761-
bool dont_care;
762-
rb_shape_t* next_shape = get_next_shape_internal(shape, ruby_internal_object_id, SHAPE_OBJ_ID, &dont_care, true);
763-
RUBY_ASSERT(next_shape);
764-
return next_shape;
759+
else {
760+
bool dont_care;
761+
shape = get_next_shape_internal(shape, ruby_internal_object_id, SHAPE_OBJ_ID, &dont_care, true);
762+
}
763+
RUBY_ASSERT(shape);
764+
return rb_shape_id(shape);
765765
}
766766

767767
/*

shape.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ shape_id_t rb_shape_transition_complex(VALUE obj);
168168
bool rb_shape_transition_remove_ivar(VALUE obj, ID id, VALUE *removed);
169169
shape_id_t rb_shape_transition_add_ivar(VALUE obj, ID id);
170170
shape_id_t rb_shape_transition_add_ivar_no_warnings(VALUE obj, ID id);
171+
shape_id_t rb_shape_transition_object_id(VALUE obj);
171172

172-
rb_shape_t *rb_shape_object_id_shape(VALUE obj);
173173
bool rb_shape_has_object_id(rb_shape_t *shape);
174174
void rb_shape_free_all(void);
175175

test/ruby/test_shapes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ class TooComplex
681681
id_shape = RubyVM::Shape.of(tc)
682682
refute_equal frozen_shape.id, id_shape.id
683683
assert_predicate id_shape, :too_complex?
684-
assert_predicate id_shape, :shape_frozen?
685684
assert_predicate id_shape, :has_object_id?
685+
assert_predicate id_shape, :shape_frozen?
686686
687687
assert_equal 3, tc.very_unique
688688
assert_equal 3, Ractor.make_shareable(tc).very_unique

variable.c

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,12 +1345,12 @@ gen_fields_tbl_count(VALUE obj, const struct gen_fields_tbl *fields_tbl)
13451345
}
13461346

13471347
VALUE
1348-
rb_obj_field_get(VALUE obj, rb_shape_t *target_shape)
1348+
rb_obj_field_get(VALUE obj, shape_id_t target_shape_id)
13491349
{
13501350
RUBY_ASSERT(!SPECIAL_CONST_P(obj));
1351-
RUBY_ASSERT(target_shape->type == SHAPE_IVAR || target_shape->type == SHAPE_OBJ_ID);
1351+
RUBY_ASSERT(RSHAPE(target_shape_id)->type == SHAPE_IVAR || RSHAPE(target_shape_id)->type == SHAPE_OBJ_ID);
13521352

1353-
if (rb_shape_too_complex_p(target_shape)) {
1353+
if (rb_shape_id_too_complex_p(target_shape_id)) {
13541354
st_table *fields_hash;
13551355
switch (BUILTIN_TYPE(obj)) {
13561356
case T_CLASS:
@@ -1370,12 +1370,12 @@ rb_obj_field_get(VALUE obj, rb_shape_t *target_shape)
13701370
break;
13711371
}
13721372
VALUE value = Qundef;
1373-
st_lookup(fields_hash, target_shape->edge_name, &value);
1373+
st_lookup(fields_hash, RSHAPE(target_shape_id)->edge_name, &value);
13741374
RUBY_ASSERT(!UNDEF_P(value));
13751375
return value;
13761376
}
13771377

1378-
attr_index_t attr_index = target_shape->next_field_index - 1;
1378+
attr_index_t attr_index = RSHAPE(target_shape_id)->next_field_index - 1;
13791379
VALUE *fields;
13801380
switch (BUILTIN_TYPE(obj)) {
13811381
case T_CLASS:
@@ -1689,7 +1689,7 @@ static struct general_ivar_set_result
16891689
general_ivar_set(VALUE obj, ID id, VALUE val, void *data,
16901690
VALUE *(*shape_fields_func)(VALUE, void *),
16911691
void (*shape_resize_fields_func)(VALUE, attr_index_t, attr_index_t, void *),
1692-
void (*set_shape_func)(VALUE, rb_shape_t *, void *),
1692+
void (*set_shape_id_func)(VALUE, shape_id_t, void *),
16931693
void (*transition_too_complex_func)(VALUE, void *),
16941694
st_table *(*too_complex_table_func)(VALUE, void *))
16951695
{
@@ -1726,7 +1726,7 @@ general_ivar_set(VALUE obj, ID id, VALUE val, void *data,
17261726

17271727
RUBY_ASSERT(next_shape->type == SHAPE_IVAR);
17281728
RUBY_ASSERT(index == (next_shape->next_field_index - 1));
1729-
set_shape_func(obj, next_shape, data);
1729+
set_shape_id_func(obj, next_shape_id, data);
17301730
}
17311731

17321732
VALUE *table = shape_fields_func(obj, data);
@@ -1748,34 +1748,36 @@ general_ivar_set(VALUE obj, ID id, VALUE val, void *data,
17481748
}
17491749

17501750
static void
1751-
general_field_set(VALUE obj, rb_shape_t *target_shape, VALUE val, void *data,
1751+
general_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val, void *data,
17521752
VALUE *(*shape_fields_func)(VALUE, void *),
17531753
void (*shape_resize_fields_func)(VALUE, attr_index_t, attr_index_t, void *),
1754-
void (*set_shape_func)(VALUE, rb_shape_t *, void *),
1754+
void (*set_shape_id_func)(VALUE, shape_id_t, void *),
17551755
void (*transition_too_complex_func)(VALUE, void *),
17561756
st_table *(*too_complex_table_func)(VALUE, void *))
17571757
{
17581758
rb_shape_t *current_shape = rb_obj_shape(obj);
17591759

1760-
if (UNLIKELY(rb_shape_too_complex_p(target_shape))) {
1760+
if (UNLIKELY(rb_shape_id_too_complex_p(target_shape_id))) {
17611761
if (UNLIKELY(!rb_shape_too_complex_p(current_shape))) {
17621762
transition_too_complex_func(obj, data);
17631763
}
17641764

1765-
set_shape_func(obj, target_shape, data);
1766-
17671765
st_table *table = too_complex_table_func(obj, data);
1768-
st_insert(table, (st_data_t)target_shape->edge_name, (st_data_t)val);
1766+
if (RSHAPE(target_shape_id)->next_field_index > current_shape->next_field_index) {
1767+
set_shape_id_func(obj, target_shape_id, data);
1768+
}
1769+
1770+
st_insert(table, (st_data_t)RSHAPE(target_shape_id)->edge_name, (st_data_t)val);
17691771
RB_OBJ_WRITTEN(obj, Qundef, val);
17701772
}
17711773
else {
1772-
attr_index_t index = target_shape->next_field_index - 1;
1774+
attr_index_t index = RSHAPE(target_shape_id)->next_field_index - 1;
17731775
if (index >= current_shape->capacity) {
1774-
shape_resize_fields_func(obj, current_shape->capacity, target_shape->capacity, data);
1776+
shape_resize_fields_func(obj, current_shape->capacity, RSHAPE(target_shape_id)->capacity, data);
17751777
}
17761778

1777-
if (target_shape->next_field_index > current_shape->next_field_index) {
1778-
set_shape_func(obj, target_shape, data);
1779+
if (RSHAPE(target_shape_id)->next_field_index > current_shape->next_field_index) {
1780+
set_shape_id_func(obj, target_shape_id, data);
17791781
}
17801782

17811783
VALUE *table = shape_fields_func(obj, data);
@@ -1787,7 +1789,7 @@ struct gen_fields_lookup_ensure_size {
17871789
VALUE obj;
17881790
ID id;
17891791
struct gen_fields_tbl *fields_tbl;
1790-
rb_shape_t *shape;
1792+
shape_id_t shape_id;
17911793
bool resize;
17921794
};
17931795

@@ -1801,25 +1803,25 @@ generic_fields_lookup_ensure_size(st_data_t *k, st_data_t *v, st_data_t u, int e
18011803

18021804
if (!existing || fields_lookup->resize) {
18031805
if (existing) {
1804-
RUBY_ASSERT(fields_lookup->shape->type == SHAPE_IVAR || fields_lookup->shape->type == SHAPE_OBJ_ID);
1805-
RUBY_ASSERT(RSHAPE(fields_lookup->shape->parent_id)->capacity < fields_lookup->shape->capacity);
1806+
RUBY_ASSERT(RSHAPE(fields_lookup->shape_id)->type == SHAPE_IVAR || RSHAPE(fields_lookup->shape_id)->type == SHAPE_OBJ_ID);
1807+
RUBY_ASSERT(RSHAPE(RSHAPE(fields_lookup->shape_id)->parent_id)->capacity < RSHAPE(fields_lookup->shape_id)->capacity);
18061808
}
18071809
else {
18081810
FL_SET_RAW((VALUE)*k, FL_EXIVAR);
18091811
}
18101812

1811-
fields_tbl = gen_fields_tbl_resize(fields_tbl, fields_lookup->shape->capacity);
1813+
fields_tbl = gen_fields_tbl_resize(fields_tbl, RSHAPE(fields_lookup->shape_id)->capacity);
18121814
*v = (st_data_t)fields_tbl;
18131815
}
18141816

18151817
RUBY_ASSERT(FL_TEST((VALUE)*k, FL_EXIVAR));
18161818

18171819
fields_lookup->fields_tbl = fields_tbl;
1818-
if (fields_lookup->shape) {
1820+
if (fields_lookup->shape_id) {
18191821
#if SHAPE_IN_BASIC_FLAGS
1820-
rb_shape_set_shape(fields_lookup->obj, fields_lookup->shape);
1822+
rb_shape_set_shape_id(fields_lookup->obj, fields_lookup->shape_id);
18211823
#else
1822-
fields_tbl->shape_id = rb_shape_id(fields_lookup->shape);
1824+
fields_tbl->shape_id = fields_lookup->shape_id;
18231825
#endif
18241826
}
18251827

@@ -1853,11 +1855,11 @@ generic_ivar_set_shape_resize_fields(VALUE obj, attr_index_t _old_capa, attr_ind
18531855
}
18541856

18551857
static void
1856-
generic_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *data)
1858+
generic_ivar_set_set_shape_id(VALUE obj, shape_id_t shape_id, void *data)
18571859
{
18581860
struct gen_fields_lookup_ensure_size *fields_lookup = data;
18591861

1860-
fields_lookup->shape = shape;
1862+
fields_lookup->shape_id = shape_id;
18611863
}
18621864

18631865
static void
@@ -1901,30 +1903,28 @@ generic_ivar_set(VALUE obj, ID id, VALUE val)
19011903
.obj = obj,
19021904
.id = id,
19031905
.resize = false,
1904-
.shape = NULL,
19051906
};
19061907

19071908
general_ivar_set(obj, id, val, &fields_lookup,
19081909
generic_ivar_set_shape_fields,
19091910
generic_ivar_set_shape_resize_fields,
1910-
generic_ivar_set_set_shape,
1911+
generic_ivar_set_set_shape_id,
19111912
generic_ivar_set_transition_too_complex,
19121913
generic_ivar_set_too_complex_table);
19131914
}
19141915

19151916
static void
1916-
generic_field_set(VALUE obj, rb_shape_t *target_shape, VALUE val)
1917+
generic_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val)
19171918
{
19181919
struct gen_fields_lookup_ensure_size fields_lookup = {
19191920
.obj = obj,
19201921
.resize = false,
1921-
.shape = NULL,
19221922
};
19231923

1924-
general_field_set(obj, target_shape, val, &fields_lookup,
1924+
general_field_set(obj, target_shape_id, val, &fields_lookup,
19251925
generic_ivar_set_shape_fields,
19261926
generic_ivar_set_shape_resize_fields,
1927-
generic_ivar_set_set_shape,
1927+
generic_ivar_set_set_shape_id,
19281928
generic_ivar_set_transition_too_complex,
19291929
generic_ivar_set_too_complex_table);
19301930
}
@@ -1982,9 +1982,9 @@ obj_ivar_set_shape_resize_fields(VALUE obj, attr_index_t old_capa, attr_index_t
19821982
}
19831983

19841984
static void
1985-
obj_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *_data)
1985+
obj_ivar_set_set_shape_id(VALUE obj, shape_id_t shape_id, void *_data)
19861986
{
1987-
rb_shape_set_shape(obj, shape);
1987+
rb_shape_set_shape_id(obj, shape_id);
19881988
}
19891989

19901990
static void
@@ -2007,18 +2007,18 @@ rb_obj_ivar_set(VALUE obj, ID id, VALUE val)
20072007
return general_ivar_set(obj, id, val, NULL,
20082008
obj_ivar_set_shape_fields,
20092009
obj_ivar_set_shape_resize_fields,
2010-
obj_ivar_set_set_shape,
2010+
obj_ivar_set_set_shape_id,
20112011
obj_ivar_set_transition_too_complex,
20122012
obj_ivar_set_too_complex_table).index;
20132013
}
20142014

20152015
static void
2016-
obj_field_set(VALUE obj, rb_shape_t *target_shape, VALUE val)
2016+
obj_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val)
20172017
{
2018-
general_field_set(obj, target_shape, val, NULL,
2018+
general_field_set(obj, target_shape_id, val, NULL,
20192019
obj_ivar_set_shape_fields,
20202020
obj_ivar_set_shape_resize_fields,
2021-
obj_ivar_set_set_shape,
2021+
obj_ivar_set_set_shape_id,
20222022
obj_ivar_set_transition_too_complex,
20232023
obj_ivar_set_too_complex_table);
20242024
}
@@ -2138,22 +2138,22 @@ rb_ivar_set_internal(VALUE obj, ID id, VALUE val)
21382138
ivar_set(obj, id, val);
21392139
}
21402140

2141-
static void class_field_set(VALUE obj, rb_shape_t *target_shape, VALUE val);
2141+
static void class_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val);
21422142

21432143
void
2144-
rb_obj_field_set(VALUE obj, rb_shape_t *target_shape, VALUE val)
2144+
rb_obj_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val)
21452145
{
21462146
switch (BUILTIN_TYPE(obj)) {
21472147
case T_OBJECT:
2148-
obj_field_set(obj, target_shape, val);
2148+
obj_field_set(obj, target_shape_id, val);
21492149
break;
21502150
case T_CLASS:
21512151
case T_MODULE:
21522152
ASSERT_vm_locking();
2153-
class_field_set(obj, target_shape, val);
2153+
class_field_set(obj, target_shape_id, val);
21542154
break;
21552155
default:
2156-
generic_field_set(obj, target_shape, val);
2156+
generic_field_set(obj, target_shape_id, val);
21572157
break;
21582158
}
21592159
}
@@ -4739,9 +4739,9 @@ class_ivar_set_shape_resize_fields(VALUE obj, attr_index_t _old_capa, attr_index
47394739
}
47404740

47414741
static void
4742-
class_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *_data)
4742+
class_ivar_set_set_shape_id(VALUE obj, shape_id_t shape_id, void *_data)
47434743
{
4744-
rb_shape_set_shape(obj, shape);
4744+
rb_shape_set_shape_id(obj, shape_id);
47454745
}
47464746

47474747
static void
@@ -4772,7 +4772,7 @@ rb_class_ivar_set(VALUE obj, ID id, VALUE val)
47724772
existing = general_ivar_set(obj, id, val, NULL,
47734773
class_ivar_set_shape_fields,
47744774
class_ivar_set_shape_resize_fields,
4775-
class_ivar_set_set_shape,
4775+
class_ivar_set_set_shape_id,
47764776
class_ivar_set_transition_too_complex,
47774777
class_ivar_set_too_complex_table).existing;
47784778
}
@@ -4782,13 +4782,13 @@ rb_class_ivar_set(VALUE obj, ID id, VALUE val)
47824782
}
47834783

47844784
static void
4785-
class_field_set(VALUE obj, rb_shape_t *target_shape, VALUE val)
4785+
class_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val)
47864786
{
47874787
RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE));
4788-
general_field_set(obj, target_shape, val, NULL,
4788+
general_field_set(obj, target_shape_id, val, NULL,
47894789
class_ivar_set_shape_fields,
47904790
class_ivar_set_shape_resize_fields,
4791-
class_ivar_set_set_shape,
4791+
class_ivar_set_set_shape_id,
47924792
class_ivar_set_transition_too_complex,
47934793
class_ivar_set_too_complex_table);
47944794
}

0 commit comments

Comments
 (0)