Skip to content

Commit 81b35fe

Browse files
committed
rb_evict_ivars_to_hash: get rid of the sahpe paramater
It's only used to allocate the table with the right size, but in some case we were passing `rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX)` which `next_iv_index` is a bit undefined. So overall we're better to just allocate a table the size of the existing object, it should be close enough in the vast majority of cases, and that's already a de-optimizaton path anyway.
1 parent b92a92a commit 81b35fe

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

internal/variable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ VALUE rb_mod_set_temporary_name(VALUE, VALUE);
4848
struct gen_ivtbl;
4949
int rb_gen_ivtbl_get(VALUE obj, ID id, struct gen_ivtbl **ivtbl);
5050
int rb_obj_evacuate_ivs_to_hash_table(ID key, VALUE val, st_data_t arg);
51-
void rb_evict_ivars_to_hash(VALUE obj, rb_shape_t * shape);
51+
void rb_evict_ivars_to_hash(VALUE obj);
5252

5353
RUBY_SYMBOL_EXPORT_BEGIN
5454
/* variable.c (export) */

object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ mutable_obj_clone(VALUE obj, VALUE kwfreeze)
474474
if (RB_OBJ_FROZEN(obj)) {
475475
rb_shape_t * next_shape = rb_shape_transition_shape_frozen(clone);
476476
if (!rb_shape_obj_too_complex(clone) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
477-
rb_evict_ivars_to_hash(clone, rb_shape_get_shape(clone));
477+
rb_evict_ivars_to_hash(clone);
478478
}
479479
else {
480480
rb_shape_set_shape(clone, next_shape);
@@ -498,7 +498,7 @@ mutable_obj_clone(VALUE obj, VALUE kwfreeze)
498498
// If we're out of shapes, but we want to freeze, then we need to
499499
// evacuate this clone to a hash
500500
if (!rb_shape_obj_too_complex(clone) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
501-
rb_evict_ivars_to_hash(clone, rb_shape_get_shape(clone));
501+
rb_evict_ivars_to_hash(clone);
502502
}
503503
else {
504504
rb_shape_set_shape(clone, next_shape);

variable.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
13311331

13321332
if (!rb_shape_transition_shape_remove_ivar(obj, id, shape, &val)) {
13331333
if (!rb_shape_obj_too_complex(obj)) {
1334-
rb_evict_ivars_to_hash(obj, shape);
1334+
rb_evict_ivars_to_hash(obj);
13351335
}
13361336

13371337
st_table *table = NULL;
@@ -1371,11 +1371,11 @@ rb_attr_delete(VALUE obj, ID id)
13711371
}
13721372

13731373
void
1374-
rb_evict_ivars_to_hash(VALUE obj, rb_shape_t * shape)
1374+
rb_evict_ivars_to_hash(VALUE obj)
13751375
{
13761376
RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
13771377

1378-
st_table *table = st_init_numtable_with_size(shape->next_iv_index);
1378+
st_table *table = st_init_numtable_with_size(rb_ivar_count(obj));
13791379

13801380
// Evacuate all previous values from shape into id_table
13811381
rb_ivar_foreach(obj, rb_obj_evacuate_ivs_to_hash_table, (st_data_t)table);
@@ -1570,7 +1570,7 @@ generic_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *data)
15701570
static void
15711571
generic_ivar_set_transition_too_complex(VALUE obj, void *_data)
15721572
{
1573-
rb_evict_ivars_to_hash(obj, rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX));
1573+
rb_evict_ivars_to_hash(obj);
15741574
FL_SET_RAW(obj, FL_EXIVAR);
15751575
}
15761576

@@ -1666,7 +1666,7 @@ obj_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *_data)
16661666
static void
16671667
obj_ivar_set_transition_too_complex(VALUE obj, void *_data)
16681668
{
1669-
rb_evict_ivars_to_hash(obj, rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX));
1669+
rb_evict_ivars_to_hash(obj);
16701670
}
16711671

16721672
static st_table *
@@ -1756,7 +1756,7 @@ void rb_obj_freeze_inline(VALUE x)
17561756
// If we're transitioning from "not complex" to "too complex"
17571757
// then evict ivars. This can happen if we run out of shapes
17581758
if (!rb_shape_obj_too_complex(x) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
1759-
rb_evict_ivars_to_hash(x, rb_shape_get_shape(x));
1759+
rb_evict_ivars_to_hash(x);
17601760
}
17611761
rb_shape_set_shape(x, next_shape);
17621762

@@ -4185,7 +4185,7 @@ class_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *_data)
41854185
static void
41864186
class_ivar_set_transition_too_complex(VALUE obj, void *_data)
41874187
{
4188-
rb_evict_ivars_to_hash(obj, rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX));
4188+
rb_evict_ivars_to_hash(obj);
41894189
}
41904190

41914191
static st_table *

0 commit comments

Comments
 (0)