Scoped variables

Doug Lea dl at
Wed Dec 5 13:24:38 UTC 2018

On 12/5/18 5:15 AM, Andrew Haley wrote:
> About half of the code in ThreadLocal.get() is because of bounds 
> checks, indirections, type checks, and null pointer checks.

Yeah, welcome to my world. It takes code that people think is weird/ugly
to express these in ways that are still pretty fast. I haven't tried
recently with ThreadLocal, but I once checked that similar constructions
in j.u.HashMap were about as fast as Unsafe cheats. So I don't think
you'd see significant speedup recoding natively. But please prove me wrong.

> The null pointer checks facilitate lazy initialization, which we
> don't need in scoped variables. 

Well, something somewhere would surely null check result, so it's hard
to imagine this helping much.

> There are some because of the use of WeakReferences, which scoped
> variables won't need because they disappear when the scope exits.

Not using Weak Refs would be the biggest win. Lots of simplifications
would be possible. But would require a new API.


More information about the loom-dev mailing list