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

collections: Use Go 1.23 iterator patterns instead of exposing internals #35426

Closed
wants to merge 1 commit into from

Conversation

apparentlymart
Copy link
Contributor

In the first round of collections we exposed the internals of Set and Map as a pragmatic concession to allow efficiently iterating over the elements.

Go 1.23 is planned to stabilize "range-over-func" (golang/go#61405) and package iter (golang/go#61897) which together allow us to expose an efficient iteration API without exposing the internal representations of these collections.

The benefit for Set is pretty marginal, but the benefit for Map is more substantial: it avoids the oddity of exposing the MapElem values directly and instead returns the key and value directly so that usage is very similar to iterating over a built-in map.

I tried this now because I wanted to confirm that we could indeed retrofit these collection types with an iterator without too much disruption, and that seems to be true. We aren't using Go 1.23 yet and so we can't merge this, but I tested it using Go 1.22 and GOEXPERIMENT=rangefunc and it seems to work just fine. I expect we will be able to merge a PR like this shortly after adopting Go 1.23, which isn't yet released at the time I'm writing this but is likely to arrive in August.

internal/collections/map.go Outdated Show resolved Hide resolved
@apparentlymart
Copy link
Contributor Author

I'm no longer at HashiCorp after today, so I won't be able to work on this any further but I'm going to leave it open to keep it visible in case someone else wants to take over this branch in a new PR.

In the first round of collections we exposed the internals of Set and Map
as a pragmatic concession to allow efficiently iterating over the elements.

Go 1.23 stabilizes "range-over-func" and package iter which together allow
us to expose an efficient iteration API _without_ exposing the internal
representations of these collections.

The benefit for Set is pretty marginal, but the benefit for Map is more
substantial: it avoids the oddity of exposing the MapElem values directly
and instead returns the key and value directly so that usage is very
similar to iterating over a built-in map.
Copy link
Contributor

github-actions bot commented Nov 4, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants