Summary “Squeezing a go function” FOSDEM 04.02.2023 by Jesús Espino, Mattermost

Nicolai Antiferov
1 min readFeb 4, 2023

Optimize what you need when you need. Don’t over optimize.

Don’t guess. Measure everything and optimize based on data.

Benchmarks

go benchmark — built in with go, like tests.

“go test -bench .”

It’s possible to report allocations from benchmark context.

Profiling

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.

Reduce allocations

Pre sized slice is about the same of speed, but has less allocations and memory usage. Array even faster.

Packing

A lot of variables are less effective then they packed in struct in terms of memory used.

Function inlining

Aligned functions are faster.

Escape analysts

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.

Concurrency

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.

References

Link to the page where video and presentation will be added https://fosdem.org/2023/schedule/event/gosqueezingfunction/

--

--