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

Optimize counter inc-by-one #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Nov 12, 2024

  1. Optimize counter inc-by-one

    Conceivably increment-by-one is by far the most common operation on counters.
    This PR optimize such operation, making it alloc-free.
    
    Benchmark with:
    ```
    func BenchmarkInc(b *testing.B) {
    	b.Run("Increment", func(b *testing.B) {
    		w := writer.MemoryWriter{}
    		id := NewId("foo", nil)
    		c := NewCounter(id, &w)
    		b.ReportAllocs()
    		b.ResetTimer()
    		for i := 0; i < b.N; i++ {
    			c.Increment()
    		}
    	})
    }
    ```
    
    before optimization:
    ```
    goos: darwin
    goarch: arm64
    pkg: github.com/Netflix/spectator-go/v2/spectator/meter
    cpu: Apple M3 Pro
    BenchmarkInc/Increment-12               10298648               115.2
    ns/op           120 B/op          3 allocs/op
    PASS
    ok      github.com/Netflix/spectator-go/v2/spectator/meter      2.485s
    ```
    
    after optimization:
    ```
    goos: darwin
    goarch: arm64
    pkg: github.com/Netflix/spectator-go/v2/spectator/meter
    cpu: Apple M3 Pro
    BenchmarkInc/Increment-12               40374824                31.44
    ns/op           97 B/op          0 allocs/op
    PASS
    ok      github.com/Netflix/spectator-go/v2/spectator/meter      3.563s
    ```
    Ling Yuan committed Nov 12, 2024
    Configuration menu
    Copy the full SHA
    efb742b View commit details
    Browse the repository at this point in the history