Skip to content
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

Optimize RLP length calculation #6729

Merged
merged 6 commits into from
Feb 15, 2024
Merged

Optimize RLP length calculation #6729

merged 6 commits into from
Feb 15, 2024

Conversation

PaulusParssinen
Copy link
Contributor

@PaulusParssinen PaulusParssinen commented Feb 14, 2024

Changes

  • Make Rlp.LengthOfLength branchless
    • This is just calculating the amount of bytes required the represent the value, we can CLZ it. This makes the method inlineable for the JIT thanks to much tighter IL (and codegen).
    • SharpLab
  • Make Rlp.LengthOf almost branchless
    • We can use similar bit tricks here. Wasn't able to get rid of the branch that handles the 0 < value < 128 special case, should still be friendlier to the branch prediction and to the JIT.
    • SharpLab
  • Use BinaryPrimitives in Rlp.SerializeLength
    • Little bit better codegen. We get bswap for 32-bit case and reduce remove few bounds-checks. We could also reduce bounds-checks for the 24-bit case but I thought this change more as a refactor to utilize the "new" built-in APIs.

Types of changes

What types of changes does your code introduce?

  • Optimization

Testing

Requires testing

  • No

Notes on testing

All tests green locally.

@PaulusParssinen PaulusParssinen changed the title Rlp length optimizations Optimize RLP length calculation Feb 14, 2024
@benaadams
Copy link
Member

Fails Negative_port_just_in_case_for_resilience currently; but I very much like the change

}

if (value < 256L * 256L * 256L * 256L * 256L * 256L * 256L)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wild set of if statements has always bothered me when I saw it

@benaadams benaadams merged commit 90080a8 into NethermindEth:master Feb 15, 2024
67 checks passed
@benaadams
Copy link
Member

@PaulusParssinen thank you for your contribution

@PaulusParssinen PaulusParssinen deleted the rlp-length-optimizations branch February 15, 2024 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants