How Swiss tables work in Go built-in map
summary
Go 1.24 replaces its old map implementation with Swiss Tables to reduce pointer chasing and improve lookup speeds. This new design utilizes control bytes and SIMD instructions to scan multiple slots simultaneously, significantly decreasing the time spent on key equality checks. By organizing storage into groups and tables that grow independently, the runtime achieves better CPU cache locality and more efficient memory redistribution during growth.
key points
- Swiss Tables use a 7-bit hash (H2) stored in control bytes, allowing the runtime to use SIMD instructions to identify potential key matches across eight slots at once without reading the full keys.
- The map structure evolves from a single group to a table-based system that uses a triangular probe sequence to resolve collisions, avoiding the long overflow bucket chains of previous versions.
- To prevent expensive global redistributions, Go employs a directory system with global and local depths, allowing individual tables to split and grow without requiring all other tables to be rebuilt.
who should read it
Software engineers and Go developers who want to understand the internal memory layout and performance optimizations of the Go runtime.
discussion positive
consensus
Commenters agree that Swiss Tables offer high performance but face adoption hurdles in languages like Python and Ruby due to requirements for preserving insertion order. There is a general understanding that Go's delayed adoption was due to the language's age and specific implementation challenges. The sentiment is technically curious, with a focus on the trade-offs between performance and specific language guarantees.
top insight
Python's 'dict' preserves insertion order because of a historical transition from a poor hash table implementation to a faster 'OrderedDict', which became the standard. This architectural promise prevents a simple swap to Swiss Tables, as the latter does not inherently support ordering. This highlights how user-facing API guarantees can lock a language into specific internal data structures.
Generated by an LLM from the article and thread. Check the sources before relying on it.