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

Optimize Sugar logger for calls with a single string arg #913

Merged
merged 1 commit into from Feb 8, 2021

Conversation

prashantv
Copy link
Collaborator

Currently, the Sugar logger uses fmt.Sprint in all cases when the
template is empty. However, this call is unnecessary if there's
a single string type argument, as we can use it directly.

With this optimization, we reduce the cost and avoid an unnecessary
alloc:

> benchcmp pre post
benchmark                         old ns/op     new ns/op     delta
BenchmarkSugarSingleStrArg-10     636           570           -10.38%

benchmark                         old allocs     new allocs     delta
BenchmarkSugarSingleStrArg-10     1              0              -100.00%

Currently, the Sugar logger uses fmt.Sprint in all cases when the
template is empty. However, this call is unnecessary if there's
a single string type argument, as we can use it directly.

With this optimization, we reduce the cost and avoid an unnecessary
alloc:
```
> benchcmp pre post
benchmark                         old ns/op     new ns/op     delta
BenchmarkSugarSingleStrArg-10     636           570           -10.38%

benchmark                         old allocs     new allocs     delta
BenchmarkSugarSingleStrArg-10     1              0              -100.00%
```
@prashantv prashantv requested a review from abhinav Feb 5, 2021
@codecov
Copy link

codecov bot commented Feb 5, 2021

Codecov Report

Merging #913 (9382aa4) into master (a68efdb) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #913      +/-   ##
==========================================
- Coverage   98.21%   98.16%   -0.06%     
==========================================
  Files          43       44       +1     
  Lines        1910     2015     +105     
==========================================
+ Hits         1876     1978     +102     
- Misses         27       29       +2     
- Partials        7        8       +1     
Impacted Files Coverage Δ
sugar.go 100.00% <100.00%> (ø)
logger.go 95.78% <0.00%> (-0.38%) ⬇️
options.go 100.00% <0.00%> (ø)
http_handler.go 100.00% <0.00%> (ø)
clock.go 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a68efdb...9382aa4. Read the comment docs.

rabbbit
rabbbit approved these changes Feb 8, 2021
@rabbbit
Copy link

rabbbit commented Feb 8, 2021

However, this call is unnecessary if there's a single string type argument,

Why would anyone pass in no template, but a single string argument? zap.Info("", "hello")..?

abhinav
abhinav approved these changes Feb 8, 2021
@abhinav
Copy link
Contributor

abhinav commented Feb 8, 2021

Why would anyone pass in no template, but a single string argument? zap.Info("", "hello")..?

IIRC, internally Zap calls that function with an empty template for the Sprint-equivalent cases.

@prashantv
Copy link
Collaborator Author

Why would anyone pass in no template, but a single string argument? zap.Info("", "hello")..?

IIRC, internally Zap calls that function with an empty template for the Sprint-equivalent cases.

Yep, exactly. zap.Info("hello") will actually go through this path (the first argument is the template if used with Sprintf).

@prashantv prashantv merged commit 4f6f7e9 into master Feb 8, 2021
1 check passed
@prashantv prashantv deleted the sugar-sprint branch Feb 8, 2021
abhinav pushed a commit that referenced this pull request May 25, 2021
Currently, the Sugar logger uses fmt.Sprint in all cases when the
template is empty. However, this call is unnecessary if there's
a single string type argument, as we can use it directly.

With this optimization, we reduce the cost and avoid an unnecessary
alloc:
```
> benchcmp pre post
benchmark                         old ns/op     new ns/op     delta
BenchmarkSugarSingleStrArg-10     636           570           -10.38%

benchmark                         old allocs     new allocs     delta
BenchmarkSugarSingleStrArg-10     1              0              -100.00%
```
cgxxv pushed a commit to cgxxv/zap that referenced this pull request Mar 25, 2022
Currently, the Sugar logger uses fmt.Sprint in all cases when the
template is empty. However, this call is unnecessary if there's
a single string type argument, as we can use it directly.

With this optimization, we reduce the cost and avoid an unnecessary
alloc:
```
> benchcmp pre post
benchmark                         old ns/op     new ns/op     delta
BenchmarkSugarSingleStrArg-10     636           570           -10.38%

benchmark                         old allocs     new allocs     delta
BenchmarkSugarSingleStrArg-10     1              0              -100.00%
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants