-
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
runtime: performance of map[string(bytes)]++ #45021
Comments
One option would also be to avoid slicebytetostring when writing the longer form of the assignment:
While this is not as convenient, it still would allow to write the code without the conversion. |
The map functions themselfs do not make calls to slicebytetostring but that is done before passing the string to them. If this is really important for optimisation I see two possible improvements:
Personally I am not a fan of adding even more copies of map functions to the runtime for special string variants. Maybe it would at that point be better to allow byte slices as map keys (not using cap for key comparison and hashes) and making byte slice copies on demand in specialised byte slice map runtime functions. |
cc @randall77 |
Dup of #5147 ? |
While #5147 if implemented would give one option to avoid an allocation (for existing keys) by rewriting |
This seems reasonable to me. We can have (Incidentally, I thought we used to have the semantics that if we overwrote a string key in a map, we retained the new string instead of the old one? It doesn't look like that's currently the case. Am I misremembering that and/or misreading the current code?) |
That sounds right. I think the current code does that, look for needkeyupdate. |
Looks like it: https://play.golang.org/p/M6JXPe8-EH4 On the upside, that means we can be assured that this optimization won't break anyone expecting |
Currently using
map[string]*int
to count[]byte
values is significantly faster than usingmap[string]int
. This came up in https://benhoyt.com/writings/count-words/ post. It would be nicer ifmap[string]int
was as fast or faster.To demonstrate the issue:
Outputs:
The underlying issue seems to be that, in the
map[string]*int
case the code ends up calling firstmapaccess2_faststr
(for the common case) and thenmapassign_faststr
. Whereasmap[string]int
only callsmapassign_faststr
. See https://go.godbolt.org/z/j6PY77.mapaccess2_faststr
currently does not cause a call toslicebytetostring
, howevermapassign_faststr
does, which means that withmap[string]int
it ends up doing bunch of conversions that are unnecessary.The text was updated successfully, but these errors were encountered: