Skip to content

Fall back to normal call path for non-symbol keyword keys - #9815

Merged
headius merged 1 commit into
jruby:masterfrom
sferik:jit-braceless-hash-keywords
Oct 19, 2026
Merged

headius merged 1 commit into
jruby:masterfrom
sferik:jit-braceless-hash-keywords

Conversation

@sferik

@sferik sferik commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

A call with a braceless hash argument whose key isn’t a plain symbol, such as h.merge("a" => 1), sets the keywords flag, but its last argument is a temporary variable, not a literal operands.Hash. The direct keyword-argument path in compileCallCommon (added in #9219) cast that argument to Hash anyway. The cast threw ClassCastException, so the method could never be JIT-compiled and ran in the interpreter for good.

This change takes the direct path only when the last argument is a literal hash whose keys are all symbols. Every other call goes through the normal call path, as it did in 10.0.7. Checking for a Hash alone isn’t enough, because compileCallWithKeywords also casts each key to Symbol.

The new spec in spec/compiler/general_spec.rb covers each failing form from the issue, a mixed positional/keyword call, and a call inside a block. Before this change it fails in JIT mode with the ClassCastException; after it, it passes in all four modes.

A call with a braceless hash argument whose key is not a plain symbol,
such as h.merge("a" => 1), set the keywords flag but did not pass a
literal symbol-keyed hash as its last argument. The direct keyword
path in compileCallCommon cast that argument to Hash anyway, so the
JIT threw ClassCastException and the method stayed interpreted.

Take the direct path only when the last argument is a literal hash
whose keys are all symbols, and use the normal call path otherwise.

Fixes jruby#9814
@sferik
sferik force-pushed the jit-braceless-hash-keywords branch from 39018e2 to d343166 Compare October 17, 2026 12:46

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch here. I wonder how many such calls remain in typical Ruby code that were prevented from JIT compiling because of this bug.

Seems like something could go into ruby/spec but I'm not sure what that would look like; this is internal behavior that would be difficult to spec.

We probably need to start setting up a suite of ruby/spec specs that we force to JIT and confirm everything JITs as a way to audit. Currently we do some forced JIT runs of specs and tests, but because of fallback to interpreter they may fail to JIT silently.

spec:compiler is intended to be our "hard check" on JIT success for now, so this addition is correct.

@headius headius added this to the JRuby 10.1.3.0 milestone Oct 19, 2026
@headius
headius merged commit 81ed517 into jruby:master Oct 19, 2026
133 of 135 checks passed
@sferik

sferik commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

IMHO, verification is probably better achieved as a benchmark that fails in CI if performance degrades below some threshold vs. specifying the implementation, since performance is the thing you actually care about.

@sferik

sferik commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@headius I took a crack at implementing this in #9819. Feedback welcome!

@headius

headius commented Oct 19, 2026

Copy link
Copy Markdown
Member

Just to clarify a bit... JRuby does no profiled optimizations when emitting JVM bytecode, so it's possible to verify the bytecode JIT without ever running the code it compiles.

We can deterministically verify that something we expect to compile does indeed compile. I think the gap in that verification was introduced when jrubyc command-line AOT compilation stopped actually generating JVM bytecode (it just does IR compilation and stores that in the .class). We have test runs that attempt to compile a whole directory of sources, but they no longer actually exercise the bytecode compiler.

Performance tests make sense if, for example, we are testing profiled optimizations that require code to run for a long time in a particular way. Basically, for verifying behaviors that are circumstantial or temporal. In our case, that would be the appropriate way to verify that the JVM is properly optimizing the code we give it.

@headius

headius commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

An example that would have caught this case had we been testing for it:

Running jruby -X+C -Xjit.logging -S gem list we can see the following failure before this PR:

2026-10-19T15:19:18.481-05:00 [main] ERROR Ruby : failed to compile target script: /Users/headius/work/jruby/lib/ruby/stdlib/rubygems/config_file.rb - java.lang.ClassCastException: class org.jruby.ir.operands.TemporaryLocalVariable cannot be cast to class org.jruby.ir.operands.Hash (org.jruby.ir.operands.TemporaryLocalVariable and org.jruby.ir.operands.Hash are in module org.jruby.dist of loader 'app')

The -X+C forces everything to compile eagerly, but it does not reject things that don't get compiled to bytecode. The extra -Xjit.logging flag force all JIT attempts to be logged and we can see failures.

After your PR, the failures in this case no longer include the Hash cast issue and have these expected failures:

2026-10-19T15:21:43.430-05:00 [main] ERROR Ruby : failed to compile target script: file:///Users/headius/work/jruby/lib/jruby.jar!/jruby/kernel.rb - AOT not supported for scripts containing eval.
2026-10-19T15:21:43.690-05:00 [main] ERROR Ruby : failed to compile target script: /Users/headius/work/jruby/lib/ruby/stdlib/rubygems.rb - AOT not supported for scripts containing eval.
2026-10-19T15:21:44.721-05:00 [main] ERROR Ruby : failed to compile target script: /Users/headius/work/jruby/lib/ruby/gems/shared/gems/rdoc-7.2.0/lib/rdoc/markdown/entities.rb - literal hash has more than 125 pairs
2026-10-19T15:21:44.846-05:00 [main] ERROR Ruby : failed to compile target script: /Users/headius/work/jruby/lib/ruby/stdlib/rubygems/vendor/uri/lib/uri/common.rb - literal hash has more than 125 pairs

sferik added a commit to sferik/jruby that referenced this pull request Oct 20, 2026
JRuby's bytecode JIT uses no runtime profile, so whether a method
compiles doesn't depend on how it has run. tool/jit_compile_check.rb
builds IR for each file without running it, then compiles each method
and block on its own and loads the class, as the JIT does once one gets
hot. Any exception is a method that would stay interpreted forever.

rake test:jit_compile runs it over JRuby's Ruby kernel, the stdlib,
ruby/spec and the MRI tests: about 118,000 methods and blocks in under a
minute. With the fix for jruby#9814
reverted, it fails on 937 of them.

It finds 11 causes of failure on master. Three are deliberate JIT
limits and one only appears with Java assertions on. They are listed
in tool/jit_compile_check.excludes, so that the check fails only on a
new cause. The most common, a VerifyError for keyword arguments with a
block-pass argument, is jruby#9820.

Suggested in
jruby#9815 (comment)
sferik added a commit to sferik/jruby that referenced this pull request Oct 20, 2026
JRuby's bytecode JIT uses no runtime profile, so whether a method
compiles doesn't depend on how it has run. tool/jit_compile_check.rb
builds IR for each file without running it, then compiles each method
and block on its own and loads the class, as the JIT does once one gets
hot. Any exception is a method that would stay interpreted forever.

rake test:jit_compile runs it over JRuby's Ruby kernel, the stdlib,
ruby/spec and the MRI tests: about 118,000 methods and blocks in under a
minute. With the fix for jruby#9814
reverted, it fails on 937 of them.

tool/jit_compile_check.known.yml lists the causes that don't fail the
check. The only ones on master are the forms the JIT deliberately
doesn't compile: array literals over 250 elements, hash literals over
125 pairs, and a splat after other arguments. A bug that can't be fixed
right away can be listed with links to its issues, and the report
labels each failure EXPECTED, KNOWN BUG or FAIL.

Suggested in
jruby#9815 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants