ScopedValue performance declines as number of dynamic bindings increases

Andrew Haley aph-open at littlepinkcloud.com
Wed Jun 25 10:28:18 UTC 2025


On 24/06/2025 16:12, Johan Sjolen wrote:
> I've been working on implementing a feature and the ScopedValue preview feature was an excellent fit to a particular design problem I faced.
> Reading the source code, I noticed that it seems as though the linked list of value bindings are shared across all ScopedValue instances. This was a bit surprising to me, as this means that the performance of accessing
> a ScopedValue binding declines linearly with the number of bindings before it on the stack.

Only the first time you use it: thereafter, the value is cached, and if 
you use it repeatedly in a method, hoisted into a register.

However, we don't cache failure. One reason for this is that the scoped 
value cache is created lazily, and we don't want to create it if all a 
thread is doing is using a scoped value to detect recursion. Also, we 
don't want to kick a value out of the cache for the sake of a failure. 
We can review this decision if the negative case turns out to be worth 
caching.

Finally, we don't expect to see huge numbers of scoped values in use. I 
guess there may be some edge cases where it might be appropriate, but I 
don't think this will be a common pattern.

 > I'm wondering about the reasoning and trade-offs here, what do we get 
  with our implementation that the Schemers do not? Maybe we should 
consider changing our implementation?

We want to be able to share a set of scoped values between parent and 
child threads with (near-)zero time and space overhead, so whatever 
structure contains them should be immutable. We want to be able to bind 
(and unbind) a scoped value in constant (hopefully very low) time, so 
rebinding can't, say, update a hash map. We want repeated lookups to be 
very fast.

We expect scoped values to be created as static finals, and much 
optimization depends on that. We expect to see small numbers of scoped 
values active at any time.

Finally, if you're aware of any application need for large numbers of 
scoped values, I'm interested to hear about it.

-- 
Andrew Haley  (he/him)O
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671


More information about the loom-dev mailing list