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.94k stars 1.76k forks source link

Reduce the size of the Cookie by 32 bytes. #1866

Closed ksw2000 closed 2 months ago

ksw2000 commented 2 months ago

Replace the field bufKV with two []byte fields and remove the filed buf.

type Cookie struct {
-       buf    []byte    // -24 bytes
-       bufKV  argsKV    // -56 bytes
+       bufK   []byte    // +24 bytes
+       bufV   []byte    // +24 bytes
}

In the benchmark test, the execution time increased slightly by about 1%.

goos: linux
goarch: amd64
pkg: github.com/valyala/fasthttp
cpu: AMD EPYC 7763 64-Core Processor                
                       │   old.txt   │              new2.txt              │
                       │   sec/op    │   sec/op     vs base               │
CookieParseMin-4         26.55n ± 1%   26.53n ± 1%       ~ (p=0.733 n=20)
CookieParseNoExpires-4   89.89n ± 3%   91.65n ± 3%  +1.96% (p=0.033 n=20)
CookieParseFull-4        531.0n ± 3%   540.8n ± 1%       ~ (p=0.057 n=20)
geomean                  108.2n        109.6n       +1.24%

                       │   old.txt    │              new2.txt               │
                       │     B/op     │    B/op     vs base                 │
CookieParseMin-4         0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=20) ¹
CookieParseNoExpires-4   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=20) ¹
CookieParseFull-4        163.0 ± 0%     163.0 ± 0%       ~ (p=1.000 n=20) ¹
geomean                             ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                       │   old.txt    │              new2.txt               │
                       │  allocs/op   │ allocs/op   vs base                 │
CookieParseMin-4         0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=20) ¹
CookieParseNoExpires-4   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=20) ¹
CookieParseFull-4        4.000 ± 0%     4.000 ± 0%       ~ (p=1.000 n=20) ¹
geomean                             ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean
erikdubbelboer commented 2 months ago

Thanks!