Skip to content

⚡ Optimize ToRawMap performance#146

Merged
kpango merged 1 commit intomainfrom
perf-optimize-torawmap-9827599965449859755
Feb 25, 2026
Merged

⚡ Optimize ToRawMap performance#146
kpango merged 1 commit intomainfrom
perf-optimize-torawmap-9827599965449859755

Conversation

@google-labs-jules
Copy link
Contributor

This PR optimizes ToRawMap by replacing the concurrent g.Range call (which caused high lock contention on a single mutex) with a sequential iteration over the shards. Since ToRawMap constructs a new map, sequential writes are safe and significantly faster than managing 512 concurrent goroutines fighting for a single lock.

Performance Impact:

  • Latency: ~3.5x improvement (4.18ms -> 1.18ms)
  • Allocations: ~30x reduction (1066 -> 34 allocs/op)

Tests passed.


PR created automatically by Jules for task 9827599965449859755 started by @kpango

- Replaced concurrent `Range` iteration with sequential shard iteration.
- Removed `sync.Mutex` and goroutine overhead.
- Maintained expiration logic for invalid items.
- Benchmarks show ~3.5x latency improvement and ~30x fewer allocations.
@google-labs-jules
Copy link
Contributor Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@kpango kpango marked this pull request as ready for review February 25, 2026 03:29
Copilot AI review requested due to automatic review settings February 25, 2026 03:29
@kpango kpango merged commit 0c6f936 into main Feb 25, 2026
5 checks passed
@kpango kpango deleted the perf-optimize-torawmap-9827599965449859755 branch February 25, 2026 03:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves gache.(*gache).ToRawMap performance by removing the per-entry mutex + concurrent shard Range pattern (which caused lock contention) and instead iterating shards sequentially and writing to the output map directly.

Changes:

  • Replaced ToRawMap’s g.Range(...) (concurrent shard iteration) with a sequential loop over g.shards.
  • Removed the mutex previously required to protect concurrent writes to the output map.
  • Preserved existing behavior of omitting expired entries and triggering expiration cleanup during traversal.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants