Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
reduce cache mishits #2583
While running discourse benchmark under linux perf, I noticed one of the most frequent operations that ruby does is the method lookup.
That was kind of known, but something didn't smell right. I pushed 3ffd98c and ran the benchmark again, to get this output:
So 65,039,079 out of 94,613,117 inline cache mishits are spurious; they resulted in method lookups that end up exactly the same method entry the cache already stored. This is not a buggy behaviour and your program works as expected (apart from unnecessarily consuming our precious environmental resources to generate electricity for the computation). However there definitely is a room of improvements.
Let's use the cache more efficiently. We are facing the fact that several classes share the identical method entry for a method name; possibly due to inheritance, inclusions, and so on. We can use this. A call cache is valid for multiple classes. So in order to express the info, this changeset expands
By this changeset the debug counter output for the same benchmark is now like this:
Prior to this changeset, majority of inline cache mishits resulted into the same method entry when rb_callable_method_entry() resolves a method search. Let's not call the function at the first place on such situations. In doing so we extend the struct rb_call_cache from 44 bytes (in case of 64 bit machine) to 64 bytes, and fill the gap with secondary class serial(s). Call cache's class serials now behavies as a LRU cache. Calculating ------------------------------------- ours 2.7 2.6 vm2_poly_same_method 2.339M 1.744M 1.369M i/s - 6.000M times in 2.565086s 3.441329s 4.381386s Comparison: vm2_poly_same_method ours: 2339103.0 i/s 2.7: 1743512.3 i/s - 1.34x slower 2.6: 1369429.8 i/s - 1.71x slower