perf(lib/buffer_readwriter): make buffered read writer lock-free to reduce mutexes#616
Draft
sambhav-jain-16 wants to merge 3 commits into
Draft
perf(lib/buffer_readwriter): make buffered read writer lock-free to reduce mutexes#616sambhav-jain-16 wants to merge 3 commits into
sambhav-jain-16 wants to merge 3 commits into
Conversation
f682f0d to
8a56bcc
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR replaces the in-memory BufferReadWriter implementation to reduce lock contention during concurrent WriteAt workloads (e.g., sharded downloads/uploads), and adds tests/benchmarks to validate correctness and quantify performance.
Changes:
- Replaced the AWS
WriteAtBuffer-backed implementation with a custom[]bytebuffer guarded by anRWMutexplus an atomic “written extent”. - Updated
Size(),Bytes(),Read(),ReadAt(), andSeekEndsemantics to reflect only the maximum written extent (not the preallocated capacity). - Added a concurrent
WriteAtcorrectness test and a benchmark suite that reports mutex-contentions/op.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
lib/store/base/buffer_readwriter.go |
New RWMutex/atomic-based buffer implementation to reduce serialization for non-overlapping concurrent WriteAt calls. |
lib/store/base/buffer_readwriter_test.go |
Added concurrency test and benchmarks to validate/measure parallel WriteAt behavior and contention. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8a56bcc to
751f1a2
Compare
751f1a2 to
fd48f32
Compare
fd48f32 to
f7eb51a
Compare
Comment on lines
+47
to
+48
| // Pass the exact blob size when known to enable lock-free | ||
| // concurrent WriteAt calls for non-overlapping shard ranges. |
Comment on lines
+70
to
+76
| end := off + int64(len(p)) | ||
| if end < off { | ||
| return 0, fmt.Errorf("write at offset %d length %d overflows int64", off, len(p)) | ||
| } | ||
|
|
||
| b.mu.RLock() | ||
| if end <= int64(len(b.buf)) { |
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?
Mutexes on the buffer are the third-highest mutex contention for kraken-origin.
The
aws.WriteAtBufferacquires a lock at every lock in the whole buffer even when there's no overlapSome observations:
Statcall is rare; growth of the buffer and the need to acquire a lock at that time are rare but required.This change removes
aws.WriteAtBufferand adds a normal byte array that can be edited by simultaneous writers. Also, to handle the rare scenario where the buffer is smaller than expected, a lock is acquired, and a new buffer is allocated with the new size.Testing
Benchmarks