Permalink
Browse files

runtime: limit the number of map overflow buckets

Consider repeatedly adding many items to a map
and then deleting them all, as in #16070. The map
itself doesn't need to grow above the high water
mark of number of items. However, due to random
collisions, the map can accumulate overflow
buckets.

Prior to this CL, those overflow buckets were
never removed, which led to a slow memory leak.

The problem with removing overflow buckets is
iterators. The obvious approach is to repack
keys and values and eliminate unused overflow
buckets. However, keys, values, and overflow
buckets cannot be manipulated without disrupting
iterators.

This CL takes a different approach, which is to
reuse the existing map growth mechanism,
which is well established, well tested, and
safe in the presence of iterators.
When a map has accumulated enough overflow buckets
we trigger map growth, but grow into a map of the
same size as before. The old overflow buckets will
be left behind for garbage collection.

For the code in #16070, instead of climbing
(very slowly) forever, memory usage now cycles
between 264mb and 483mb every 15 minutes or so.

To avoid increasing the size of maps,
the overflow bucket counter is only 16 bits.
For large maps, the counter is incremented
stochastically.

Fixes #16070

Change-Id: If551d77613ec6836907efca58bda3deee304297e
Reviewed-on: https://go-review.googlesource.com/25049
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
  • Loading branch information...
josharian committed Jul 19, 2016
1 parent 0cd3ecb commit 9980b70cb460f27907a003674ab1b9bea24a847c
Showing with 212 additions and 78 deletions.
  1. +12 −10 src/cmd/compile/internal/gc/reflect.go
  2. +170 −62 src/runtime/hashmap.go
  3. +30 −6 src/runtime/hashmap_fast.go
@@ -182,20 +182,22 @@ func hmap(t *Type) *Type {
}
bucket := mapbucket(t)
var field [8]*Field
field[0] = makefield("count", Types[TINT])
field[1] = makefield("flags", Types[TUINT8])
field[2] = makefield("B", Types[TUINT8])
field[3] = makefield("hash0", Types[TUINT32])
field[4] = makefield("buckets", Ptrto(bucket))
field[5] = makefield("oldbuckets", Ptrto(bucket))
field[6] = makefield("nevacuate", Types[TUINTPTR])
field[7] = makefield("overflow", Types[TUNSAFEPTR])
fields := []*Field{
makefield("count", Types[TINT]),
makefield("flags", Types[TUINT8]),
makefield("B", Types[TUINT8]),
makefield("noverflow", Types[TUINT16]),
makefield("hash0", Types[TUINT32]),
makefield("buckets", Ptrto(bucket)),
makefield("oldbuckets", Ptrto(bucket)),
makefield("nevacuate", Types[TUINTPTR]),
makefield("overflow", Types[TUNSAFEPTR]),
}
h := typ(TSTRUCT)
h.Noalg = true
h.Local = t.Local
h.SetFields(field[:])
h.SetFields(fields)
dowidth(h)
t.MapType().Hmap = h
h.StructType().Map = t
Oops, something went wrong.

0 comments on commit 9980b70

Please sign in to comment.