-
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
context: Remove allocation discussion from WithValue documentation #33742
Comments
If you change
to
then it allocates. If you change
to
then it allocates. (Tested with Go 1.12.5.) There are some nice optimizations to prevent certain interface allocations, but they shouldn't be documented here. Best to stick with simple rules of thumb. |
CC @Sajmani for |
Wow thanks @cespare! Since I was wrong and this does matter, should we change the example in the package to follow its own advice and not use There is also duplicated description about context key types: There is a description on Context.Value: "A key can be any type that supports equality [...]", as well as on Here are the updated metrics with the var string and key types for Go 1.12. As you can see: var/const makes a huge difference. Go 1.12
|
I agree we should update the documentation to be consistent with best practices. Even better would be to provide a library function to create a good context key, if that provides good performance:
|
How does this compare to the comment from net/http package:
So should this be the recommended way? Simply using a pointer? Does |
This is a proposed package documentation change. I'm happy to submit a code change with this update, if it makes sense. I took the liberty of abbreviating the questions in the template.
What version of Go are you using (
go version
)?Documentation in the latest source of context.go (commit d6ffc1d8394d6f6420bb92d79d320da88720fbe0)
What does the current documentation say
WithValue: "To avoid allocating when assigning to an interface{}, context keys often have concrete type struct{}. Alternatively, exported context key variables' static type should be a pointer or interface."
Current documentation: https://tip.golang.org/pkg/context/#WithValue
Code:
go/src/context/context.go
Lines 476 to 479 in d6ffc1d
What should it say
Those two sentences should be removed. With Go >= 1.9, it no longer matters. To verify, I ran the following test with different versions of Go on a VM with a command like:
docker run --workdir=/wtf/test -v $HOME:/wtf -ti --rm golang:1.12 go test -bench=. .
I tested each release from 1.12 through to 1.8. With version 1.8, this mattered a lot. It no longer does. From the output below, you can see that using an
int
key is slower, but does not allocate. The other choices (interface{}
, pointer, custom string), all appear to be equivalent.I think it would simplify the package documentation to omit this.
This was previously changed after the discussion in #17826 . My test is based on that one.
Go 1.12
Go 1.9
Go 1.8
Test code
The text was updated successfully, but these errors were encountered: