PaulSonOfLars / gotgbot

Autogenerated Go wrapper for the telegram API. Inspired by the python-telegram-bot library.
MIT License
509 stars 114 forks source link

optimisation of GC for heavily-accessed structures and small linting #77

Closed vassilit closed 1 year ago

vassilit commented 1 year ago

The Go garbage collector scans for pointers to free referenced objects in every allocated structures up to the last field that potentially reference something.

By moving reference field to the beginning of the structure this allows the GC to fetch less often from memory as they can fetch the whole referenced part of the structure in one operation if it fits the CPU cacheline size

For example, handlers.Conversation became a consolidated 48 bytes of pointers instead of 72 bytes. If we follow 10 000 conversations this may well reduce the GC interruption time.

Of course, they are numerous factors (like a change of the Go runtime GC implementation in the future) that can make this optimisation useless, but as far as Go 1.19, there is no harm to try.

Others seldom allocated structures are not pointer-aligned, but as changing their order may break users using unnamed fields, better not to change them.

vassilit commented 1 year ago

«Premature optimization is the root of all evil»