Speed up shape transitions
This commit significantly speeds up shape transitions as it changes get_next_shape_internal to not perform a lookup (and instead require the caller to perform the lookup). This avoids double lookups during shape transitions. There is a significant (~2x) speedup in the following micro-benchmark: puts(Benchmark.measure do o = Object.new 100_000.times do |i| o.instance_variable_set(:"@a#{i}", 0) end end) Before: 22.393194 0.201639 22.594833 ( 22.684237) After: 11.323086 0.022284 11.345370 ( 11.389346)
This commit is contained in:
parent
648927d71b
commit
1f0888ab3e
Notes:
git
2022-11-21 15:22:54 +00:00
29
shape.c
29
shape.c
@ -98,36 +98,12 @@ rb_shape_get_shape(VALUE obj)
|
||||
return rb_shape_get_shape_by_id(rb_shape_get_shape_id(obj));
|
||||
}
|
||||
|
||||
static rb_shape_t *
|
||||
rb_shape_lookup_id(rb_shape_t* shape, ID id, enum shape_type shape_type)
|
||||
{
|
||||
while (shape->parent_id != INVALID_SHAPE_ID) {
|
||||
if (shape->edge_name == id) {
|
||||
// If the shape type is different, we don't
|
||||
// want this to count as a "found" ID
|
||||
if (shape_type == (enum shape_type)shape->type) {
|
||||
return shape;
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
shape = rb_shape_get_parent(shape);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static rb_shape_t*
|
||||
get_next_shape_internal(rb_shape_t * shape, ID id, enum shape_type shape_type)
|
||||
{
|
||||
rb_shape_t *res = NULL;
|
||||
RB_VM_LOCK_ENTER();
|
||||
{
|
||||
if (rb_shape_lookup_id(shape, id, shape_type)) {
|
||||
// If shape already contains the ivar that is being set, we'll return shape
|
||||
res = shape;
|
||||
}
|
||||
else {
|
||||
if (!shape->edges) {
|
||||
shape->edges = rb_id_table_create(0);
|
||||
}
|
||||
@ -167,7 +143,6 @@ get_next_shape_internal(rb_shape_t * shape, ID id, enum shape_type shape_type)
|
||||
res = new_shape;
|
||||
}
|
||||
}
|
||||
}
|
||||
RB_VM_LOCK_LEAVE();
|
||||
return res;
|
||||
}
|
||||
@ -183,10 +158,6 @@ rb_shape_transition_shape_remove_ivar(VALUE obj, ID id, rb_shape_t *shape)
|
||||
{
|
||||
rb_shape_t * next_shape = get_next_shape_internal(shape, id, SHAPE_IVAR_UNDEF);
|
||||
|
||||
if (shape == next_shape) {
|
||||
return;
|
||||
}
|
||||
|
||||
rb_shape_set_shape(obj, next_shape);
|
||||
}
|
||||
|
||||
|
25
variable.c
25
variable.c
@ -1276,28 +1276,33 @@ static void
|
||||
generic_ivar_set(VALUE obj, ID id, VALUE val)
|
||||
{
|
||||
struct ivar_update ivup;
|
||||
|
||||
attr_index_t index;
|
||||
// The returned shape will have `id` in its iv_table
|
||||
rb_shape_t * shape = rb_shape_get_next(rb_shape_get_shape(obj), obj, id);
|
||||
rb_shape_t *shape = rb_shape_get_shape(obj);
|
||||
bool found = rb_shape_get_iv_index(shape, id, &index);
|
||||
if (!found) {
|
||||
index = shape->next_iv_index;
|
||||
shape = rb_shape_get_next(shape, obj, id);
|
||||
RUBY_ASSERT(index == (shape->next_iv_index - 1));
|
||||
}
|
||||
|
||||
ivup.shape = shape;
|
||||
|
||||
RB_VM_LOCK_ENTER();
|
||||
{
|
||||
attr_index_t ent_data;
|
||||
if (rb_shape_get_iv_index(shape, id, &ent_data)) {
|
||||
ivup.iv_index = (uint32_t) ent_data;
|
||||
}
|
||||
else {
|
||||
rb_bug("unreachable. Shape was not found for id: %s", rb_id2name(id));
|
||||
}
|
||||
ivup.iv_index = (uint32_t)index;
|
||||
|
||||
st_update(generic_ivtbl(obj, id, false), (st_data_t)obj, generic_ivar_update, (st_data_t)&ivup);
|
||||
}
|
||||
RB_VM_LOCK_LEAVE();
|
||||
|
||||
ivup.ivtbl->ivptr[ivup.iv_index] = val;
|
||||
|
||||
rb_shape_set_shape(obj, shape);
|
||||
RB_OBJ_WRITTEN(obj, Qundef, val);
|
||||
|
||||
if (!found) {
|
||||
rb_shape_set_shape(obj, shape);
|
||||
}
|
||||
}
|
||||
|
||||
static VALUE *
|
||||
|
Loading…
x
Reference in New Issue
Block a user