default := ...
v, ok := m[k]
if !ok {
m[k] = default
v = default
}
// Use v
This is a mapaccess2 followed (conditionally) by a mapassign. The beginning of mapassign is identical to mapaccess2: hashing the key and looking for an existing entry (which won't be found).
In theory, the compiler could detect this pattern and use a special call for the map assignment that avoids the duplicate work (this would effectively be internal/runtime/maps.(*table).uncheckedPutSlot in today's implementation).
Another pattern is iteration plus delete:
for k := range m {
if iDontLikeIt {
delete(m, k)
}
}
It could be optimized in a similar way. This is also maps.DeleteFunc which could be directly specialized more easily than adding new compiler optimizations.
This is a
mapaccess2followed (conditionally) by amapassign. The beginning ofmapassignis identical tomapaccess2: hashing the key and looking for an existing entry (which won't be found).In theory, the compiler could detect this pattern and use a special call for the map assignment that avoids the duplicate work (this would effectively be
internal/runtime/maps.(*table).uncheckedPutSlotin today's implementation).Another pattern is iteration plus delete:
It could be optimized in a similar way. This is also
maps.DeleteFuncwhich could be directly specialized more easily than adding new compiler optimizations.