Use a cached buffer for serialization#867
Merged
Merged
Conversation
Contributor
Author
|
/azp run Benchmarks |
|
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
Contributor
Author
|
/azp run Benchmarks |
|
Azure Pipelines successfully started running 1 pipeline(s). |
kevingosse
force-pushed
the
kevin/optimizeflushtraces
branch
from
August 19, 2020 11:30
2d7d7e6 to
eecb82a
Compare
lucaspimentel
approved these changes
Aug 24, 2020
lucaspimentel
left a comment
Member
There was a problem hiding this comment.
LGTM. Only non-blocking comments.
| buffer = new byte[InitialBufferSize]; | ||
| } | ||
|
|
||
| int length = MessagePackSerializer.Serialize(ref buffer, 0, obj, resolver); |
Member
There was a problem hiding this comment.
Out of scope of this PR, but something to keep in mind: having the size of the payload means we can now send it as a tracer health metric (see TracerMetricNames for examples of other metrics).
kevingosse
force-pushed
the
kevin/optimizeflushtraces
branch
from
August 25, 2020 09:55
eecb82a to
e6431b9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
MessagePack has an internal pool of buffers, but they are thread-static. It's bad because:
We almost never send traces to the agent concurrently (I believe the call to
IAgentWriter.Ping()at startup is the only exception so far). Therefore it makes sense to use a single cached buffer, and fallback to allocating a new one in the unlikely case we need it from two different threads at the same time.I also updated the
WriteAndFlushTracesbenchmark to use a fake stream, because the MemoryStream was the source of a lot of extra allocations. In the real world we would be writing to a network stream and we have (AFAIK) no control over how much it'll allocate. It's better to focus on measuring what's actionable.Benchmark:
AgentWriterBenchmark.WriteAndFlushTracesThe main source of allocations remaining on this codepath is
GetUniqueTraceIds. I tried replacing the hashset by a Linq Distinct() operation. It reduces heap allocations by about 40% (down to ~45KB) but also increase processing time by about 10% on .NET Framework (up to 440µs, no significant increase on .NET Core), so I don't know if it's worth it.