perf(sn): reuse buffer for ReplicateRequest unmarshaling #808
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 this PR does
Improve unmarshaling performance by reusing buffers for ReplicateRequest in the
backup replica.
The protobuf message
github.com/kakao/varlog/proto/snpb.(ReplicateRequest)
hastwo slice fields—LLSN (
[]uint64
) and Data ([][]byte
). The backup replicareceives replicated log entries from the primary replica via the gRPC service
github.com/kakao/varlog/proto/snpb.(ReplicatorServer).Replicate
, which sendsReplicateRequest
messages.Upon receiving a
ReplicateRequest
, the backup replica unmarshals the message,which involves growing slices for fields such as LLSN and Data. This growth
causes copy overhead whenever the slice capacities need to expand.
To address this, we introduce a new method,
ResetReuse
, for reusing slicesinstead of resetting them completely. The
ResetReuse
method shrinks the slicelengths while preserving their capacities, thus avoiding the overhead of
reallocating memory.
Example implementation:
Risks:
This approach has potential downsides. Since the heap space consumed by the
slices is not reclaimed, the storage node's memory consumption may increase.
Currently, there is no mechanism to shrink the heap usage.
Additionally, this PR changes the generated code. The protobuf compiler can
revert it, which is contrary to our intention. To catch this mistake, this PR
includes a unit test (github.com/kakao/varlog/proto/snpb.TestReplicateRequest)
to verify that the buffer backing the slices is reused.
Which issue(s) this PR resolves
Resolves: #795
See also: #806