Skip to content

internal/core/runtime: replace slice with sync.Map#4262

Open
jonjohnsonjr wants to merge 1 commit intocue-lang:masterfrom
jonjohnsonjr:syncmap
Open

internal/core/runtime: replace slice with sync.Map#4262
jonjohnsonjr wants to merge 1 commit intocue-lang:masterfrom
jonjohnsonjr:syncmap

Conversation

@jonjohnsonjr
Copy link

We have a workload that makes many calls to cue.Value.Path(), which spends most of its time inside IndexToString's RWMutex.Rlock() call.

This change attempts to optimize that by replacing the labels slice with a sync.Map.

From sync.Map's godoc:

The Map type is optimized for two common use cases:
(1) when the entry for a given key is only ever written once but read
many times, as in caches that only grow, ...
In these two cases, use of a Map may significantly reduce lock
contention compared to a Go map paired with a separate Mutex or RWMutex.

I believe this maps exactly to what IndexToString is doing, and indeed we spend about 50% less time inside IndexToString after this change.

We have a workload that makes many calls to cue.Value.Path(), which
spends most of its time inside IndexToString's RWMutex.Rlock() call.

This change attempts to optimize that by replacing the `labels` slice
with a `sync.Map`.

From `sync.Map`'s godoc:

> The Map type is optimized for two common use cases:
> (1) when the entry for a given key is only ever written once but read
> many times, as in caches that only grow, ...
> In these two cases, use of a Map may significantly reduce lock
> contention compared to a Go map paired with a separate Mutex or RWMutex.

I believe this maps exactly to what IndexToString is doing, and indeed
we spend about 50% less time inside IndexToString after this change.

Signed-off-by: Jon Johnson <jon.johnson@chainguard.dev>
@mvdan mvdan requested a review from rogpeppe February 13, 2026 09:12
@mvdan
Copy link
Member

mvdan commented Feb 13, 2026

Assigning to @rogpeppe given that he's been looking at concurrency issues with Marcel, and he's also been chatting with you recently :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments