-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
unique: optimize interning of a byte slice #71926
Comments
Related Issues (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
This does not need to be a proposal. |
Also it would be nice if this worked with structs containing string, then net/netip could take use of that: Lines 1061 to 1063 in fba83cd
Lines 489 to 502 in fba83cd
|
If I understand correctly, in your example the allocation would happen already when calling zone := unique.Make(string(b[16:]))
*ip = AddrFrom16([16]byte(b[:16])).WithZone(zone.Value()) |
CC @mknyszek |
You can safely do: func makeUnique(data []byte) unique.Handle[string] {
return unique.Make(unsafe.String(&data[0], len(data)))
} We have a test for it, too. I've been meaning to document that |
Proposal Details
Currently the following function allocates a new string for the
string(data)
conversion even when the value already exists in the unique map.The built-in map has an optimization to not allocate a string for map lookups, this optimization would be similar.
It seems that during the discussions in the original
unique
proposal the assumptions were that the string conversion will not allocate in this case, but such behavior was not documented in the final proposed API.This proposal is to:
unique.Make(string(data))
allocates only when the value does not already exist in the unique map.unique
package to mention this special case.Such optimization would be useful for parsers that handle byte slices.
The text was updated successfully, but these errors were encountered: