cloudflare / goflow

The high-scalability sFlow/NetFlow/IPFIX collector used internally at Cloudflare.
BSD 3-Clause "New" or "Revised" License
852 stars 171 forks source link

Reduce allocs #116

Open kbatyr opened 1 year ago

kbatyr commented 1 year ago

Refactor goflow package to escape allocations while parsing NetFlow/IPFIX packets. Benchmark results before refactoring:

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
BenchmarkNetFlow2-8        54494         22757 ns/op       15512 B/op        400 allocs/op

And after refactoring:

goos: linux
goarch: amd64
pkg: github.com/cloudflare/goflow/v3/parsers
cpu: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
BenchmarkNetFlow1-8              2067685               529.6 ns/op           194 B/op          6 allocs/op
BenchmarkNetFlow2-8               313855              3765 ns/op             128 B/op          4 allocs/op
BenchmarkNetFlow3-8               377473              3141 ns/op               0 B/op          0 allocs/op
BenchmarkNetFlowIPFix1-8         1987146               571.7 ns/op            32 B/op          1 allocs/op
BenchmarkNetFlowIPFix2-8         3976148               277.8 ns/op            64 B/op          1 allocs/op
BenchmarkNetFlowIPFix3-8         3392520               306.3 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/cloudflare/goflow/v3/parsers 10.248s

P.S. Cases with allocations occurs when we get new template or in error cases (template not found, etc.) by calling NewError().