13. class A
def a
puts 'Hi!'
end
end
class B < A; end
class C < B; end
class D < C; end
class E < D; end
class F < E; end
F.new.a
Thursday, 17 July, 14
19. Summary
• Methods are stored in a hashtable on the class where
they’re defined.
• Method resolution is a variable time algorithm whose
complexity depends on the depth of your class hierarchy.
• Module inclusion substantially increases the depth of
your class hierarchy, especially if those modules
themselves include modules.
• Method resolution is expensive.
Thursday, 17 July, 14
36. Defining methods.
Aliasing methods.
Removing methods.
Setting or removing constants.
Defining a class.
Defining a module.
Including a module.
things that
bust the cache
Thursday, 17 July, 14
37. Extending a module.
Using a refinement. (Ruby 2.0)
Garbage collecting a class.
Garbage collecting a module.
Changing the visibility of a constant.
Marshal loading an extended constant.
Autoload.
Built-in non-blocking IO methods.
things that
bust the cache
Thursday, 17 July, 14
39. Summary
• Method resolutions are cached in two places.
• Instruction caches are structs attached to the send
instruction.
• The global method cache is a hash table fixed at 2048
entries with no collision semantics and a random eviction
policy.
• Method cache entries are valid if their `vm_state` property
is the same as the current value of the `global_vm_state`
counter.
Thursday, 17 July, 14
40. Summary
• Method cache invalidation is always global, and
happens frequently in most ruby code.
• Method cache invalidation is constant time.
Thursday, 17 July, 14
52. Summary
• Both types of method cache entries now only need to
store a seq and method entry.
• Method caches are now stored with the RClass structs
and are (effectively) unbounded in size.
• Each RClass has a globally unique 64bit identifier.
• Method cache entries are tagged with the sequence of
their target klass at the time the cache entry was filled.
Thursday, 17 July, 14
53. Summary
• Entries are valid if their filled entry sequence is the same
as the current sequence identifier of the klass that is the
target of the invocation.
• Method caches are invalidated by assigning a new
sequence value to a klass.
• When changes are made to a klass, we traverse all of its
descendents and assign them new sequence values.
• This traversal is unfortunately a variable time algorithm,
and can be quite expensive.
Thursday, 17 July, 14