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

shyouhei commented Oct 21, 2019

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 shyouhei:n-way-cc branch 2 times, most recently from 21b977c to 4a05e3a Oct 21, 2019
@methodmissing

This comment has been minimized.

Copy link
Contributor

methodmissing commented Oct 21, 2019

Excellent write up 🤤

@shyouhei shyouhei force-pushed the shyouhei:n-way-cc branch from 4a05e3a to 9f323ac Oct 23, 2019
@shyouhei shyouhei marked this pull request as ready for review Oct 23, 2019
@shyouhei

This comment has been minimized.

Copy link
Member Author

shyouhei commented Oct 23, 2019

@ko1 any idea?

@shyouhei

This comment has been minimized.

Copy link
Member Author

shyouhei commented Oct 25, 2019

Conflicts with #2564, going to resolve...

@shyouhei shyouhei force-pushed the shyouhei:n-way-cc branch from 9f323ac to 241f0de Oct 26, 2019
@shyouhei

This comment has been minimized.

Copy link
Member Author

shyouhei commented Nov 4, 2019

Can I merge this?

@shyouhei shyouhei force-pushed the shyouhei:n-way-cc branch 5 times, most recently from 3833358 to 1de890a Nov 5, 2019
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
@shyouhei shyouhei force-pushed the shyouhei:n-way-cc branch from 0ed795b to 99b15b7 Nov 7, 2019
@shyouhei shyouhei merged commit d45a013 into ruby:master Nov 7, 2019
14 of 15 checks passed
14 of 15 checks passed
latest (check)
Details
check_branch
Details
latest (ubuntu-latest, check)
Details
latest (windows-2016, 2017, test)
Details
latest (test-bundler)
Details
latest (ubuntu-latest, test-bundler)
Details
latest (windows-2019, 2019, test)
Details
latest (test-bundled-gems)
Details
latest (ubuntu-latest, test-bundled-gems)
Details
latest (ubuntu-16.04, check)
Details
latest (ubuntu-16.04, test-bundler)
Details
latest (ubuntu-16.04, test-bundled-gems)
Details
continuous-integration/drone/pr Build was killed
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@shyouhei shyouhei deleted the shyouhei:n-way-cc branch Nov 7, 2019
@eregon eregon mentioned this pull request Dec 30, 2019
28 of 70 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.