Skip to content

Conversation

@analytically
Copy link
Contributor

@analytically analytically commented Oct 1, 2025

Replace time.Time with int64 nanotime values for expiration tracking, simplifying TTL checks and reducing memory footprint. 19-30% faster.

Performance improvements (benchstat with n=3 on Apple M1):

  • LRU_Rand_NoExpire: 219.6ns → 173.9ns (-20.8%)
  • LRU_Freq_NoExpire: 218.5ns → 176.2ns (-19.4%)
  • LRU_Rand_WithExpire: 251.8ns → 175.4ns (-30.3%)
  • LRU_Freq_WithExpire: 250.1ns → 176.7ns (-29.3%)

Changes:

  • Use runtime.nanotime() via //go:linkname for fastest monotonic time
  • Store expiration as int64 nanoseconds instead of time.Time
  • Consolidate type assertions (2+ calls → 1 per operation)
  • Replace time.After() with direct integer comparison
  • Remove GetExpiration()

Benefits:

  • Performance: 20-30% faster across all operations
  • Memory: 16 bytes saved per entry (67% reduction in time storage)
  • Correctness: Immune to system clock adjustments (NTP, DST, manual changes)
  • Throughput: Increased from ~4.6M to ~5.7M ops/sec per goroutine

Note: runtime.nanotime() is an internal Go API that may change in future
versions. Trade-off accepted for significant performance and correctness benefits.

…d type assertions

Replace time.Time with int64 nanotime values for expiration tracking,
simplifying TTL checks and reducing memory footprint. 19-30% faster.

Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>
@analytically analytically requested a review from umputun as a code owner October 1, 2025 17:34
@analytically
Copy link
Contributor Author

@paskal could you review?

@umputun umputun requested review from Copilot and paskal October 1, 2025 17:50
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 optimizes cache performance by replacing time.Time with int64 nanotime values for TTL expiration tracking, achieving 19-30% performance improvements across all operations.

Key changes:

  • Use runtime.nanotime() via //go:linkname for fastest monotonic time access
  • Store expiration as int64 nanoseconds instead of time.Time structures
  • Consolidate type assertions to reduce redundant operations

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

File Description
v3/cache.go Core optimization replacing time.Time with int64 nanotime, consolidating type assertions, and updating all expiration logic
v3/options.go Convert TTL parameter from time.Duration to int64 nanoseconds
v3/go.mod Update Go version to 1.25 and remove unused dependency
v3/cache_test.go Remove unused import and test function for removed dependency

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.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