ScopedValue, finalized in Java 25, provides a fast, JIT-friendly way to access values scoped to a given thread, similar to ThreadLocal values but without many of their shortcomings.
We currently use ThreadLocal in a number of places, but most notably for accessing the current ThreadContext for a given thread. Over the years, we have worked to propagate this value through our own call stacks, including through core methods and JIT-compiled code, but we still must fall back on a ThreadLocal whenever calling into JRuby from external code like Java libraries.
By modifying our ThreadContext management to use a ScopedValue, we can start to provide a fast path for Java 25+ users to reach the context without traversing a ThreadLocal. Eventually we will require Java 25 minimum and nearly all context accesses will be through ScopedValue.
This process should start in JRuby 10.1.x as an optional feature only available when the ScopedValue API is accessible. It should be automatically available in Java 25, and might also possibly be available in 21 with preview features turned on. We will need to evaluate whether that is functional enough to allow use prior to the full support in 25.
ScopedValue, finalized in Java 25, provides a fast, JIT-friendly way to access values scoped to a given thread, similar to ThreadLocal values but without many of their shortcomings.
We currently use ThreadLocal in a number of places, but most notably for accessing the current ThreadContext for a given thread. Over the years, we have worked to propagate this value through our own call stacks, including through core methods and JIT-compiled code, but we still must fall back on a ThreadLocal whenever calling into JRuby from external code like Java libraries.
By modifying our ThreadContext management to use a ScopedValue, we can start to provide a fast path for Java 25+ users to reach the context without traversing a ThreadLocal. Eventually we will require Java 25 minimum and nearly all context accesses will be through ScopedValue.
This process should start in JRuby 10.1.x as an optional feature only available when the ScopedValue API is accessible. It should be automatically available in Java 25, and might also possibly be available in 21 with preview features turned on. We will need to evaluate whether that is functional enough to allow use prior to the full support in 25.