-
Notifications
You must be signed in to change notification settings - Fork 18k
maps: make Equal O(1) for pointer-identical maps #71388
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
Related Issues
Related Documentation (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
What if a map maintained a |
The NaN could be buried within the tree of aggregate (array/struct) values of the key, and the information that hashing encountered a NaN is not returned by the hash algorithm. Annoyingly, for Equal, one must also recursively search the values for NaNs. Simpler to go by types. The rtype for the keys and values could record a statically computed hasFloat bit so that no recursion is required at runtime. |
The old non-Swiss map type recorded But does this case come up a lot in practice? |
In my use-case, I was doing a copy-on-write mechanism where I needed to detect if something changed. It is quite likely that I could be comparing a map to itself. |
The same is for |
Fortunately for slices, it is possible for users to perform this optimization themselves with something like: len(x) > 0 && len(x) == len(y) && &x[0] == &y[0] This doesn't handle NaNs "correctly", but honestly most usages do not care. |
Go version
go1.23
Output of
go env
in your module/workspace:What did you do?
Ran this benchmark:
What did you see happen?
What did you expect to see?
As a trivial test, Equal could use unsafe to check whether the map pointers are identical.
In which case, the maps are equal.
Unfortunately, we need to skip this optimization if the key and value types could possibly contain floating point values, which violate the reflexive property of equality for NaNs.
The text was updated successfully, but these errors were encountered: