Skip to content

Hold the fstring dedup wrapper weakly in its thread-local - #9927

Merged
headius merged 1 commit into
jruby:jruby-10.0from
matrei:fix/fstring-weak-holder
Oct 28, 2026
Merged

headius merged 1 commit into
jruby:jruby-10.0from
matrei:fix/fstring-weak-holder

Conversation

@matrei

@matrei matrei commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Part of #9781, against jruby-10.0 for the 10.0 LTS line.

DEDUP_WRAPPER_CACHE is a per-runtime ThreadLocal<FStringEqual>, so every thread that dedups a string keeps an FStringEqual in its ThreadLocalMap. FStringEqual is a JRuby class, so after the runtime is torn down, a pooled thread keeps the class loader that loaded JRuby alive until its ThreadLocalMap happens to expunge the stale entry. tearDown() can't clear it, because ThreadLocal.remove() only removes the calling thread's value.

The thread-local now holds a WeakReference<FStringEqual>, so the threads' maps only keep JDK types. The wrapper is still reused while it's alive. When it has been collected, the next lookup on that thread makes a new one, so it costs one allocation per thread per GC instead of one per lookup.

Performance. The options are compared in this comment. On the hit path with 1 to 32 threads, a lookup takes 2–7% longer than with the current thread-local when the threads do nothing but lookups, and about 1% longer with about 490 ns of other work per lookup. It doesn't allocate, and the threads don't share anything. A single synchronized wrapper and a single wrapper taken with an atomic swap were 105 and 28 times slower than the current code with 32 threads.

Before and after. With the reproducer from #9781, on current jruby-10.0, which includes #9922, and with jffi 1.4.4, these class loaders were still alive after each round, with -XX:SoftRefLRUPolicyMSPerMB=0:

rounds 1, 2, 3, 4 class space after round 4
jruby-10.0 1 of 1, 2 of 2, 1 of 3, 1 of 4 6 MB
this PR 0 of 1, 0 of 2, 0 of 3, 0 of 4 1 MB

With the default settings, the remaining references are soft ones from the RubyEncoding coder thread-locals, which the JVM clears under memory pressure.

rake spec:ji passes (974 examples, 0 failures, 9 pending). rake spec:ruby:fast has 1 failure and 4 errors, all IPv6 socket specs that fail the same way on unmodified jruby-10.0, because my machine has no IPv6 network.

DEDUP_WRAPPER_CACHE left an FStringEqual in every thread that deduped a
string. FStringEqual is a JRuby class, so after the runtime was torn
down, a pooled thread kept the class loader that loaded JRuby alive
until its ThreadLocalMap happened to expunge the stale entry. tearDown()
can't clear it, because ThreadLocal.remove() only removes the calling
thread's value.

The thread-local now holds a WeakReference to the wrapper, so the
threads only keep JDK types. When the wrapper has been collected, the
next lookup on that thread makes a new one.

@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.

As discussed!

@headius
headius merged commit 700170b into jruby:jruby-10.0 Oct 28, 2026
127 checks passed
@matrei
matrei deleted the fix/fstring-weak-holder branch October 28, 2026 18:54
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