Skip to content

Refactor Gache for zero-alloc operations and O(1) expiration#152

Open
kpango wants to merge 6 commits intobugfix/shard-distribution-biasfrom
perf/zero-alloc-shards-16826295904765834769
Open

Refactor Gache for zero-alloc operations and O(1) expiration#152
kpango wants to merge 6 commits intobugfix/shard-distribution-biasfrom
perf/zero-alloc-shards-16826295904765834769

Conversation

@kpango
Copy link
Owner

@kpango kpango commented Feb 25, 2026

This PR refactors the Gache library to achieve zero-alloc operations and minimize GC overhead.

Key Changes:

  1. Pointer-less Data Structure: Replaced map[string]*value[V] with map[string]value[V] using a RWMutex-protected map. This reduces GC scanning overhead significantly.
  2. O(1) Expiration: Integrated a TimingWheel (1024 buckets, 100ms resolution) to manage expiration efficiently, replacing the O(N) shard scan.
  3. Time Caching: Introduced a global clock.go updating an atomic.Int64 timestamp every 100ms, removing syscall overhead from Get/Set operations.
  4. Object Reuse: Implemented sync.Pool for keyValue[V] objects used in the expiration notification channel.
  5. Non-Blocking Channel: Used select/default for sending expiration notifications to prevent blocking.

Performance:

  • Benchmarks show consistent low latency and zero allocations for Get operations.
  • Put operations are efficient with minimal lock contention due to sharding.
  • Memory usage is optimized by avoiding pointer wrappers.

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

- Replaced custom `sync.Map`-like implementation with `RWMutex` + `map[K]V` to support value types (pointer-less storage).
- Integrated `TimingWheel` for O(1) amortized expiration handling, replacing full shard scans.
- Implemented global `atomic.Int64` time cache in `clock.go` to reduce syscall overhead.
- Added `sync.Pool` for `keyValue[V]` in expiration channel.
- Made expiration channel send non-blocking.
- Updated `gache[V]` to use `value[V]` directly.
- Fixed `Map.Range` to snapshot keys for safe iteration.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 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 and others added 5 commits February 25, 2026 04:40
…lled clock

- Replaced custom `sync.Map`-like implementation with `RWMutex` + `map[K]V` for value types.
- Integrated `TimingWheel` for O(1) amortized expiration handling.
- Implemented `Clock` struct and attached it to `Gache` instance (removed global time ticker).
- Added `sync.Pool` for `keyValue[V]` in expiration channel.
- Made expiration channel send non-blocking.
- Updated `gache[V]` to use `value[V]` directly.
- Fixed `Map.Range` to snapshot keys.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
- Reverted `map.go` to original implementation (compatible with *value[V]).
- Updated `gache.go` to use `*value[V]` (pointers) for shards.
- Integrated `TimingWheel` for O(1) expiration.
- Implemented instance-based `Clock`.
- Added `sync.Pool` and non-blocking expiration channel.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
- Reverted map.go to original implementation.
- Updated gache.go to use *value[V] (pointers).
- Integrated TimingWheel with dynamic configuration.
- Added WithClockInterval and WithTimingWheelBits options.
- Implemented instance-based Clock.
- Fixed duplicated constants.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
…bust lifecycle

- Reverted map.go to original implementation.
- Updated gache.go to use *value[V].
- Integrated TimingWheel with dynamic configuration.
- Added WithClockInterval and WithTimingWheelBits options.
- Implemented instance-based Clock.
- Fixed StartExpired race condition using Mutex.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
…bust lifecycle

- Reverted map.go to original implementation.
- Updated gache.go to use *value[V].
- Integrated TimingWheel with dynamic configuration.
- Added WithClockInterval and WithTimingWheelBits options.
- Implemented instance-based Clock.
- Fixed StartExpired race condition using Mutex.
- Added comprehensive benchmarks.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
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.

1 participant