Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local counter: Don't re-allocate maps in Go 1.21+ #40

Merged
merged 1 commit into from
Jul 25, 2024
Merged

Conversation

VojtechVitek
Copy link
Contributor

@VojtechVitek VojtechVitek commented Jul 25, 2024

Yet another performance improvement for Go 1.21+. Reuse maps backing the sliding windows to avoid map re-allocation, resizing and garbage collection when shifting the windows every second (or minute).

Speeds up throughput by 2.67x and reduces memory allocations by 2x on Go 1.21+ 🚀

$ benchstat old.txt new.txt
goos: darwin
goarch: arm64
pkg: github.com/go-chi/httprate
               │   old.txt   │               new.txt               │
               │   sec/op    │   sec/op     vs base                │
LocalCounter-8   34.65m ± 3%   12.99m ± 2%  -62.52% (p=0.000 n=10)

               │   old.txt    │               new.txt                │
               │     B/op     │     B/op      vs base                │
LocalCounter-8   4.384Mi ± 0%   2.846Mi ± 0%  -35.07% (p=0.000 n=10)

               │   old.txt   │               new.txt               │
               │  allocs/op  │  allocs/op   vs base                │
LocalCounter-8   253.9k ± 0%   121.6k ± 0%  -52.12% (p=0.000 n=10)

(The CI runs Go 1.17, so we won't see any difference there.)

Copy link

Benchmark Results

goos: linux
goarch: amd64
pkg: github.com/go-chi/httprate
cpu: AMD EPYC 7763 64-Core Processor                
               │ master.txt  │            pr.txt             │
               │   sec/op    │   sec/op     vs base          │
LocalCounter-4   19.67m ± 1%   19.53m ± 1%  ~ (p=0.143 n=10)

               │  master.txt  │             pr.txt             │
               │     B/op     │     B/op      vs base          │
LocalCounter-4   2.833Mi ± 0%   2.832Mi ± 0%  ~ (p=0.796 n=10)

               │ master.txt  │            pr.txt             │
               │  allocs/op  │  allocs/op   vs base          │
LocalCounter-4   121.5k ± 0%   121.4k ± 0%  ~ (p=0.810 n=10)

@VojtechVitek VojtechVitek merged commit 6aa26b0 into master Jul 25, 2024
3 checks passed
@VojtechVitek VojtechVitek deleted the eGGyVKZH branch July 25, 2024 18:30
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