Hash tables in Go and advantage of self-hosted compilers

https://news.ycombinator.com/rss Hits: 7
Summary

Recently, I was looking at the Go codebase that was using map[int]bool to track unique values. As some of you may know, Go has no set data structure. Instead, developers usually use hash maps (key-value data structure). The first idea that comes to mind is to use map[int]bool, where keys are integers and values are booleans. But experienced Go developers know that Go has an empty-struct type that can be used for maps: map[int]struct{}. The benefit of such a type is that it occupies zero bytes in memory; it's a so-called zero-sized type. The compiler knows this and uses it to your advantage when it can. In this case, it should omit storing values and keep only keys. So when I saw the bool struct, my first thought was to switch to map[int]struct{} to save some memory. In theory, that map can hold more than 100 000 integers. To my surprise, this change had no effect on memory consumption when running in production. Debugging the issue I double-checked that using struct{} should save memory in Google and with LLMs. It was, in fact, true. After a bit more searching, I found that since Go 1.24, there is a new map implementation called Swiss Tables. A lot of articles, including the official Go blog, say that the new implementation consumes less memory on average (for all use cases). If you specifically ask LLMs about Go 1.24 and map[int]struct{} (with web search enabled), they will assure you that using empty maps consumes less memory. So what's actually happening? What is self-hosted compiler? Not to be confused with self-hosting a compiler, a self-hosted compiler means that the compiler itself is written in the same language. So, the current compiler for Go is also written in Go. This has a big advantage. Since Go is a relatively simple language, we can quickly check how some of the parts are actually implemented. I had an experience of looking at Python's dict implementation, and I can say that Go's source code is much easier to understand. Especially given that it's ha...

First seen: 2025-12-20 12:27

Last seen: 2025-12-20 18:29