-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: language: allow getting capacity of map? #52157
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
Comments
I think I'd rather just make maps shrinkable. |
I think both cases would be useful. Allowing cap() on maps would make Go more consistent and easier to learn, even if it is only informative. And making maps shrink automatically is also a useful feature but that is what issue #20135 is for. |
It's hard to explain what cap means at all. It's a rough estimate of the size of the underlying storage and that's about it. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
I couldn't find the discussion that Russ mentioned (but I think I know the gist). I still think this is a worthwhile idea. An alternative would be to add a builtin function as part of the unsafe "package". |
@AndrewWPhillips Sorry, this proposal is closed. We aren't going to reopen it without new information that we didn't previously consider. |
I don't know if this meets the threshold for "new information" sufficient to reopen the issue, but I've just run into this in the following scenario.... Developing a module which internally uses a map in a struct type that is initialised using a func where the caller indicates a desired capacity for the contained map. Without the ability to determine the capacity of the newly initialised map, it is not possible to write a unit test to verify that the initialisation function returned a correctly initialised struct. For all other types where I cannot conceive of any problem that addressing this omission/exception would create and would appreciate consideration being given to re-opening the proposal. |
I uphold |
A little story about why I'd have found it useful to have |
Maybe the new map implementation can avoid this problem? |
Swiss tables will not make the clear-is-expensive-on-large-capacity-maps problem any better. The new map implementation also makes the proposed |
I posted a quiz on Twitter:
Only 21% of people got it right, that it fails to compile with:
It's a little weird that you can kinda set the capacity of a map (a hint) but you can't get it back out. (Where "it" is really an "optional capacity hint")
I imagine the reason is the same reason that the previous proposal to automatically round up slice capacities to the nearest slab class was declined: it binds us to an implementation, or at least leaks their details that we'd be locked into.
Filing a bug in any case, as I can't find any past discussions of it on the issue tracker, and don't remember hearing any discussions elsewhere.
My motivation, FWIW, was to periodically re-make a map representing a set of work to do when it dropped back down to
len(m) == 0
. But I only wanted to re-make
it if the underlying hash map capacity had grown enough to be worthwhile. Of course that's all just a workaround for #20135 (maps not shrinking). And that means I wanted more than just the initial capacity hint; I wanted to see the underlying capacity change as a function of insertions into the map.The text was updated successfully, but these errors were encountered: