-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
reflect: add Value.Clear #55002
Comments
cc @dsnet |
Interesting observation about NaN. It does mean that the documentation on // Clear removes all entries from m, leaving it empty.
func Clear[M ~map[K]V, K comparable, V any](m M) since it doesn't always leave it empty. \cc @ianlancetaylor |
FWIW that observation was made by @randall77 here, I'm not taking credit. |
Suddenly I like #20660. |
It's definitely a little odd to add something to reflect that you can't do in the language proper. Seems like we should fix that somehow first. |
This proposal has been added to the active column of the proposals project |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@rsc For my purposes, I'd also be okay with having v.MapClear()
if v.Len() > 0 { // there where some NaNs in that map
v.Set(reflect.New(v.Type()).Elem())
} That way, |
There are never any good answers involving NaNs. |
Personally, I would like something like that. However, this does open the door to other functionality in For example, #54454 proposes |
An arbitrary suggestion: Then adding |
That's kind of just kicking the can down the road one step. I would argue that I'm ok with |
One possible halfway point would be to have |
A bit semantically strange, the existence of map.Clear that wouldn't actually clear the map. If there was such an operation, I would expect it to work on clearable maps, i.e. where map keys are proven to have reflexive comparison. Otheriwse that could easily leak into Set types for example, especially when computing cardinality. (sets of floats or floats composites). But then if it requires modifying map.Clear from an x package, how many programs it would break is another question too perhaps. [edit] to be fair, I'm not sure that the true semantics of map clearing have to rely on reflexive comparability. In that case, assistance from the runtime would be relevant. But then there is still a similar issue for user-defined datastructures. Leaving the NaNs and related in could also leak memory no? |
@bcmills to me, that feels like the kind of perfect compromise in that it leaves everyone unhappy. Especially as it doesn't seem possible to check if |
I find the nuance in this discussion quite interesting, but I thought I'd point out that I have never in eight years of writing Go used |
Me neither, but if the issue appears, it might appear for any type which is a composite of floats values. Not just directly float32 or float64. What happens if a pointer value is stored for a struct key where one field is a NaN float64 for instance? Isn't it now dangling? |
@Merovius, while it's true that one couldn't always check for the panic locally immediately before making the call to |
@bcmills I don't like that. As a library author using Not to mention that checking if an inserted value has this problem isn't always easy. @mvdan I tend to agree. I feel that right now, this issue - at least as far as NaN is concerned - is simply blocked on #20660 - i.e. "somehow decide what to do about NaN in map keys":
|
Change https://go.dev/cl/457895 mentions this issue: |
Ops, the issue is not done yet, need to wait for compiler implementation. |
Ah, this proposal is for reflect package, so should be closed after https://go.dev/cl/457895 merged. |
Fixes golang#55002 Change-Id: I7d0f14cc54f67f2769b51d2efafc4ae3714f0e3d Reviewed-on: https://go-review.googlesource.com/c/go/+/457895 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
Change https://go.dev/cl/481935 mentions this issue: |
So iterators that are in progress can know entries have been deleted and terminate the iterator properly. Update #55002 Update #56351 Fixes #59411 Change-Id: I924f16a00fe4ed6564f730a677348a6011d3fb67 Reviewed-on: https://go-review.googlesource.com/c/go/+/481935 Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
Change https://go.dev/cl/498757 mentions this issue: |
Added Value.Clear, deprecated SliceHeader and StringHeader. For #55002 For #56906 Change-Id: Ib7497aff830d56fad90c31ec28596e71a448e9ce Reviewed-on: https://go-review.googlesource.com/c/go/+/498757 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Eli Bendersky <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
I propose adding a new method
MapClear
toreflect.Value
, which efficiently deletes all elements from a map.I am working on a decoder for a serialization format. If I want decoding into a non-nil map to leave it with only the serialized elements, I currently have two choices: 1. Allocate a new map, or 2. Clear all keys.
Clearing the keys has the advantage that it re-uses existing storage, if any, so it is lighter on allocations. But it takes more time to iterate over the map and clear keys. It also isn't correct, strictly speaking, as it doesn't behave right with NaN-keys.
It would be preferable to have a way to efficiently clear the map, preserving the already allocated buckets. This isn't an operation available in the Go language, so arguably it does not belong in
reflect
either. But we have #20138 to at least make the iteration-and-deletion idiom transparently efficient in plain Go.The text was updated successfully, but these errors were encountered: