diff options
author | Kevin Newton <[email protected]> | 2021-11-24 10:31:23 -0500 |
---|---|---|
committer | Aaron Patterson <[email protected]> | 2022-03-24 09:14:38 -0700 |
commit | 629908586b4bead1103267652f8b96b1083573a8 (patch) | |
tree | c2d53b1ae8b86571256f290851d95d6af4ba73db /class.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 'class.c')
-rw-r--r-- | class.c | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -1169,11 +1169,20 @@ module_in_super_chain(const VALUE klass, VALUE module) return false; } +// For each ID key in the class constant table, we're going to clear the VM's +// inline constant caches associated with it. +static enum rb_id_table_iterator_result +clear_constant_cache_i(ID id, VALUE value, void *data) +{ + rb_clear_constant_cache_for_id(id); + return ID_TABLE_CONTINUE; +} + static int do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super, bool check_cyclic) { VALUE p, iclass, origin_stack = 0; - int method_changed = 0, constant_changed = 0, add_subclass; + int method_changed = 0, add_subclass; long origin_len; VALUE klass_origin = RCLASS_ORIGIN(klass); VALUE original_klass = klass; @@ -1266,13 +1275,12 @@ do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super } tbl = RCLASS_CONST_TBL(module); - if (tbl && rb_id_table_size(tbl)) constant_changed = 1; + if (tbl && rb_id_table_size(tbl)) + rb_id_table_foreach(tbl, clear_constant_cache_i, (void *) 0); skip: module = RCLASS_SUPER(module); } - if (constant_changed) rb_clear_constant_cache(); - return method_changed; } |