dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.18k stars 378 forks source link

SA6001: flag m[buf.String()] for bytes.Buffer #753

Open dominikh opened 4 years ago

dominikh commented 4 years ago

The optimization of m[string(b)] applies to bytes.Buffer, too, making m[string(buf.Bytes())] more efficient than m[buf.String()].

martisch commented 4 years ago

In case this is not already covered It also applies to m[struct{...,string(...),....}] and arrays. But that might not come up often enough.

https://go-review.googlesource.com/c/go/+/116275

dominikh commented 4 years ago

@martisch I'm wondering, would the compiler be able to optimize m[buf.String()]? Or does the branch in the String method make that too difficult?

martisch commented 4 years ago

@dominikh I think such an optimization would only come in the future through inlining and dead code elimination together with moving more such optimizations into the SSA backend.

The current frontend itself doesnt do these cross function optimizations and doesnt as far as I know try to give std packages semantics hardcoded in the compiler.