GeezerVison is showing github.com — Cache immutable literals in indy JIT by headius · Pull Request #9644 · jruby/jruby
Conversation
headius
commented
Sep 3, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Immutable literal values like numerics and ranges can be created once globally and never constructed again. Without a global cache, however, both the IR and the JIT will potentially construct them more than once, even if those values may be locally cached. In addition, the IR optimizer will sometimes propagate immutable literal values through variables and copies making it appear that there are multiple of the same literal in a piece of code. This all combines to potentially create more instances of immutable literal values than the single global value actually needed.
This PR introduces a global cache for several immutable literal values using an object key and a weak-valued map. So long as one piece of code remains loaded and referencing a given value, it should not be recreated in duplicate operands or call sites. This cache acts as a sort of constant pool for these values.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Immutable literal values like numerics and ranges can be created
once globally and never constructed again. Without a global cache,
however, both the IR and the JIT will potentially construct them
more than once, even if those values may be locally cached. In
addition, the IR optimizer will sometimes propagate immutable
literal values through variables and copies making it appear that
there are multiple of the same literal in a piece of code. This all
combines to potentially create more instances of immutable literal
values than the single global value actually needed.
This PR introduces a global cache for several immutable literal
values using an object key and a weak-valued map. So long as one
piece of code remains loaded and referencing a given value, it
should not be recreated in duplicate operands or call sites. This
cache acts as a sort of constant pool for these values.
|
This is a simple way to handle caching immutable literals but I need to run some tests to make sure it does not increase memory size. Where before there might have been two RubyFloat with the same value, now there will be one RubyFloat plus a Double key and a WeakReference holder along with whatever bookkeeping objects are required inside WeakValuedMap. For values repeated in many places, this will be a savings, but for values that only appear a few times in code, we may actually be using more memory.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
what's the motivation behind this, does this matter in production apps?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@kares I don't have an answer to that. I do know that it surfaced a bug when I started re-enabling non-indy test runs #9643. It is of debatable utility, though, which is why I commented above that the overhead of this system may not be worth any small benefit.
It is arguably similar to a constant pool, which JVM classes typically do on a per-classfile basis, or deduplicated Strings in the JVM. But at the JVM level they can implement weak caches much more efficiently.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|