json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.34k stars 1.02k forks source link

all: remove unnecessary string([]byte) conversion in fmt.Sprintf yet %s #590

Open odeke-em opened 2 years ago

odeke-em commented 2 years ago

Noticed from Orijtech's continuous benchmarking product "Bencher" per

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

that there is a bunch of code in the wild that invokes:

fmt.Sprintf("%s", string([]byte(...)))

yet the "%s" format specifier in fmt has a purpose:

%s  the uninterpreted bytes of the string or slice

We can improve and reduce allocations simply by invoking

fmt.Sprintf("%s", []byte(...))

which led to big improvements across every dimension:

/cc @kirbyquerby @willpoint