Zero-copy improvement for U256 and I256#1867
Open
dkcumming wants to merge 4 commits into
Open
Conversation
U256 and I256
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes U256/I256 ↔ u128/i128 conversions in the Soroban Rust SDK by avoiding Bytes intermediates and instead extracting/constructing values directly via {U,I}256Val / {U,I}256Object, reducing host allocations and improving compute/memory budget usage.
Changes:
- Reworked
U256::{from_u128,to_u128}to construct/deconstruct using 64-bit pieces and direct object field access. - Reworked
I256::{from_i128,to_i128}similarly, including sign-extension handling fori128intoi256. - Updated internal imports to include
U256Object/I256Objectfor direct object access.
Comment on lines
+258
to
+261
| // If v is U256Small it can be converted directly | ||
| if let Ok(small) = U256Small::try_from(v) { | ||
| return Some(u64::from(small) as u128); | ||
| } |
Comment on lines
+518
to
+521
| // If v is I256Small it can be converted directly | ||
| if let Ok(small) = I256Small::try_from(v) { | ||
| return Some(i64::from(small) as i128); | ||
| } |
Contributor
There was a problem hiding this comment.
This seems reasonable to address if that's actually not covered
Member
There was a problem hiding this comment.
There's some benefit to filling these test gaps ahead of merging this PR.
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
Functions
U256::to_u128,U256::from_u128,I256::to_i128,I256::from_i128are changed to read the{U,I}256Valdirectly, instead of usingBytesas an intermediary.Why
The
Bytesroute invokes memory allocation host functions which are expensive. Using the{U,I}256Valdirectly is a zero-copy approach that avoids memory allocation and so consumes less resources. This is observed by some simple benchmarks:baselineu256_from_u128_maxu256_to_u128_fitsu256_to_u128_overflowi256_from_i128_mini256_to_i128_posi256_to_i128_negThe harnesses that were used for benching are in the details tab that follows
Details
The contract:
The benchmark harnesses:
Known limitations
This approach does not seem to come with a tradeoff, and it still might not be completely optimised. But it offers a clear improvement. This was a targeted investigation and is not comprehensive in scope, there may be other places where similar improvements can be made.