Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce cache mishits #2583

Merged
merged 1 commit into from
Nov 7, 2019
Merged

reduce cache mishits #2583

merged 1 commit into from
Nov 7, 2019

Conversation

shyouhei
Copy link
Member

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 struct rb_call_cache from 44-ish bytes to cache line width. The space we add is used for second and later class serials.

By this changeset the debug counter output for the same benchmark is now like this:

Screenshot from 2019-10-21 17-56-48

The mc_miss_spurious counter dropped down to 23,344,738.

@shyouhei shyouhei force-pushed the n-way-cc branch 2 times, most recently from 21b977c to 4a05e3a Compare October 21, 2019 12:06
@methodmissing
Copy link
Contributor

Excellent write up 🤤

@shyouhei shyouhei marked this pull request as ready for review October 23, 2019 01:07
@shyouhei
Copy link
Member Author

@ko1 any idea?

@shyouhei
Copy link
Member Author

Conflicts with #2564, going to resolve...

@shyouhei
Copy link
Member Author

shyouhei commented Nov 4, 2019

Can I merge this?

@shyouhei shyouhei force-pushed the n-way-cc branch 5 times, most recently from 3833358 to 1de890a Compare November 7, 2019 05:31
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants