Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict: support efficient key+value iteration #518

Open
ash2k opened this issue Oct 30, 2023 · 1 comment
Open

Dict: support efficient key+value iteration #518

ash2k opened this issue Oct 30, 2023 · 1 comment

Comments

@ash2k
Copy link
Contributor

ash2k commented Oct 30, 2023

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 using Get(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.

@adonovan
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants