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

Open
renthraysk opened this issue May 18, 2022 · 2 comments
Labels
NeedsInvestigation Performance
Milestone

Comments

@renthraysk
Copy link

@renthraysk renthraysk commented May 18, 2022

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

@randall77 randall77 commented May 18, 2022

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 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 label May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Performance
Projects
None yet
Development

No branches or pull requests

3 participants