Skip to content

ring/kv/dynamodb: reuse timers in watch loops to avoid per-poll allocations#7266

Merged
friedrichg merged 3 commits intocortexproject:masterfrom
sandy2008:master
Feb 16, 2026
Merged

ring/kv/dynamodb: reuse timers in watch loops to avoid per-poll allocations#7266
friedrichg merged 3 commits intocortexproject:masterfrom
sandy2008:master

Conversation

@sandy2008
Copy link
Contributor

What this PR does:

  • Replaces time.After(c.pullerSyncTime) in DynamoDB watch loops with a reusable time.Timer.
  • Applies this to both:
    • WatchKey()
    • WatchPrefix()
  • Adds resetTimer() helper (stop + drain + reset) to safely reuse timers.
  • Adds a focused benchmark to validate the change:
    • BenchmarkWatchLoopWaitWithTimeAfter
    • BenchmarkWatchLoopWaitWithReusableTimer

Performance benchmark on local machine shows:

  • time.After: ~248 B/op, 3 allocs/op
  • reusable timer: 0 B/op, 0 allocs/op

No behavior change intended, only allocation reduction in long-running watch loops.

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Signed-off-by: Sandy Chen <Yuxuan.Chen@morganstanley.com>
Copy link
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

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

Thanks for the PR

Adds a focused benchmark to validate the change:
BenchmarkWatchLoopWaitWithTimeAfter
BenchmarkWatchLoopWaitWithReusableTimer

I think you forgot to include these in the PR

Signed-off-by: Sandy Chen <Yuxuan.Chen@morganstanley.com>
@pull-request-size pull-request-size bot added size/M and removed size/S labels Feb 15, 2026
@sandy2008
Copy link
Contributor Author

Thanks for the PR

Adds a focused benchmark to validate the change:
BenchmarkWatchLoopWaitWithTimeAfter
BenchmarkWatchLoopWaitWithReusableTimer

I think you forgot to include these in the PR

@friedrichg Thx, added :)

@sandy2008
Copy link
Contributor Author

Execution for the test:

  • go test ./pkg/ring/kv/dynamodb

    • ok github.com/cortexproject/cortex/pkg/ring/kv/dynamodb (cached)
  • go test ./pkg/ring/kv/dynamodb -run '^$' -bench 'BenchmarkWatchLoopWaitWith(TimeAfter|ReusableTimer)$' -benchmem -count=3

    • BenchmarkWatchLoopWaitWithTimeAfter: ~260–264 ns/op, 248 B/op, 3 allocs/op
    • BenchmarkWatchLoopWaitWithReusableTimer: ~248–259 ns/op, 0 B/op, 0 allocs/op
    • PASS

Result: reusable timer keeps latency similar and removes per-iteration allocations in the watch-loop wait path.

@sandy2008 sandy2008 requested a review from friedrichg February 16, 2026 01:20
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 16, 2026
Signed-off-by: Sandy Chen <Yuxuan.Chen@morganstanley.com>
@sandy2008
Copy link
Contributor Author

@friedrichg Sorry, reran modernize.

@friedrichg friedrichg merged commit 5069a1a into cortexproject:master Feb 16, 2026
59 of 60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend/dynamodb lgtm This PR has been approved by a maintainer size/M type/performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants