Migrate to Copy-on-Write (Cow) for zero-copy decoding support - #40
Conversation
Add lifetime parameters to BencodexValue<'a> and BencodexKey<'a>, replacing internal Vec<u8>/String with Cow<'a, [u8]>/Cow<'a, str>. This enables zero-copy decoding via the new decode_borrowed() API that borrows directly from the input slice. Also changes Encode trait from consuming self to &self, eliminating unnecessary clones during encoding. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughThis pull request introduces zero-copy borrowing semantics throughout the bencodex codec by adding lifetime parameters to BencodexValue and BencodexKey using Cow-backed storage, refactoring the Encode trait to accept borrowed references, and providing dual decode paths for both owned and borrowed data across all codec implementations. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #40 +/- ##
==========================================
+ Coverage 74.78% 75.23% +0.44%
==========================================
Files 14 14
Lines 714 751 +37
==========================================
+ Hits 534 565 +31
- Misses 180 186 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging this PR will improve performance by ×3.5Comparing Summary
Performance Changes
Footnotes
|
Parameterize decode_impl with const generic BORROW to control whether leaf values (Binary/Text) use Cow::Borrowed or Cow::Owned. When BORROW=false, owned values are produced directly during parsing, eliminating the post-decode into_owned() walk that was rebuilding every BTreeMap and Vec container. This fixes the 10-20% decode_scalar regression on small/medium files introduced by the Cow migration. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add lifetime parameters to
BencodexValue<'a>andBencodexKey<'a>, replacing internalVec<u8>/StringwithCow<'a, [u8]>/Cow<'a, str>. This enables zero-copy decoding via the newdecode_borrowed()API that borrows directly from the input slice. Also changesEncodetrait from consumingselfto&self, eliminating unnecessary clones during encoding.Summary by CodeRabbit
New Features
decode_borrowedfunction for improved performance with borrowed data.Bug Fixes
Refactor
Encodetrait to borrow values instead of consuming them.✏️ Tip: You can customize this high-level summary in your review settings.