Skip to content

Commit a80a500

Browse files
committed
Refactor rb_obj_shape out.
It still exists but only in `shape.c`.
1 parent 97f44ac commit a80a500

File tree

5 files changed

+65
-36
lines changed

5 files changed

+65
-36
lines changed

gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ object_id0(VALUE obj)
18941894
{
18951895
VALUE id = Qfalse;
18961896

1897-
if (rb_shape_has_object_id(rb_obj_shape(obj))) {
1897+
if (rb_shape_id_has_object_id(RBASIC_SHAPE_ID(obj))) {
18981898
shape_id_t object_id_shape_id = rb_shape_transition_object_id(obj);
18991899
id = rb_obj_field_get(obj, object_id_shape_id);
19001900
RUBY_ASSERT(id, "object_id missing");

object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ rb_class_allocate_instance(VALUE klass)
128128
T_OBJECT | ROBJECT_EMBED | (RGENGC_WB_PROTECTED_OBJECT ? FL_WB_PROTECTED : 0), size, 0);
129129
VALUE obj = (VALUE)o;
130130

131-
RUBY_ASSERT(rb_obj_shape(obj)->type == SHAPE_ROOT);
131+
RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(obj), SHAPE_ROOT));
132132

133133
RBASIC_SET_SHAPE_ID(obj, rb_shape_root(rb_gc_heap_id_for_size(size)));
134134

shape.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ rb_shape_depth(shape_id_t shape_id)
372372
return depth;
373373
}
374374

