-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Closed
Labels
FrozenDueToAgeGarbageCollectorNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.Performance
Milestone
Description
Many functions take a []byte but only read from it.
If the escape analysis code could flag parameters of a function as read-only, then code
which passes in a []byte(string) conversion could be cheaper.
bytes.IndexByte is one example.
For example, http/sniff.go does:
// -------------------
// Index of the first non-whitespace byte in data.
firstNonWS := 0
for ; firstNonWS < len(data) && isWS(data[firstNonWS]); firstNonWS++ {
}
func isWS(b byte) bool {
return bytes.IndexByte([]byte("\t\n\x0C\n "), b) != -1
}
// -------------------
But it's faster to avoid re-creating the []byte:
func BenchmarkIndexByteLiteral(b *testing.B) {
for i := 0; i < b.N; i++ {
IndexByte([]byte("\t\n\x0C\n "), 'x')
}
}
func BenchmarkIndexByteVariable(b *testing.B) {
var whitespace = []byte("\t\n\x0C\n ")
for i := 0; i < b.N; i++ {
IndexByte(whitespace, 'x')
}
}
bytes_test.BenchmarkIndexByteLiteral 20000000 125 ns/op
bytes_test.BenchmarkIndexByteVariable 100000000 25.4 ns/op
Related is issue #2204.dsnet, tillulen, odeke-em, ns-cweber, 2opremio and 27 more
Metadata
Metadata
Assignees
Labels
FrozenDueToAgeGarbageCollectorNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.Performance