Skip to content
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

cmd/compile: allocation when string key already exists in a map when using map[string([]byte{})] #52971

Closed
renthraysk opened this issue May 18, 2022 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@renthraysk
Copy link

What version of Go are you using (go version)?

$ gotip version
go version devel go1.19-41b9d8c75e Tue May 17 03:26:28 2022 +0000 linux/amd64
$ go version
go version go1.18.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

Modify an existing string key value within a map, causes an allocation when using string([]byte{}) conversion, whereas using a string direct doesn't.

https://go.dev/play/p/q5XXrMngkZV

What did you expect to see?

0 allocations for when using map[string([]byte{})] when the map contains the key.

What did you see instead?

1 allocation using map[string([]byte{})] when the map contains the key.

@randall77
Copy link
Contributor

m[string(b)] is allocation free only on map lookup. It is not allocation free on map assignment.
That's because we need to store the key if it isn't in the map yet.
Conceivably we could allocate only when the insert actually inserts (as opposed to updates), but actually inserting is probably the common case.

@renthraysk
Copy link
Author

renthraysk commented May 18, 2022

Yeah, figured just surprised it didn't use the already available key.
Idea was to see if it was worth pre-allocating a map with common keys.

buf := make([][]byte, 32)
m := map[string][][]byte{
    "Accept": buf[0:0:1],
    "Accept-Charset": buf[1:1:2], 
    ...
} 

@randall77 randall77 added this to the Backlog milestone May 18, 2022
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 18, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@renthraysk
Copy link
Author

Was raised again in issue #55930

@golang golang locked and limited conversation to collaborators Sep 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

4 participants