1
0
mirror of https://github.com/json-iterator/go.git synced 2025-07-03 23:30:41 +02:00

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

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

which led to big improvements across every dimension:
* CPU time reduction by 11+% (ns/op)
* throughput improvement by 13+% (MBs/op)
* allocations reduction by 45+% (B/op)
* number of allocations reduction by 18+% (alloc/op)
This commit is contained in:
Emmanuel T Odeke
2021-11-20 16:51:38 +01:00
parent 024077e996
commit 8c49f9b370
2 changed files with 8 additions and 8 deletions

View File

@ -40,7 +40,7 @@ func (iter *Iterator) ReadObject() (ret string) {
case '}': case '}':
return "" // end of object return "" // end of object
default: default:
iter.ReportError("ReadObject", fmt.Sprintf(`expect { or , or } or n, but found %s`, string([]byte{c}))) iter.ReportError("ReadObject", fmt.Sprintf(`expect { or , or } or n, but found %c`, c))
return return
} }
} }

View File

@ -97,34 +97,34 @@ func (iter *Iterator) Skip() {
func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) { func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
if iter.readByte() != b1 { if iter.readByte() != b1 {
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4}))) iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", []byte{b1, b2, b3, b4}))
return return
} }
if iter.readByte() != b2 { if iter.readByte() != b2 {
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4}))) iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", []byte{b1, b2, b3, b4}))
return return
} }
if iter.readByte() != b3 { if iter.readByte() != b3 {
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4}))) iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", []byte{b1, b2, b3, b4}))
return return
} }
if iter.readByte() != b4 { if iter.readByte() != b4 {
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4}))) iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", []byte{b1, b2, b3, b4}))
return return
} }
} }
func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) { func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
if iter.readByte() != b1 { if iter.readByte() != b1 {
iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3}))) iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", []byte{b1, b2, b3}))
return return
} }
if iter.readByte() != b2 { if iter.readByte() != b2 {
iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3}))) iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", []byte{b1, b2, b3}))
return return
} }
if iter.readByte() != b3 { if iter.readByte() != b3 {
iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3}))) iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", []byte{b1, b2, b3}))
return return
} }
} }