4 ms·
> Maps are similar, but have internal data structures that require initialization, thus cannot be reliably used when zeroed. Its a performance thing, the map s
by bbatha 2y ago
> Maps are similar, but have internal data structures that require initialization, thus cannot be reliably used when zeroed.
Its a performance thing, the map structure requires a heap allocation even with zero elements. This is because the map structure is type generic without being fully monomorphized.
- 9rx 2y agoThe heap allocation could transparently happen at use. The only additional performance overhead that would bring is the conditional around if it has already been initialized or not, which is negligible for modern computers. Go is not obsessed with eking every last bit of performance anyway. But, that is not the convention. It is well understood that if a struct needs allocations that you must first call a constructor function (e.g. package.NewFoo). It is reasonable to expect that map would stick to the same convention.