-
Notifications
You must be signed in to change notification settings - Fork 18.1k
unique: optimize interning of string([]byte) #71926
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 (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 |
Change https://go.dev/cl/671955 mentions this issue: |
Change https://go.dev/cl/672135 mentions this issue: |
….Make unique.Make always copies strings passed into it, so it's safe to not copy byte slices converted to strings either. Handle this just like map accesses with string(b) as keys. This CL only handles unique.Make(string(b)), not nested cases like unique.Make([2]string{string(b1), string(b2)}); this could be done in a followup CL but the map lookup code in walk is sufficiently different than the call handling code that I didn't attempt it. (SSA is much easier). Fixes golang#71926 Change-Id: Ic2f82f2f91963d563b4ddb1282bd49fc40da8b85 Reviewed-on: https://go-review.googlesource.com/c/go/+/672135 Reviewed-by: David Chase <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
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: