Tweak kwargs logic to improve JIT inlining#8095
Merged
headius merged 4 commits intojruby:masterfrom Feb 9, 2024
Merged
Conversation
This method was often too big for JDK to inline, and performed more boolean checks than necessary for the common paths. This patch splits it up so it will be simpler and more inlinable. * Split ruby2 kwarg logic into a separate path. It can change after method definition, and potentially after JIT, but it would be strange and buggy to not set it immediately. It can't be unset, so we assume if it is true at JIT time it will be true always. * Fetch and compare relevant state only as-needed, rather than all ahead of time. This avoids extra fetches, branches, and comparisons that won't be used along common paths. Primarily this means deferring the bit checks of callInfo and the cast of the last argument to a RubyHash until needed.
Most JVM JITs can trivially inline simple static methods, and these calls are made frequently, so we flip them to static versions.
4769248 to
f1472ff
Compare
* Split out ruby2_keywords path * Create a simplified path for non-ruby2_keywords, non-rest, non- keyword receivers (most common no-op varargs case) * Refactor full receiveKeywords for simplicity by inlining utility methods and reducing boolean logic as much as possible. This commit loses some of the original comments during development due to the difficulty of tracking them during refactoring, but new comments have been strategically added to aid readability of the new version. The most simplified case described above should inline better, and the complex path is at least reduced and perhaps easier to optimize.
The ruby2_keywords method is used to switch a method or block to use Ruby 2.x keyword argument behavior, based on a normal Hash passed as the last argument. When this behavior changed for Ruby 3.0, ruby2_keywords was provided as a way to indicate the old behavior is required for a given scope. Recent optimizations to kwarg handling in JRuby assume that by the time JIT has happened, the ruby2_keywords bit has already been set or will never be set, so we can avoid checking it on each call. This works fine for normal situations, but under the following circumstance (found only in CRuby tests, so far), JIT may happen before the call to ruby2_keywords, and the compiled code uses the wrong (Ruby 3) logic: * Scope 1 (a method or block) contains the definition of scope 2 (another method or block). * Scope 1 also calls ruby2_keywords to alter the kwarg behavior of scope 2. * Scope 1 gets compiled immediately (no interpretation), which forces scope 2 to compile immediately *before* ruby2_keywords can be called. * As a result scope 2 is pinned to Ruby 3 kwarg behavior and because immediately-compiled child scopes do not have a way to deopt back to interpreter, the ruby2_keywords behavior is never reflected. This situation is extremely rare; only a few unusual cases can trigger it: * Whole-script AOT compilation, because the methods get compiled before executing code that calls ruby2_keywords. * JIT with threshold=0 and method or block bodies that contain child scopes intended to use ruby2_keywords, as described above and found in CRuby tests. * Code that defines methods, allows them to be called enough times for JIT, and some time after JIT attempts to set ruby2_keywords. Because of the unusual circumstances that can cause problems, this patch disables compilation for any scope that contains calls to ruby2_keywords, which solves the first two scenarios. The third scenario is not fixed by this, but is by far the most rare and least concern of the three; if the method is being called before ruby2_keywords is calls, then by definition it needs to work properly with Ruby 3 kwarg logic, so the failure to switch to Ruby 2 kwarg logic should not be apparent. A future update to JRuby may improve our ability to "back off" from jitted code and recompile if ruby2_keywords is called.
headius
added a commit
to headius/jruby
that referenced
this pull request
Apr 23, 2024
In jruby#8095 (jruby/jruby@121c60fb) I optimized the JIT to statically compile in the logic for ruby2_keywords at the time of JIT. Unfortunately there turned out to be too many cases where this flag can change at runtime, leading to bugs like jruby#8119 where a previously-jitted scope gets flipped to ruby2_keywords later on. This patch partially reverts that optimization and resumes passing in the live ruby2_keywords value from the scope. We can revisit this in the future when it is possible to throw out and re-compile scopes that get this flag set later on. Fixes jruby#8119
This was referenced Apr 23, 2024
headius
added a commit
that referenced
this pull request
Apr 24, 2024
Partially undo kwarg optz from #8095
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A series of commits to make keyword argument logic more inlinable in JVM JIT compilers.