Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Conversation

@odeke-em
Copy link
Contributor

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

as well as using the "%c" format specifier

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)

Also (*bytes.Buffer).String is invoked when "%q" or "%s" is passed in so
no need to use it.

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

as well as using the "%c" format specifier

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)

Also (*bytes.Buffer).String is invoked when "%q" or "%s" is passed in so
no need to use it.
@jhendrixMSFT jhendrixMSFT merged commit 8c4109c into Azure:master Dec 9, 2021
@odeke-em odeke-em deleted the remove-unnecessary-string-byteslice-in-fmtSprintf branch December 9, 2021 20:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants