-
Notifications
You must be signed in to change notification settings - Fork 566
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
otelzap: Use pool for encoder #5719
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5719 +/- ##
=======================================
+ Coverage 64.6% 64.7% +0.1%
=======================================
Files 201 201
Lines 12587 12626 +39
=======================================
+ Hits 8134 8173 +39
Misses 4216 4216
Partials 237 237
|
Please make sure to add benchmarks to the PRs which are meant to improve performance. |
@@ -196,15 +230,13 @@ type arrayEncoder struct { | |||
} | |||
|
|||
func (a *arrayEncoder) AppendArray(v zapcore.ArrayMarshaler) error { | |||
// TODO: Use arrayEncoder from a pool. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding sync.pool here does not improve performance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What benchmark was used to see this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At quick glance, I think that the issue is caused by the fact that in array encoder free
function does:
arr.elems = arr.elems[:0]
which causes to reuse the same underlying array. The next ArrayAppender
is going to access the same elements when doing
err := v.MarshalLogArray(arr)
before the logs are emitted
I suggest not using the pooled encoder for AppendArray
and AppendObject
in scope of this PR.
I think (hope) that AppendArray
and AppendObject
are not going to be frequently invoked/used by the bridge users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the same problem with using shared memory applies for AddArray
and AddObject
. Similarly to #5879.
Probably we miss some unit test that would detect it.
Please use https://pkg.go.dev/golang.org/x/perf/cmd/benchstat to show the performance improvement (between main and this branch) in the PR description. |
func getArrayEncoder() (arr *arrayEncoder, free func()) { | ||
arr = arrayEncoderPool.Get().(*arrayEncoder) | ||
return arr, func() { | ||
arr.elems = arr.elems[:0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this comment?
opentelemetry-go-contrib/bridges/otelslog/handler.go
Lines 352 to 354 in c80e464
// TODO: limit returned size so the pool doesn't hold on to very large | |
// buffers. Idea is based on | |
// https://cs.opensource.google/go/x/exp/+/814bf88c:slog/internal/buffer/buffer.go;l=27-34 |
func getObjectEncoder() (obj *objectEncoder, free func()) { | ||
obj = objectEncoderPool.Get().(*objectEncoder) | ||
return obj, func() { | ||
obj.root.attrs = obj.root.attrs[:0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this comment?
opentelemetry-go-contrib/bridges/otelslog/handler.go
Lines 352 to 354 in c80e464
// TODO: limit returned size so the pool doesn't hold on to very large | |
// buffers. Idea is based on | |
// https://cs.opensource.google/go/x/exp/+/814bf88c:slog/internal/buffer/buffer.go;l=27-34 |
Can you please fix the build? |
Part of #5191
Pre-work #5279
Results: