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
util/httputil: use const string instead of variable int. #6963
Conversation
Thanks for the pull request. regarding int -> var: what is the added value? why is it better? can we use a const int? with zero value? |
The code is good which has no bugs. Mainly for better understanding. The better reason for using Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
int has a smaller footprint. What matters here is the name of the variable for the visibility. Adding a value equal to the variable name does not really add value IMO, even for readability. const is an improvement however ; and it can be done with a one liner. WDYT? |
It's hard to say that go version go1.13.8 darwin/amd64 goos: darwin
goarch: amd64
pkg: tmp
BenchmarkIntVarKey-8 1000000000 0.867 ns/op 0 B/op 0 allocs/op
BenchmarkIntKey-8 1000000000 0.472 ns/op 0 B/op 0 allocs/op
BenchmarkStringKey-8 1000000000 0.447 ns/op 0 B/op 0 allocs/op The test cases: package do_test
import (
"context"
"testing"
)
type (
Int int
String string
)
var (
intVarKey Int = 0
)
const (
intKey Int = 0
stringKey String = "test"
)
func BenchmarkIntVarKey(b *testing.B) {
b.ReportAllocs()
ctx := context.Background()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
ctx.Value(intVarKey)
}
})
}
func BenchmarkIntKey(b *testing.B) {
b.ReportAllocs()
ctx := context.Background()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
ctx.Value(intKey)
}
})
}
func BenchmarkStringKey(b *testing.B) {
b.ReportAllocs()
ctx := context.Background()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
ctx.Value(stringKey)
}
})
} |
I believe int vs string discussion on some tiny |
util/httputil/context.go
Outdated
const ( | ||
pathParam ctxParam = "path_param" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const ( | |
pathParam ctxParam = "path_param" | |
) | |
const pathParam ctxParam = "path" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest moving to a one liner with shorter value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.
Signed-off-by: johncming <johncming@yahoo.com>
While you are at it: can you add |
👍 updated. |
promql/engine.go
Outdated
|
||
var queryOrigin queryCtx | ||
const queryOrigin queryCtx = "query" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const queryOrigin queryCtx = "query" | |
const queryOrigin queryCtx = "origin" |
makes more sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://golang.org/pkg/context/#WithValue recommends struct{}
. I find string
confusing as it suggests the value of the key has any meaning.
This is an alternative to #6963. Signed-off-by: beorn7 <beorn@grafana.com>
I tried out an alternative approach, see #6965. |
This is an alternative to #6963. Signed-off-by: beorn7 <beorn@grafana.com>
(I had it in code anyway to play with it, and it was easier to just push it instead of explaining it here.) |
@johncming do you agree with #6965 ? |
Signed-off-by: johncming <johncming@yahoo.com>
Some discussions in golang: golang/go#33742
It looks like an old discussion. 😊 There are more than one best practices. |
You did not answer my question. |
Even besides the allocation discussion, I think the |
I think all three are OK. |
I feel that all three options are subjective. From a view of binding variable in a functional language, it looks more like |
I think if you assign a value to a variable, const or not, and you never inspect it, that's objectively weird. But I also don't feel super strongly about it. Just wanted to suggest something that IMHO makes most sense in this case and is also shorter. Whatever you others decide… |
Going for @beorn7 as there is less repetition in the names etc. It is, I think, a bit more elegant. Thank you for your work! |
It's better to use const string than variable int.
Because
pathParam
is only visible in a package, it's not a bug but an improvement.