Decode: use unsafe []byte->string conversion for discarded value #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For Decode's MLI4I, we invoke strconv.Atoi after string(*b)
which incurs a []byte->string allocation. The converted []byte->string
is discarded so anyways it is okay to use unsafe, as the string is
never used again. You can see the results at https://dashboard.github.orijtech.com/benchmark/dbb0fca65f644d95bcf8d0c8caafc601
Results:
time/op (ns/op)
Encoding/Decoding_2I-8 7.5ns ± 1% 6.7ns ± 0% -10.36% (p=0.000 n=10+9)
Encoding/Decoding_4E-8 8.8ns ± 0% 7.9ns ± 0% -10.35% (p=0.000 n=10+10)
Encoding/Decoding_A4E-8 38ns ± 1% 17ns ± 0% -54.84% (p=0.000 n=10+10)
allocs/op (B/op)
Encoding/Decoding_A4E-8 4.0B ± 0% 0 -100.00% (p=0.000 n=10+10)
allocs/op (N/op)
Encoding/Decoding_A4E-8 1.0 ± 0% 0 -100.00% (p=0.000 n=10+10)
Or visually
Problem Statement
What is the current behavior? Why and how does it need to change?
string(*b) incurs a []byte->string allocation yet the value will be discarded.
Description of Change
Please include a summary of the change and, if applicable, tag related issues, add code snippets or logs.
This change removes a []byte->string allocation
Breaking Change
Is this a breaking change?
No.
Caveats
Please list any caveats or special considerations for this change.
It uses unsafe, but the usage is isolated as we directly insert the converted string into strconv.Atoi, so this isn't a caveat really. /cc @kirbyquerby @cuonglm @madflojo