Skip to content

Commit d8beeb7

Browse files
committed
Refactor class_fields_ivar_set to use generic_shape_ivar
1 parent 980ebb0 commit d8beeb7

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

variable.c

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4535,43 +4535,30 @@ rb_iv_set(VALUE obj, const char *name, VALUE val)
45354535
static bool
45364536
class_fields_ivar_set(VALUE klass, VALUE fields_obj, ID id, VALUE val, bool concurrent, VALUE *new_fields_obj)
45374537
{
4538-
bool existing = true;
45394538
const VALUE original_fields_obj = fields_obj;
45404539
fields_obj = original_fields_obj ? original_fields_obj : rb_imemo_fields_new(klass, 1);
45414540

45424541
shape_id_t current_shape_id = RBASIC_SHAPE_ID(fields_obj);
4543-
shape_id_t next_shape_id = current_shape_id;
45444542

45454543
if (UNLIKELY(rb_shape_too_complex_p(current_shape_id))) {
45464544
goto too_complex;
45474545
}
45484546

4549-
attr_index_t index;
4550-
if (!rb_shape_get_iv_index(current_shape_id, id, &index)) {
4551-
existing = false;
4552-
4553-
index = RSHAPE_LEN(current_shape_id);
4554-
if (index >= SHAPE_MAX_FIELDS) {
4555-
rb_raise(rb_eArgError, "too many instance variables");
4556-
}
4557-
4558-
next_shape_id = rb_shape_transition_add_ivar(fields_obj, id);
4559-
if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
4560-
fields_obj = imemo_fields_complex_from_obj(klass, fields_obj, next_shape_id);
4561-
goto too_complex;
4562-
}
4547+
bool new_ivar;
4548+
shape_id_t next_shape_id = generic_shape_ivar(fields_obj, id, &new_ivar);
45634549

4564-
attr_index_t next_capacity = RSHAPE_CAPACITY(next_shape_id);
4565-
attr_index_t current_capacity = RSHAPE_CAPACITY(current_shape_id);
4550+
if (UNLIKELY(rb_shape_too_complex_p(next_shape_id))) {
4551+
fields_obj = imemo_fields_complex_from_obj(klass, fields_obj, next_shape_id);
4552+
goto too_complex;
4553+
}
45664554

4567-
if (next_capacity > current_capacity) {
4555+
attr_index_t index = RSHAPE_INDEX(next_shape_id);
4556+
if (new_ivar) {
4557+
if (index >= RSHAPE_CAPACITY(current_shape_id)) {
45684558
// We allocate a new fields_obj even when concurrency isn't a concern
45694559
// so that we're embedded as long as possible.
4570-
fields_obj = imemo_fields_copy_capa(klass, fields_obj, next_capacity);
4560+
fields_obj = imemo_fields_copy_capa(klass, fields_obj, RSHAPE_CAPACITY(next_shape_id));
45714561
}
4572-
4573-
RUBY_ASSERT(RSHAPE(next_shape_id)->type == SHAPE_IVAR);
4574-
RUBY_ASSERT(index == (RSHAPE_LEN(next_shape_id) - 1));
45754562
}
45764563

45774564
VALUE *fields = rb_imemo_fields_ptr(fields_obj);
@@ -4588,12 +4575,12 @@ class_fields_ivar_set(VALUE klass, VALUE fields_obj, ID id, VALUE val, bool conc
45884575
RB_OBJ_WRITE(fields_obj, &fields[index], val);
45894576
}
45904577

4591-
if (!existing) {
4578+
if (new_ivar) {
45924579
RBASIC_SET_SHAPE_ID(fields_obj, next_shape_id);
45934580
}
45944581

45954582
*new_fields_obj = fields_obj;
4596-
return existing;
4583+
return new_ivar;
45974584

45984585
too_complex:
45994586
{
@@ -4605,7 +4592,7 @@ class_fields_ivar_set(VALUE klass, VALUE fields_obj, ID id, VALUE val, bool conc
46054592
}
46064593

46074594
st_table *table = rb_imemo_fields_complex_tbl(fields_obj);
4608-
existing = st_insert(table, (st_data_t)id, (st_data_t)val);
4595+
new_ivar = !st_insert(table, (st_data_t)id, (st_data_t)val);
46094596
RB_OBJ_WRITTEN(fields_obj, Qundef, val);
46104597

46114598
if (fields_obj != original_fields_obj) {
@@ -4614,7 +4601,7 @@ class_fields_ivar_set(VALUE klass, VALUE fields_obj, ID id, VALUE val, bool conc
46144601
}
46154602

46164603
*new_fields_obj = fields_obj;
4617-
return existing;
4604+
return new_ivar;
46184605
}
46194606

46204607
bool
@@ -4628,7 +4615,7 @@ rb_class_ivar_set(VALUE obj, ID id, VALUE val)
46284615
const VALUE original_fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
46294616
VALUE new_fields_obj = 0;
46304617

4631-
bool existing = class_fields_ivar_set(obj, original_fields_obj, id, val, rb_multi_ractor_p(), &new_fields_obj);
4618+
bool new_ivar = class_fields_ivar_set(obj, original_fields_obj, id, val, rb_multi_ractor_p(), &new_fields_obj);
46324619

46334620
if (new_fields_obj != original_fields_obj) {
46344621
RCLASS_WRITABLE_SET_FIELDS_OBJ(obj, new_fields_obj);
@@ -4640,7 +4627,7 @@ rb_class_ivar_set(VALUE obj, ID id, VALUE val)
46404627
// Perhaps INVALID_SHAPE_ID?
46414628
RBASIC_SET_SHAPE_ID(obj, RBASIC_SHAPE_ID(new_fields_obj));
46424629

4643-
return !existing;
4630+
return new_ivar;
46444631
}
46454632

46464633
void

0 commit comments

Comments
 (0)