Repository navigation
Hold the fstring dedup wrapper weakly in its thread-local - #9927
Merged
Merged
Conversation
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.
This was referenced Oct 28, 2026
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.
Part of #9781, against
jruby-10.0for the 10.0 LTS line.DEDUP_WRAPPER_CACHEis a per-runtimeThreadLocal<FStringEqual>, so every thread that dedups a string keeps anFStringEqualin itsThreadLocalMap.FStringEqualis a JRuby class, so after the runtime is torn down, a pooled thread keeps the class loader that loaded JRuby alive until itsThreadLocalMaphappens to expunge the stale entry.tearDown()can't clear it, becauseThreadLocal.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:jruby-10.0With the default settings, the remaining references are soft ones from the
RubyEncodingcoder thread-locals, which the JVM clears under memory pressure.rake spec:jipasses (974 examples, 0 failures, 9 pending).rake spec:ruby:fasthas 1 failure and 4 errors, all IPv6 socket specs that fail the same way on unmodifiedjruby-10.0, because my machine has no IPv6 network.