Dict: support efficient key+value iteration
ash2k opened this issue · comments
I'd like to go over keys and values in a Dict, process them and put results somewhere else. I see two options:
Iterate() Iterator
allows me to go over keys. I can gen get the corresponding values usingGet(Value)
. This is suboptimal because of the extra hashing+lookup costs.Items() []Tuple
allows me to get all keys+values in one go. This is suboptimal because of the extra allocation to hold all the data.
I'd like a more efficient way that avoids the above issues.
It's a breaking change to introduce a new method to Iterator
to get the value of the mapping (it iterates over keys for Dict
) so perhaps we can have a new interface (e.g. KeyValueIterator
/MappingIterator
) with one more extra method (or a method to get both values in one go)? And then perhaps one more interface to extend IterableMapping
with a method to get the new kind of iterator (e.g. IterateKeysAndValues() KeyValueIterator
or IterateMapping() MappingIterator
).
keyIterator
already has the value in the entry
, it just lacks a way to get it.
Go itself seems likely to add support for language-level iterators which will open up many exciting possibilities for the Starlark API. For example, it may be possible to say for k, v := range dict.All { ... }
without the need for an explicit iterator, or a call to iter.Done
, and without allocating an array proportional to dict.Len()
.
I think we should wait to see what happens there before committing to new API in Starlark.