Summary “Squeezing a go function” FOSDEM 04.02.2023 by Jesús Espino, Mattermost
Optimize what you need when you need. Don’t over optimize.
Don’t guess. Measure everything and optimize based on data.
go benchmark — built in with go, like tests.
“go test -bench .”
It’s possible to report allocations from benchmark context.
Usually first you profile and then benchmark parts.
But in these examples we do first benchmark with output and then profiling on this output.
Reducing cpu usage
Return faster for example.
Pre sized slice is about the same of speed, but has less allocations and memory usage. Array even faster.
A lot of variables are less effective then they packed in struct in terms of memory used.
Aligned functions are faster.
Pass by value copies value in stack and there is no allocations.
Escape analysis and function inlining
Combining both you achieve less allocations and less time spend.
Goroutines are lightweight, but not free.
If you run them on more than cpu cores and workload is heavy, whole performance will suck and generate a lot of allocations and memory usage.
Link to the page where video and presentation will be added https://fosdem.org/2023/schedule/event/gosqueezingfunction/