RFR: 8361497: Scoped Values: orElse and orElseThrow do not access the cache

Chen Liang liach at openjdk.org
Mon Jul 7 16:35:39 UTC 2025


On Mon, 7 Jul 2025 16:13:08 GMT, Andrew Haley <aph at openjdk.org> wrote:

> Neither `ScopedValue.orElse` nor `ScopedValue.orElseThrow` consult the cache when searching for a binding. Neither do they update the cache when a binding is found.
> While this issue does not affect spec compliance, it is surprising to users that `x.orElse(other)` should be slower than `x.isBound ? x.get() : other`.

Can `get` and `slowGet` be refactored to use this `findBinding` too? That way we have just one piece of code accessing the cache, easier for long-term maintenance. `get` can probably become something like:


        var value = findBinding();
        if (value == Snapshot.NIL) {
            throw new NoSuchElementException("ScopedValue not bound");
        }
        return (T)value;


and remove `slowGet` altogether.

P.S. moved from inline comments, not really suitable for inline

-------------

PR Comment: https://git.openjdk.org/jdk/pull/26164#issuecomment-3045855108


More information about the core-libs-dev mailing list