Memory usage optimizations - 61% reduction#278
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #278 +/- ##
==========================================
+ Coverage 90.48% 90.52% +0.03%
==========================================
Files 84 84
Lines 15729 15835 +106
==========================================
+ Hits 14233 14335 +102
- Misses 1496 1500 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Optimizations: - NLRI Add-Path clone-free parsing: Use speculative parsing from byte slices instead of cloning entire input buffer. Parses without consuming, only advances Bytes after successful parse. Saves ~1.2GB (61%) peak memory. - Attribute vector pre-allocation: Estimate capacity from data size - RIS Live vector pre-allocation: Calculate capacity from announcements+withdrawals Results: - Peak memory reduced by 60.6% (2,049 MB → 808 MB) - Tested with RouteViews LINX RIB (151MB compressed) - Added 7 new test cases for NLRI parsing edge cases No breaking changes. All 565 tests pass.
0270bf0 to
b3f52ad
Compare
Implement Option C from simplify agent review: - Make try_parse_prefix the primary implementation (speculative parsing) - Make read_nlri_prefix a thin wrapper (just calls try_parse_prefix + advance) - Eliminates ~68 lines of duplicated code - Maintains 61% memory reduction (2,032 MB → 784 MB) - All 565 tests pass Benefits: - Single source of truth for prefix parsing logic - No code duplication - Cleaner architecture - Same performance gains
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.
Summary
This PR implements memory usage optimizations that reduce peak memory consumption by 61% when parsing large BGP table dumps, while maintaining clean code architecture with no duplication.
Changes
1. NLRI Add-Path Clone-Free Parsing (Major Optimization)
File:
src/parser/utils.rsProblem: The original implementation cloned the entire input buffer when the Add-Path heuristic triggered (first byte is 0), retaining it for potential retry. This caused ~2x memory usage during parsing.
Solution:
try_parse_prefix()- parses from a byte slice without consuming, returns(prefix, bytes_consumed)read_nlri_prefix()- thin wrapper that callstry_parse_prefixand advances the cursorinput.clone()operationArchitecture:
2. Attribute Vector Pre-allocation
File:
src/parser/bgp/attributes/mod.rsEstimate capacity from data size:
remaining_bytes / 3(minimum bytes per attribute), capped at 256.3. RIS Live Vector Pre-allocation
File:
src/parser/rislive/mod.rsCalculate capacity from announcements + withdrawals before creating the vector.
Benchmark Results
Test Setup:
/usr/bin/time -lpeak memory footprintResults:
Code Quality Improvements
Before: ~140 lines of duplicated prefix parsing logic (in
read_nlri_prefixandtry_parse_prefix)After: ~70 lines in
try_parse_prefixonly,read_nlri_prefixis a 4-line wrapperBenefits:
Test Coverage
9 comprehensive NLRI tests covering:
Backward Compatibility
✅ No breaking changes:
read_nlri_prefixmaintains same interfaceVerification
cargo clippy --all-targets --all-features -- -D warnings)Related
Addresses code review feedback from simplify agent on PR #278 - eliminated code duplication while maintaining optimization gains.