Go Performance – A Checklist

Avoid Global Mutex

Don’t use a global mutex, keep it tied to structures. Go doesn’t have GIL and it is tempting to create a global mutex to simulate a similar behavior but then why use Go in the first place? You can tie a mutex to a structure to keep its impact as low as possible.

Avoid Defer Functions

Deffer functions are significantly expensive than the non-deferred ones. In the case of static functions, an improved version is compiled but it is still slower than direct functions.

Avoid recreate, reuse Variables

It is a usual way to create a variable in a loop. It takes an extra CPU cycle as well as it would keep Garbage Collection busy. Instead, use sync.Pool