Skip to Content
marmot

Status: proposal, not implemented. Reviewer: frontend (map lowering, codegen fast paths). Verify (iteration and GC rules).

Goal

A growing map should never hold two copies of its whole table. Today a marmot map is one swiss table that doubles. While it rebuilds, the old and the new table are both resident. mapint has 36 MB old and 71 MB new at the last rebuild. Its peak RSS is 107 MB after the early free of old tables, against 95 to 100 MB for go. Each rebuild also moves every entry at once, a pause that grows with the map.

Design (as Go 1.24's internal/runtime/maps)

  • A map is a directory of table pointers, indexed by the top bits of the hash (globalDepth bits). Each table holds at most 1024 slots (128 groups of 8) and has a localDepth. Several directory entries can point to one table while localDepth < globalDepth.
  • A small map (at most 8 entries) keeps one group and no directory, as today's first group.
  • Growth of a full table: if it has fewer than 1024 slots, it doubles in place (as today, but bounded). At 1024 slots it splits into two tables by the next hash bit. When localDepth reaches globalDepth, the directory doubles (pointers only).
  • The transient copy is at most one 1024-slot table (for map[int]int: 1024 x 16 bytes plus control bytes, about 17 KiB), not the whole map.
  • Lookup: hash, directory index from the top bits, then the same group probe as today inside the table. One extra load (the directory entry) per operation.

Iteration

Go's rules: every entry present for the whole iteration is produced once. Entries added may or may not be produced. With tables that split, an iterator walks directory entries in order. It skips entries that point to a table it already visited (same table pointer, localDepth). A table that splits during the iteration: the iterator keeps its old table pointer. As today, it looks each key up in the current map, to see whether the key still exists and to get its current element. The old table is then freed only when no iteration ever started (the rule of 993e8863), else at a collection.

Costs and risks

  • One more load per lookup and a directory index computation. Measure on mapint, mapstr, mapstruct, wordfreq and the map-heavy tests/run programs.
  • map.go's table layer is about 500 lines. The codegen fast paths call assign32/assign64/assignstr/assign128 and the access functions, whose signatures stay the same.
  • Iteration across splits is the risky part. Tests: inserts during an iteration that split tables (the shape of map_grow_free), and deletes and clear during an iteration. Also maps.Clone during growth, reflect.MapIter, and the existing map tests in tests/run.

Measurements before a default flip

  • Peak RSS and CPU of the map rows against today and against go, perfcmp in dev, O2 and release, conformance including gcstress and cards.
  • Expected: mapint about 107 to 75 to 85 MB (the old table at the last rebuild is no longer resident), mapstr similar. CPU within noise (one extra load, less copying).

Alternative considered

  • Incremental rehash of one big table, which moves a few groups per insert, as Go before 1.24 did with buckets. It keeps both arrays resident for the whole growth, so there is no memory gain. Dropped.

Review (frontend, 2026-11-17)

  • The code generator depends only on three things. They are the runtime map function names and signatures, and runtime.hiter (one stack slot per range loop: keep it small). The third is the rule that the slot that mapassign returns stays valid until the next write (map fusion). The design keeps all three.
  • Watch: the directory index bits and the group probe bits must not overlap. reflect.MapIter and maps.Clone linknames. Hiter key and elem pointers must point into the table the iterator holds.
  • Measure tuios startup too: its large map literals split tables during package init.

On this page

Was this page helpful?

Report an issueOpen issues