-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Description
Currently, iterating over a map requires two allocations: A reflect.MapIter and a runtime.hiter. By embedding a runtime.hiter (or rather, a type with the same shape and size) in a reflect.MapIter, it is possible to eliminate the second allocation. If we could re-use a reflect.MapIter to iterate over multiple maps, then we could eliminate the first allocation as well.
I propose that we add:
// Reset changes iter to iterate over v.
// It panics if v's Kind is not Map and v is not the zero Value.
func (iter *MapIter) Reset(v Value)
Allowing v to be the zero Value enables the caller to avoid pinning whatever map iter was previously iterating over.
Note that these two would be equivalent:
iter := new(reflect.MapIter)
iter.Reset(mapVal)
iter := mapVal.MapRange()
dsnet, bradfitz, komuw, kortschak, bokwoon95 and 3 more