Skip to content

perf: cached clock#4315

Merged
Flo4604 merged 1 commit intomainfrom
11-11-perf_cached_clock
Nov 11, 2025
Merged

perf: cached clock#4315
Flo4604 merged 1 commit intomainfrom
11-11-perf_cached_clock

Conversation

@chronark
Copy link
Collaborator

@chronark chronark commented Nov 11, 2025

What does this PR do?

Adds a new CachedClock implementation that caches time values to reduce system call overhead. This implementation uses an atomic value that's updated at a configurable resolution by a background goroutine, providing better performance than direct time.Now() calls.

The PR also simplifies the Clock interface by removing ticker-related functionality, focusing solely on time retrieval. Corresponding ticker implementations have been removed from both RealClock and TestClock.

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

cd go
go test -bench=. ./pkg/clock/

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Ran make fmt on /go directory
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Also cleaned up unused tickers
@vercel
Copy link

vercel bot commented Nov 11, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
dashboard Ready Ready Preview Comment Nov 11, 2025 5:37pm
engineering Ready Ready Preview Comment Nov 11, 2025 5:37pm

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2025

⚠️ No Changeset found

Latest commit: c2ba107

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 11, 2025

📝 Walkthrough

Walkthrough

Introduces a new CachedClock implementation that caches time values at configurable intervals to minimize system calls, accompanied by comprehensive tests and benchmarks. Simultaneously removes ticker-related abstractions (NewTicker method and Ticker interface) from the Clock interface and all implementations (RealClock and TestClock).

Changes

Cohort / File(s) Summary
New CachedClock Implementation
go/pkg/clock/cached_clock.go
Adds CachedClock type with atomic nanos field, background goroutine for periodic cache updates at configurable resolution, Now() method returning cached time, and Close() for cleanup. Includes compile-time interface assertion.
CachedClock Tests
go/pkg/clock/cached_clock_test.go
Comprehensive unit tests covering initialization, time caching behavior, concurrent access safety, post-close behavior, edge cases (very small/large resolutions), and interface compliance.
Clock Benchmarks
go/pkg/clock/clock_benchmarks_test.go
Parallel benchmarks comparing CachedClock performance across multiple resolutions (1µs–10ms) against RealClock's Now() method.
Interface Simplification
go/pkg/clock/interface.go
Removes NewTicker(d time.Duration) method from Clock interface and deletes entire Ticker interface with C() and Stop() methods.
RealClock Ticker Removal
go/pkg/clock/real_clock.go
Removes NewTicker method and internal realTicker type/implementation from RealClock.
TestClock Ticker Removal
go/pkg/clock/test_clock.go
Removes tickers field from TestClock struct, eliminates NewTicker method, removes testTicker type and all ticker-related methods (C, Stop, removeTicker, checkForTick), and simplifies Tick/Set to only update time value.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant CC as CachedClock
    participant BG as Background Goroutine
    participant Ticker
    
    User->>CC: NewCachedClock(resolution)
    activate CC
    CC->>CC: Initialize with current time
    CC->>BG: Start background goroutine
    deactivate CC
    
    par Periodic Updates
        loop Every resolution interval
            Ticker->>BG: Tick
            BG->>CC: atomic.Store(nanos, time.Now().UnixNano())
        end
    and User Calls
        loop Concurrent access
            User->>CC: Now()
            CC->>CC: atomic.Load(nanos)
            CC-->>User: Return cached time
        end
    end
    
    User->>CC: Close()
    activate CC
    CC->>BG: Signal close
    CC->>Ticker: Stop()
    deactivate CC
    BG-->>BG: Exit goroutine
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

  • CachedClock goroutine safety: Verify atomic operations are correctly used; ensure no data races between background updates and Now() reads, and confirm proper cleanup on Close()
  • Interface contract changes: The removal of Ticker from the Clock interface is a breaking change; verify no unreviewed external code depends on NewTicker or that all internal usages have been addressed
  • TestClock simplification: Confirm that removal of ticker logic doesn't leave orphaned references or affected test utilities; verify Tick/Set simplification maintains expected semantics
  • Benchmark validity: Ensure benchmarks are representative and use proper timer reset/parallel patterns

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'perf: cached clock' directly summarizes the main change—adding a CachedClock for performance optimization.
Description check ✅ Passed The PR description covers what was changed (CachedClock addition, ticker removal), type of change, and testing instructions, mostly aligning with template requirements.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 11-11-perf_cached_clock

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 11, 2025

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between abee7f3 and c2ba107.

📒 Files selected for processing (6)
  • go/pkg/clock/cached_clock.go (1 hunks)
  • go/pkg/clock/cached_clock_test.go (1 hunks)
  • go/pkg/clock/clock_benchmarks_test.go (1 hunks)
  • go/pkg/clock/interface.go (0 hunks)
  • go/pkg/clock/real_clock.go (0 hunks)
  • go/pkg/clock/test_clock.go (4 hunks)
💤 Files with no reviewable changes (2)
  • go/pkg/clock/interface.go
  • go/pkg/clock/real_clock.go
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-05T15:19:50.563Z
Learnt from: imeyer
Repo: unkeyed/unkey PR: 3733
File: go/pkg/repeat/every_test.go:383-383
Timestamp: 2025-08-05T15:19:50.563Z
Learning: Go 1.24 introduced testing.B.Loop method which simplifies benchmark functions by handling iteration internally, ensuring setup/cleanup runs only once per benchmark count, and preventing compiler optimizations that could affect benchmark accuracy.

Applied to files:

  • go/pkg/clock/clock_benchmarks_test.go
🧬 Code graph analysis (3)
go/pkg/clock/cached_clock_test.go (2)
go/pkg/clock/cached_clock.go (2)
  • NewCachedClock (27-56)
  • CachedClock (14-19)
go/pkg/clock/interface.go (1)
  • Clock (12-18)
go/pkg/clock/cached_clock.go (1)
go/pkg/clock/interface.go (1)
  • Clock (12-18)
go/pkg/clock/clock_benchmarks_test.go (2)
go/pkg/clock/cached_clock.go (1)
  • NewCachedClock (27-56)
go/pkg/clock/real_clock.go (1)
  • New (18-20)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: PR title
  • GitHub Check: Test Go API Local / Test
  • GitHub Check: Test API / API Test Local
  • GitHub Check: Build / Build

Copy link
Member

@Flo4604 Flo4604 left a comment

Choose a reason for hiding this comment

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

lgtm but rabbit has to be resolved

@graphite-app
Copy link

graphite-app bot commented Nov 11, 2025

Illustrated gif. A hand appears and holds up an oversized thumb, giving us a thumbs up. (Added via Giphy)

@Flo4604 Flo4604 added this pull request to the merge queue Nov 11, 2025
@graphite-app
Copy link

graphite-app bot commented Nov 11, 2025

Graphite Automations

"Post a GIF when PR approved" took an action on this PR • (11/11/25)

1 gif was posted to this PR based on Andreas Thomas's automation.

Merged via the queue into main with commit 291a2c3 Nov 11, 2025
47 of 48 checks passed
@Flo4604 Flo4604 deleted the 11-11-perf_cached_clock branch November 11, 2025 18:16
@coderabbitai coderabbitai bot mentioned this pull request Nov 14, 2025
19 tasks
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