Apply ValueRlpStream optimizations to RlpStream and ValueDecoderContext#9208
Merged
Conversation
Copilot
AI
changed the title
[WIP] Apply ValueRlpStream optimizations to RlpStream and ValueDecoderContext
Apply ValueRlpStream optimizations to RlpStream and ValueDecoderContext
Aug 26, 2025
Member
|
status? |
8704cdb to
9e4ccde
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR ports the ValueRlpStream performance optimizations to RlpStream and Rlp.ValueDecoderContext, and centralizes shared logic in a new RlpHelpers class to reduce duplication and improve hot-path efficiency.
- Introduces RlpHelpers with branchless prefix length calculation and shared length deserialization/throw helpers
- Replaces if/else chains with switch expressions and extracts cold paths with NoInlining
- Simplifies SkipItem/SkipLength using PeekNextRlpLength/PeekPrefixLength
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Nethermind/Nethermind.Serialization.Rlp/ValueRlpStream.cs | Uses RlpHelpers for prefix-length calc and length deserialization; centralizes throw helpers. |
| src/Nethermind/Nethermind.Serialization.Rlp/RlpStream.cs | Adds PeekPrefixLength, switch-based PeekNextRlpLength, and helper-based length deserialization; refactors long string/list logic. |
| src/Nethermind/Nethermind.Serialization.Rlp/RlpHelpers.cs | New helper class providing branchless prefix calc, shared DeserializeLengthRef, and throw helpers. |
| src/Nethermind/Nethermind.Serialization.Rlp/Rlp.cs | Applies the same optimizations to ValueDecoderContext, replacing tuple-based peeks and manual length reads with helpers. |
Co-Authored-By: benaadams <1142958+benaadams@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
6283097 to
f0c92c4
Compare
benaadams
approved these changes
Oct 3, 2025
Member
Good to go |
LukaszRozmej
approved these changes
Oct 3, 2025
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.
This PR applies the performance optimizations from PR #9195 to
RlpStreamandRlp.ValueDecoderContextclasses, and introduces shared helper code to eliminate duplication between all three RLP processing implementations.Key Optimizations Applied
1. Branchless Prefix Length Calculation
Replaced conditional logic with bit manipulation for calculating RLP prefix lengths:
2. Switch Expression Optimizations
Converted if/else chains to switch expressions for better compiler optimization:
3. Optimized Method Separation
Split complex logic into specialized methods marked with
[MethodImpl(MethodImplOptions.NoInlining)]for better cache locality and JIT optimization of hot paths.4. Shared Helper Code
Created
RlpHelpersclass to eliminate code duplication:CalculatePrefixLength()- Branchless prefix calculationDeserializeLengthRef()- Optimized length deserialization5. Improved Bounds Checking
Replaced range checks with more efficient unsigned comparisons:
6. Optimized SkipItem Implementation
Simplified item skipping to use single method call instead of tuple destructuring:
Impact
RlpHelpersVerification
The optimizations maintain identical behavior while providing better performance characteristics for RLP processing, which is critical for Ethereum block and transaction parsing.
Fixes #9207.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
aka.mscurl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.