375+
static inline rb_shape_t *
376+
obj_shape(VALUE obj)
377+
{
378+
return RSHAPE(rb_obj_shape_id(obj));
379+
}
380+
375381
static rb_shape_t *
376382
shape_alloc(void)
377383
{
@@ -676,7 +682,7 @@ shape_transition_too_complex(rb_shape_t *original_shape)
676682
shape_id_t
677683
rb_shape_transition_complex(VALUE obj)
678684
{
679-
rb_shape_t *original_shape = rb_obj_shape(obj);
685+
rb_shape_t *original_shape = obj_shape(obj);
680686
return rb_shape_id(shape_transition_too_complex(original_shape));
681687
}
682688

@@ -695,7 +701,7 @@ rb_shape_id_has_object_id(shape_id_t shape_id)
695701
shape_id_t
696702
rb_shape_transition_object_id(VALUE obj)
697703
{
698-
rb_shape_t* shape = rb_obj_shape(obj);
704+
rb_shape_t* shape = obj_shape(obj);
699705
RUBY_ASSERT(shape);
700706

701707
if (shape->flags & SHAPE_FL_HAS_OBJECT_ID) {
@@ -817,13 +823,13 @@ shape_get_next(rb_shape_t *shape, VALUE obj, ID id, bool emit_warnings)
817823
shape_id_t
818824
rb_shape_transition_add_ivar(VALUE obj, ID id)
819825
{
820-
return rb_shape_id(shape_get_next(rb_obj_shape(obj), obj, id, true));
826+
return rb_shape_id(shape_get_next(obj_shape(obj), obj, id, true));
821827
}
822828

823829
shape_id_t
824830
rb_shape_transition_add_ivar_no_warnings(VALUE obj, ID id)
825831
{
826-
return rb_shape_id(shape_get_next(rb_obj_shape(obj), obj, id, false));
832+
return rb_shape_id(shape_get_next(obj_shape(obj), obj, id, false));
827833
}
828834

829835
// Same as rb_shape_get_iv_index, but uses a provided valid shape id and index
@@ -1085,7 +1091,7 @@ rb_shape_copy_complex_ivars(VALUE dest, VALUE obj, shape_id_t src_shape_id, st_t
10851091
RUBY_FUNC_EXPORTED bool
10861092
rb_shape_obj_too_complex_p(VALUE obj)
10871093
{
1088-
return rb_shape_too_complex_p(rb_obj_shape(obj));
1094+
return rb_shape_too_complex_p(obj_shape(obj));
10891095
}
10901096

10911097
bool
@@ -1248,7 +1254,7 @@ rb_shape_parent(VALUE self)
12481254
static VALUE
12491255
rb_shape_debug_shape(VALUE self, VALUE obj)
12501256
{
1251-
return rb_shape_t_to_rb_cShape(rb_obj_shape(obj));
1257+
return rb_shape_t_to_rb_cShape(obj_shape(obj));
12521258
}
12531259

12541260
static VALUE

shape.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,6 @@ shape_id_t rb_shape_rebuild(shape_id_t initial_shape_id, shape_id_t dest_shape_i
143143
void rb_shape_copy_fields(VALUE dest, VALUE *dest_buf, shape_id_t dest_shape_id, VALUE src, VALUE *src_buf, shape_id_t src_shape_id);
144144
void rb_shape_copy_complex_ivars(VALUE dest, VALUE obj, shape_id_t src_shape_id, st_table *fields_table);
145145

146-
static inline rb_shape_t *
147-
rb_obj_shape(VALUE obj)
148-
{
149-
return RSHAPE(rb_obj_shape_id(obj));
150-
}
151-
152146
static inline bool
153147
rb_shape_id_canonical_p(shape_id_t shape_id)
154148
{
@@ -161,6 +155,36 @@ rb_shape_root(size_t heap_id)
161155
return (shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID);
162156
}
163157

158+
static inline bool
159+
RSHAPE_TYPE_P(shape_id_t shape_id, enum shape_type type)
160+
{
161+
return RSHAPE(shape_id)->type == type;
162+
}
163+
164+
static inline attr_index_t
165+
RSHAPE_CAPACITY(shape_id_t shape_id)
166+
{
167+
return RSHAPE(shape_id)->capacity;
168+
}
169+
170+
static inline attr_index_t
171+
RSHAPE_LEN(shape_id_t shape_id)
172+
{
173+
return RSHAPE(shape_id)->next_field_index;
174+
}
175+
176+
static inline attr_index_t
177+
RSHAPE_INDEX(shape_id_t shape_id)
178+
{
179+
return RSHAPE_LEN(shape_id) - 1;
180+
}
181+
182+
static inline ID
183+
RSHAPE_EDGE_NAME(shape_id_t shape_id)
184+
{
185+
return RSHAPE(shape_id)->edge_name;
186+
}
187+
164188
static inline uint32_t
165189
ROBJECT_FIELDS_CAPACITY(VALUE obj)
166190
{
@@ -213,7 +237,7 @@ bool rb_shape_set_shape_id(VALUE obj, shape_id_t shape_id);
213237
static inline bool
214238
rb_shape_obj_has_id(VALUE obj)
215239
{
216-
return rb_shape_has_object_id(rb_obj_shape(obj));
240+
return rb_shape_id_has_object_id(RBASIC_SHAPE_ID(obj));
217241
}
218242

219243
// For ext/objspace

variable.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ rb_obj_init_too_complex(VALUE obj, st_table *table)
16601660
// This method is meant to be called on newly allocated object.
16611661
RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
16621662
RUBY_ASSERT(rb_shape_id_canonical_p(RBASIC_SHAPE_ID(obj)));
1663-
RUBY_ASSERT(rb_obj_shape(obj)->next_field_index == 0);
1663+
RUBY_ASSERT(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)) == 0);
16641664

16651665
obj_transition_too_complex(obj, table);
16661666
}
@@ -1673,8 +1673,7 @@ rb_evict_fields_to_hash(VALUE obj)
16731673

16741674
RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
16751675

1676-
rb_shape_t *shape = rb_obj_shape(obj);
1677-
st_table *table = st_init_numtable_with_size(shape->next_field_index);
1676+
st_table *table = st_init_numtable_with_size(RSHAPE_LEN(RBASIC_SHAPE_ID(obj)));
16781677
rb_obj_copy_fields_to_hash_table(obj, table);
16791678
obj_transition_too_complex(obj, table);
16801679

@@ -1771,28 +1770,28 @@ general_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val, void *data,
17711770
void (*transition_too_complex_func)(VALUE, void *),
17721771
st_table *(*too_complex_table_func)(VALUE, void *))
17731772
{
1774-
rb_shape_t *current_shape = rb_obj_shape(obj);
1773+
shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
17751774

17761775
if (UNLIKELY(rb_shape_id_too_complex_p(target_shape_id))) {
1777-
if (UNLIKELY(!rb_shape_too_complex_p(current_shape))) {
1776+
if (UNLIKELY(!rb_shape_id_too_complex_p(current_shape_id))) {
17781777
transition_too_complex_func(obj, data);
17791778
}
17801779

17811780
st_table *table = too_complex_table_func(obj, data);
1782-
if (RSHAPE(target_shape_id)->next_field_index > current_shape->next_field_index) {
1781+
if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
17831782
set_shape_id_func(obj, target_shape_id, data);
17841783
}
17851784

1786-
st_insert(table, (st_data_t)RSHAPE(target_shape_id)->edge_name, (st_data_t)val);
1785+
st_insert(table, (st_data_t)RSHAPE_EDGE_NAME(target_shape_id), (st_data_t)val);
17871786
RB_OBJ_WRITTEN(obj, Qundef, val);
17881787
}
17891788
else {
1790-
attr_index_t index = RSHAPE(target_shape_id)->next_field_index - 1;
1791-
if (index >= current_shape->capacity) {
1792-
shape_resize_fields_func(obj, current_shape->capacity, RSHAPE(target_shape_id)->capacity, data);
1789+
attr_index_t index = RSHAPE_INDEX(target_shape_id);
1790+
if (index >= RSHAPE_CAPACITY(current_shape_id)) {
1791+
shape_resize_fields_func(obj, RSHAPE_CAPACITY(current_shape_id), RSHAPE_CAPACITY(target_shape_id), data);
17931792
}
17941793

1795-
if (RSHAPE(target_shape_id)->next_field_index > current_shape->next_field_index) {
1794+
if (RSHAPE_LEN(target_shape_id) > RSHAPE_LEN(current_shape_id)) {
17961795
set_shape_id_func(obj, target_shape_id, data);
17971796
}
17981797

@@ -2257,19 +2256,18 @@ obj_fields_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, b
22572256
.ivar_only = ivar_only,
22582257
};
22592258

2260-
rb_shape_t *shape = rb_obj_shape(obj);
2261-
if (rb_shape_too_complex_p(shape)) {
2259+
shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2260+
if (rb_shape_id_too_complex_p(shape_id)) {
22622261
rb_st_foreach(ROBJECT_FIELDS_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
22632262
}
22642263
else {
2265-
iterate_over_shapes_with_callback(shape, func, &itr_data);
2264+
iterate_over_shapes_with_callback(RSHAPE(shape_id), func, &itr_data);
22662265
}
22672266
}
22682267

22692268
static void
22702269
gen_fields_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, bool ivar_only)
22712270
{
2272-
rb_shape_t *shape = rb_obj_shape(obj);
22732271
struct gen_fields_tbl *fields_tbl;
22742272
if (!rb_gen_fields_tbl_get(obj, 0, &fields_tbl)) return;
22752273

@@ -2281,11 +2279,12 @@ gen_fields_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg, b
22812279
.ivar_only = ivar_only,
22822280
};
22832281

2284-
if (rb_shape_obj_too_complex_p(obj)) {
2282+
shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2283+
if (rb_shape_id_too_complex_p(shape_id)) {
22852284
rb_st_foreach(fields_tbl->as.complex.table, each_hash_iv, (st_data_t)&itr_data);
22862285
}
22872286
else {
2288-
iterate_over_shapes_with_callback(shape, func, &itr_data);
2287+
iterate_over_shapes_with_callback(RSHAPE(shape_id), func, &itr_data);
22892288
}
22902289
}
22912290

@@ -2294,19 +2293,19 @@ class_fields_each(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
22942293
{
22952294
RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE));
22962295

2297-
rb_shape_t *shape = rb_obj_shape(obj);
22982296
struct iv_itr_data itr_data = {
22992297
.obj = obj,
23002298
.arg = arg,
23012299
.func = func,
23022300
.ivar_only = ivar_only,
23032301
};
23042302

2305-
if (rb_shape_obj_too_complex_p(obj)) {
2303+
shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2304+
if (rb_shape_id_too_complex_p(shape_id)) {
23062305
rb_st_foreach(RCLASS_WRITABLE_FIELDS_HASH(obj), each_hash_iv, (st_data_t)&itr_data);
23072306
}
23082307
else {
2309-
iterate_over_shapes_with_callback(shape, func, &itr_data);
2308+
iterate_over_shapes_with_callback(RSHAPE(shape_id), func, &itr_data);
23102309
}
23112310
}
23122311

@@ -4738,7 +4737,7 @@ rb_fields_tbl_copy(VALUE dst, VALUE src)
47384737
RUBY_ASSERT(rb_type(dst) == rb_type(src));
47394738
RUBY_ASSERT(RB_TYPE_P(dst, T_CLASS) || RB_TYPE_P(dst, T_MODULE));
47404739

4741-
RUBY_ASSERT(rb_obj_shape(dst)->type == SHAPE_ROOT);
4740+
RUBY_ASSERT(RSHAPE_TYPE_P(RBASIC_SHAPE_ID(dst), SHAPE_ROOT));
47424741
RUBY_ASSERT(!RCLASS_PRIME_FIELDS(dst));
47434742

47444743
rb_ivar_foreach(src, tbl_copy_i, dst);

0 commit comments

Comments
 (0)