Skip to content

reflect: add MapIter.Reset #46293

@josharian

Description

@josharian

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()

cc @bradfitz @randall77

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions