-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
[framework] Improves performance of BCS implementation #5407
Conversation
I think it looks great! The only thing that gave me pause was Regarding |
} | ||
|
||
/// Read `u64` value from bcs-serialized bytes. | ||
public fun peel_u64(bcs: &mut vector<u8>): u64 { | ||
assert!(v::length(bcs) >= 8, EOutOfRange); | ||
public fun peel_u64(bcs: &mut BCS): u64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have missed something, but I think this can be implemented as one loop, rather than peeling two u32s:
public fun peel_u64(bcs: &mut BCS): u64 {
assert!(v::length(&bcs.bytes) >= 8, EOutOfRange);
let (value, i) = (0u64, 0u8);
while (i < 64) {
let byte = v::pop_back(&mut bcs.bytes) as u64;
value = value + (byte << i);
i = i + 8;
};
value
}
@@ -151,8 +193,18 @@ module sui::bcs { | |||
res | |||
} | |||
|
|||
/// Peel a `vector<vector<u8>>` (eg vec of string) from serialized bytes. | |||
public fun peel_vec_vec_u8(bcs: &mut BCS): vector<vector<u8>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are your thoughts on encouraging people who need nested vector constructs like this to use peel_vec_length
and a manual loop themselves, for now? Otherwise we risk the API surface exploding with options.
In the longer-term, I could see macros giving you the expressivity you need. If that doesn't materialise, then there are other options as well, like ML-style signatures/functors. There is also the silver bullet of implementing peel<T>
as a native function, which can allow for arbitrarily nested T
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@damirka and I chatted about this offline -- this function is morally peel_vec_string
, which makes it feel less like the start of a slippery slope to vec_vec_..._vec_{bool,u8,u64,u128}
. It's possible that the native function may still be the best approach? Interested to hear people's thoughts on that, but that seems out of scope of this PR, so I won't block its land for that.
Co-authored-by: Ashok Menon <ashok@mystenlabs.com>
Co-authored-by: Ashok Menon <ashok@mystenlabs.com>
No description provided.