valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.91k stars 1.76k forks source link

Improve the performance of ConnState.String() #1871

Closed ksw2000 closed 2 months ago

ksw2000 commented 2 months ago

Decrease the ConnState.String() runtime by 95%.

We can replace the map with []string like the code in https://github.com/valyala/fasthttp/blob/65e989e8b8bcf2541d840fcf27dcf8a11931b9db/status.go#L85-L151

func BenchmarkStatusToString(b *testing.B) {
    for i := 0; i < b.N; i++ {
        StateNew.String()
        StateActive.String()
        StateIdle.String()
        StateHijacked.String()
        StateClosed.String()
    }
}
goos: linux
goarch: amd64
pkg: github.com/valyala/fasthttp
cpu: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
                 │    old.txt    │           new.txt            │
                 │    sec/op     │    sec/op     vs base        │
StatusToString-8   18.9250n ± 0%   0.8873n ± 1%  -95.31% (n=50)

                 │  old.txt   │            new.txt             │
                 │    B/op    │    B/op     vs base            │
StatusToString-8   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=50) ¹
¹ all samples are equal

                 │  old.txt   │            new.txt             │
                 │ allocs/op  │ allocs/op   vs base            │
StatusToString-8   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=50) ¹
¹ all samples are equal
erikdubbelboer commented 2 months ago

Thanks!