Skip to content

Commit a13de46

Browse files
committed
Extract rb_imemo_fields_clone2
1 parent 23a70aa commit a13de46

3 files changed

Lines changed: 31 additions & 46 deletions

File tree

imemo.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -147,56 +147,46 @@ rb_imemo_fields_new_complex(VALUE klass, size_t capa)
147147
return imemo_fields_new_complex(klass, capa);
148148
}
149149

150-
static int
151-
imemo_fields_trigger_wb_i(st_data_t key, st_data_t value, st_data_t arg)
152-
{
153-
VALUE field_obj = (VALUE)arg;
154-
RB_OBJ_WRITTEN(field_obj, Qundef, (VALUE)value);
155-
return ST_CONTINUE;
156-
}
157-
158-
static int
159-
imemo_fields_complex_wb_i(st_data_t key, st_data_t value, st_data_t arg)
160-
{
161-
RB_OBJ_WRITTEN((VALUE)arg, Qundef, (VALUE)value);
162-
return ST_CONTINUE;
163-
}
164-
165150
VALUE
166151
rb_imemo_fields_new_complex_tbl(VALUE klass, st_table *tbl)
167152
{
168153
VALUE fields = imemo_fields_new(klass, sizeof(struct rb_fields));
169154
IMEMO_OBJ_FIELDS(fields)->as.complex.table = tbl;
170-
st_foreach(tbl, imemo_fields_trigger_wb_i, (st_data_t)fields);
155+
rb_gc_writebarrier_remember(fields);
171156
return fields;
172157
}
173158

159+
VALUE
160+
rb_imemo_fields_clone2(VALUE fields_obj, size_t capa)
161+
{
162+
shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
163+
RUBY_ASSERT(!rb_shape_too_complex_p(shape_id));
164+
165+
VALUE clone = imemo_fields_new(CLASS_OF(fields_obj), capa);
166+
VALUE *fields = rb_imemo_fields_ptr(clone);
167+
attr_index_t fields_count = RSHAPE_LEN(shape_id);
168+
MEMCPY(fields, rb_imemo_fields_ptr(fields_obj), VALUE, fields_count);
169+
RBASIC_SET_SHAPE_ID(clone, shape_id);
170+
rb_gc_writebarrier_remember(clone);
171+
172+
return clone;
173+
}
174+
174175
VALUE
175176
rb_imemo_fields_clone(VALUE fields_obj)
176177
{
177178
shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
178-
VALUE clone;
179179

180180
if (rb_shape_too_complex_p(shape_id)) {
181-
clone = rb_imemo_fields_new_complex(CLASS_OF(fields_obj), 0);
181+
VALUE clone = rb_imemo_fields_new_complex(CLASS_OF(fields_obj), 0);
182182
RBASIC_SET_SHAPE_ID(clone, shape_id);
183183
st_table *src_table = rb_imemo_fields_complex_tbl(fields_obj);
184184
st_table *dest_table = rb_imemo_fields_complex_tbl(clone);
185185
st_replace(dest_table, src_table);
186-
st_foreach(dest_table, imemo_fields_complex_wb_i, (st_data_t)clone);
187-
}
188-
else {
189-
clone = imemo_fields_new(CLASS_OF(fields_obj), RSHAPE_CAPACITY(shape_id));
190-
RBASIC_SET_SHAPE_ID(clone, shape_id);
191-
VALUE *fields = rb_imemo_fields_ptr(clone);
192-
attr_index_t fields_count = RSHAPE_LEN(shape_id);
193-
MEMCPY(fields, rb_imemo_fields_ptr(fields_obj), VALUE, fields_count);
194-
for (attr_index_t i = 0; i < fields_count; i++) {
195-
RB_OBJ_WRITTEN(clone, Qundef, fields[i]);
196-
}
186+
rb_gc_writebarrier_remember(clone);
187+
return clone;
197188
}
198-
199-
return clone;
189+
return rb_imemo_fields_clone2(fields_obj, RSHAPE_CAPACITY(shape_id));
200190
}
201191

202192
void

internal/imemo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ VALUE rb_imemo_fields_new(VALUE klass, size_t capa);
282282
VALUE rb_imemo_fields_new_complex(VALUE klass, size_t capa);
283283
VALUE rb_imemo_fields_new_complex_tbl(VALUE klass, st_table *tbl);
284284
VALUE rb_imemo_fields_clone(VALUE fields_obj);
285+
VALUE rb_imemo_fields_clone2(VALUE fields_obj, size_t capa);
285286
void rb_imemo_fields_clear(VALUE fields_obj);
286287

287288
static inline VALUE *

variable.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,15 +1839,11 @@ generic_ivar_set(VALUE obj, ID id, VALUE val)
18391839

18401840
if (next_capacity != current_capacity) {
18411841
RUBY_ASSERT(next_capacity > current_capacity);
1842-
1843-
fields_obj = rb_imemo_fields_new(rb_obj_class(obj), next_capacity);
18441842
if (original_fields_obj) {
1845-
attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
1846-
VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1847-
MEMCPY(fields, rb_imemo_fields_ptr(original_fields_obj), VALUE, fields_count);
1848-
for (attr_index_t i = 0; i < fields_count; i++) {
1849-
RB_OBJ_WRITTEN(fields_obj, Qundef, fields[i]);
1850-
}
1843+
fields_obj = rb_imemo_fields_clone2(original_fields_obj, next_capacity);
1844+
}
1845+
else {
1846+
fields_obj = rb_imemo_fields_new(rb_obj_class(obj), next_capacity);
18511847
}
18521848
}
18531849

@@ -1922,14 +1918,12 @@ generic_field_set(VALUE obj, shape_id_t target_shape_id, VALUE val)
19221918
else {
19231919
attr_index_t index = RSHAPE_INDEX(target_shape_id);
19241920
if (index >= RSHAPE_CAPACITY(current_shape_id)) {
1925-
fields_obj = rb_imemo_fields_new(rb_obj_class(obj), index);
1921+
attr_index_t next_capacity = RSHAPE_CAPACITY(target_shape_id);
19261922
if (original_fields_obj) {
1927-
attr_index_t fields_count = RSHAPE_LEN(current_shape_id);
1928-
VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1929-
MEMCPY(fields, rb_imemo_fields_ptr(original_fields_obj), VALUE, fields_count);
1930-
for (attr_index_t i = 0; i < fields_count; i++) {
1931-
RB_OBJ_WRITTEN(fields_obj, Qundef, fields[i]);
1932-
}
1923+
fields_obj = rb_imemo_fields_clone2(original_fields_obj, next_capacity);
1924+
}
1925+
else {
1926+
fields_obj = rb_imemo_fields_new(rb_obj_class(obj), next_capacity);
19331927
}
19341928
}
19351929

0 commit comments

Comments
 (0)