go icon indicating copy to clipboard operation
go copied to clipboard

avoid: fmt.Sprintf”%s”, string(byteslice)) which unnecessarily consumes more memory

Open odeke-em opened this issue 4 years ago • 1 comments

fmt.*printf's "%s" verb converts values to strings in a performant way so no need to cast and unnecessarily allocate byteslice->string conversions.

Noticed from our benchmarking per

https://dashboard.github.orijtech.com/benchmark/3245b8e4bbbd44a597480319aaa4b9fe

that avoiding this unnecessary pattern can reduce time spent and allocations by quite a big value and that showed the following improvements:

  • time/op (ns/op) FormatIt-8 1.2µs ± 2% 1.1µs ± 10% -11.77% (p=0.000 n=10+9)

  • allocs/op (MBs/op) FormatIt-8 0.71GB/s ± 2% 0.80GB/s ± 9% +13.59% (p=0.000 n=10+9)

  • allocs/op (B/op) FormatIt-8 2.0kB ± 0% 1.1kB ± 0% -45.62% (p=0.000 n=10+10)

  • allocs/op (count/op) FormatIt-8 11 ± 0% 9.0 ± 0% -18.18% (p=0.000 n=10+10)

Also while here, plugging our continuous benchmarking product for Go, "Bencher". It'll remove the painful and painstaking ergonomics of having to set up your own quiet machines, running benchmarks before and after then having to interpret them. Test out and validate ideas and hypotheses fast and effectively. See https://bencher.orijtech.com/

Kindly cc-ing my colleagues @willpoint @kirbyquerby

odeke-em avatar Oct 21 '21 16:10 odeke-em

Would you be willing to submit a PR that avoids these unnecessary allocations in the Dapr API hotpath? Asking since you didn't provide examples of such (where these allocations matter).

yaron2 avatar Dec 20 '21 07:12 yaron2