-
Notifications
You must be signed in to change notification settings - Fork 6
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
perf(sn): reuse buffer for ReplicateRequest unmarshaling #808
base: main
Are you sure you want to change the base?
Conversation
d5f5e27
to
0e0a36e
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #808 +/- ##
==========================================
+ Coverage 68.09% 68.44% +0.35%
==========================================
Files 182 182
Lines 17899 17903 +4
==========================================
+ Hits 12188 12254 +66
+ Misses 4941 4863 -78
- Partials 770 786 +16 ☔ View full report in Codecov by Sentry. |
18c6632
to
770f7ef
Compare
proto/snpb/replicator_test.go
Outdated
require.Equal(t, want2, got2) | ||
}) | ||
|
||
t.Run("GrowLength", func(t *testing.T) { |
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.
We need to check whether unmarshal or grow was successful.
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.
Done.
3060aa5
to
122afa5
Compare
6feab12
to
57f4a1a
Compare
57f4a1a
to
60a1a53
Compare
Improve unmarshaling performance by reusing buffers for ReplicateRequest in the backup replica. The protobuf message `github.com/kakao/varlog/proto/snpb.(ReplicateRequest)` has two slice fields—LLSN (`[]uint64`) and Data (`[][]byte`). The backup replica receives replicated log entries from the primary replica via the gRPC service `github.com/kakao/varlog/proto/snpb.(ReplicatorServer).Replicate`, which sends `ReplicateRequest` 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 slices instead of resetting them completely. The `ResetReuse` method shrinks the slice lengths while preserving their capacities, thus avoiding the overhead of reallocating memory. Example implementation: ```go type Message struct { Buffer []byte // Other fields } func (m *Message) Reset() { *m = Message{} } func (m *Message) ResetReuse() { s := m.Buffer[:0] *m = Message{} m.Buffer = s } ``` 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. Resolves: #795 See also: #806
60a1a53
to
8ef1886
Compare
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