Skip to content

Conversation

vasayxtx
Copy link
Contributor

@vasayxtx vasayxtx commented Aug 1, 2025

  • Only create time.NewTimer() when actually needed to wait
  • Reduces allocations from 7 to 4 and memory usage by ~54%
  • Improves performance by ~50% in high-throughput scenarios

Benchmark results:

Before:

goos: darwin
goarch: arm64
pkg: github.com/go-chi/chi/v5/middleware
cpu: Apple M1 Max
BenchmarkThrottle
BenchmarkThrottle-10    	 3349486	       382.3 ns/op	     456 B/op	       7 allocs/op
PASS

After:

goos: darwin
goarch: arm64
pkg: github.com/go-chi/chi/v5/middleware
cpu: Apple M1 Max
BenchmarkThrottle
BenchmarkThrottle-10    	 6310284	       186.9 ns/op	     208 B/op	       4 allocs/op
PASS

- Only create time.NewTimer() when actually needed to wait
- Reduces allocations from 7 to 4 and memory usage by ~54%
- Improves performance by ~50% in high-throughput scenarios
Copy link
Contributor

@VojtechVitek VojtechVitek left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you!

I'll wait few more days before merging to allow more 👀 reviews

Copy link
Contributor

@VojtechVitek VojtechVitek left a comment

Choose a reason for hiding this comment

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

LGTM

@VojtechVitek VojtechVitek merged commit cf537d4 into go-chi:master Aug 26, 2025
10 checks passed
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