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

context: Consider disable Type.comparable check in WithValue #47741

Closed
morlay opened this issue Aug 17, 2021 · 1 comment
Closed

context: Consider disable Type.comparable check in WithValue #47741

morlay opened this issue Aug 17, 2021 · 1 comment

Comments

@morlay
Copy link

morlay commented Aug 17, 2021

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

$ go version
1.15 1.16 1.17

What did you do?

When I use context.WithValue(), I see it always cost 20ns/op +
I checked the source codes, i found a TypeOf(key).Comparable() https://github.com/golang/go/blob/master/src/context/context.go#L530,

Then i remove it, WithValue become faster

see the Benchmark results below

$ go test -test.bench=BenchmarkWithValue -test.benchmem ./...
goos: darwin
goarch: arm64
pkg: github.com/go-courier/x/contextx
BenchmarkWithValue/WithValue_with_reflect_type_comparable_check-8               50766240                23.37 ns/op           48 B/op          1 allocs/op
BenchmarkWithValue/WithValue_without_reflect_type_comparable_check-8            1000000000               0.3160 ns/op          0 B/op          0 allocs/op

Maybe we could consider to remove https://github.com/golang/go/blob/master/src/context/context.go#L530

Or just make valuectx public for people to recreate a faster WithValue

@seankhliao
Copy link
Member

If the value isn't comparable then there would be no way to retrieve it.
Ensuring that the program runs correctly should be more important.
See the change that added it: https://go-review.googlesource.com/c/go/+/21799/

context.Context is an interface and people are free to create other implementations with less safety.

@golang golang locked and limited conversation to collaborators Aug 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants