diff options
author | Kevin Newton <kddnewton@gmail.com> | 2021-11-24 10:31:23 -0500 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2022-03-24 09:14:38 -0700 |
commit | 629908586b4bead1103267652f8b96b1083573a8 (patch) | |
tree | c2d53b1ae8b86571256f290851d95d6af4ba73db /variable.c | |
parent | 5f10bd634fb6ae8f74a4ea730176233b0ca96954 (diff) |
Finer-grained inline constant cache invalidation
Current behavior - caches depend on a global counter. All constant mutations cause caches to be invalidated.
```ruby
class A
B = 1
end
def foo
A::B # inline cache depends on global counter
end
foo # populate inline cache
foo # hit inline cache
C = 1 # global counter increments, all caches are invalidated
foo # misses inline cache due to `C = 1`
```
Proposed behavior - caches depend on name components. Only constant mutations with corresponding names will invalidate the cache.
```ruby
class A
B = 1
end
def foo
A::B # inline cache depends constants named "A" and "B"
end
foo # populate inline cache
foo # hit inline cache
C = 1 # caches that depend on the name "C" are invalidated
foo # hits inline cache because IC only depends on "A" and "B"
```
Examples of breaking the new cache:
```ruby
module C
# Breaks `foo` cache because "A" constant is set and the cache in foo depends
# on "A" and "B"
class A; end
end
B = 1
```
We expect the new cache scheme to be invalidated less often because names aren't frequently reused. With the cache being invalidated less, we can rely on its stability more to keep our constant references fast and reduce the need to throw away generated code in YJIT.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5433
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/variable.c b/variable.c index 8cb507628c..d127bd11ea 100644 --- a/variable.c +++ b/variable.c @@ -2848,7 +2848,7 @@ rb_const_remove(VALUE mod, ID id) undefined_constant(mod, ID2SYM(id)); } - rb_clear_constant_cache(); + rb_clear_constant_cache_for_id(id); val = ce->value; if (val == Qundef) { @@ -3132,7 +3132,7 @@ rb_const_set(VALUE klass, ID id, VALUE val) struct rb_id_table *tbl = RCLASS_CONST_TBL(klass); if (!tbl) { RCLASS_CONST_TBL(klass) = tbl = rb_id_table_create(0); - rb_clear_constant_cache(); + rb_clear_constant_cache_for_id(id); ce = ZALLOC(rb_const_entry_t); rb_id_table_insert(tbl, id, (VALUE)ce); setup_const_entry(ce, klass, val, CONST_PUBLIC); @@ -3210,7 +3210,7 @@ const_tbl_update(struct autoload_const *ac) struct autoload_data_i *ele = current_autoload_data(klass, id, &ac); if (ele) { - rb_clear_constant_cache(); + rb_clear_constant_cache_for_id(id); ac->value = val; /* autoload_i is non-WB-protected */ ac->file = rb_source_location(&ac->line); @@ -3238,11 +3238,11 @@ const_tbl_update(struct autoload_const *ac) "previous definition of %"PRIsVALUE" was here", name); } } - rb_clear_constant_cache(); + rb_clear_constant_cache_for_id(id); setup_const_entry(ce, klass, val, visibility); } else { - rb_clear_constant_cache(); + rb_clear_constant_cache_for_id(id); ce = ZALLOC(rb_const_entry_t); rb_id_table_insert(tbl, id, (VALUE)ce); @@ -3297,10 +3297,6 @@ set_const_visibility(VALUE mod, int argc, const VALUE *argv, VALUE val = argv[i]; id = rb_check_id(&val); if (!id) { - if (i > 0) { - rb_clear_constant_cache(); - } - undefined_constant(mod, val); } if ((ce = rb_const_lookup(mod, id))) { @@ -3315,15 +3311,12 @@ set_const_visibility(VALUE mod, int argc, const VALUE *argv, ac->flag |= flag; } } + rb_clear_constant_cache_for_id(id); } else { - if (i > 0) { - rb_clear_constant_cache(); - } undefined_constant(mod, ID2SYM(id)); } } - rb_clear_constant_cache(); } void |