-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
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% ```
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
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 |
Yep, exactly. |
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% ```
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: