Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: remove unnecessary string(byteslice) when passed into fmt.*rintf("%s", string(b)) #17577

Merged
merged 1 commit into from Oct 19, 2021
Merged

all: remove unnecessary string(byteslice) when passed into fmt.*rintf("%s", string(b)) #17577

merged 1 commit into from Oct 19, 2021

Conversation

odeke-em
Copy link
Contributor

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 own 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)

Signed-off-by: Emmanuel T Odeke emmanuel@orijtech.com

@odeke-em odeke-em requested a review from a team October 12, 2021 07:49
@odeke-em odeke-em requested a review from a team as a code owner October 12, 2021 07:49
@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Oct 12, 2021
@odeke-em odeke-em requested a review from kkourt October 12, 2021 07:49
@odeke-em odeke-em requested a review from twpayne October 12, 2021 07:49
@odeke-em
Copy link
Contributor Author

Kindly cc-ing @tklauser

Copy link
Contributor

@twpayne twpayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you :)

@tklauser tklauser added the release-note/misc This PR makes changes that have no direct user impact. label Oct 12, 2021
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Oct 12, 2021
Copy link
Contributor

@kkourt kkourt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Just a comment on the linter complain.

pkg/sysctl/sysctl.go Outdated Show resolved Hide resolved
…("%s", string(b))

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 own 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:

```shell
* 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)
```

Signed-off-by: Emmanuel T Odeke <emmanuel@orijtech.com>
@twpayne twpayne added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Oct 18, 2021
@joamaki joamaki merged commit 9b3a2dd into cilium:master Oct 19, 2021
@odeke-em odeke-em deleted the remove-unnecessary-string-conversion-before-fmt.rintf-percent-s branch October 21, 2021 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants