fix(codec): support copying thrift fast writes (similar to pr 1983 ) - #1988
Open
wdp2020 wants to merge 2 commits into
Open
fix(codec): support copying thrift fast writes (similar to pr 1983 )#1988wdp2020 wants to merge 2 commits into
wdp2020 wants to merge 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1988 +/- ##
==========================================
+ Coverage 62.95% 63.02% +0.06%
==========================================
Files 394 396 +2
Lines 30267 30351 +84
==========================================
+ Hits 19056 19129 +73
- Misses 9924 9937 +13
+ Partials 1287 1285 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
What type of PR is this?
Description
The Thrift fast-write path currently passes the output buffer as a remote.NocopyWrite implementation to generated FastWriteNocopy methods. For string and binary fields, this causes the generated code to use WriteDirect, followed by MallocAck, instead of copying the data into the buffer allocated for the encoded message.
In workloads containing large string or binary fields, the WriteDirect and MallocAck path can introduce significant CPU and buffer-management overhead. In the observed profile, response encoding consumed 53.02% of CPU time, with two large-string WriteDirect paths accounting for approximately 49.45%.
This change adds NewThriftCodecWithFastWriteCopy, an opt-in codec constructor that keeps Thrift fast read/write enabled while forcing string and binary fields to be copied into the preallocated output buffer.
When copy mode is enabled, encodeFastThrift passes a nil NocopyWrite writer to FastWriteNocopy. The generated code therefore writes those fields into the buffer returned by Malloc instead of calling WriteDirect and MallocAck.
The option is applied consistently to:
Normal fast Thrift encoding
Fast-write fallback encoding
Thrift exception encoding
Why It Is Necessary
The existing codec does not provide a way to disable direct writes without also giving up the generated fast codec path. Although direct writes can reduce copying for some workloads, they can be more expensive when handling large fields with transports whose direct-write implementation requires additional buffer operations.
Making this behavior opt-in provides a targeted workaround while preserving the existing default behavior for all current users.
Compatibility
This change is backward compatible:
NewThriftCodec() retains the existing no-copy behavior.
Copy mode is enabled only through NewThriftCodecWithFastWriteCopy().
The Thrift wire format is unchanged.
Existing codec configuration and generated code remain compatible.
Tests verify that the default codec still calls WriteDirect and MallocAck, while the new codec avoids both operations and produces payloads that can be decoded correctly.
Check the PR title.
(Optional) Translate the PR title into Chinese.
(Optional) More detailed description for this PR(en: English/zh: Chinese).
en:
zh(optional):
(Optional) Which issue(s) this PR fixes:
(optional) The PR that updates user documentation: