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

[framework] Improves performance of BCS implementation #5407

Merged
merged 6 commits into from
Oct 23, 2022
Merged

Conversation

damirka
Copy link
Contributor

@damirka damirka commented Oct 20, 2022

No description provided.

@damirka damirka added the move label Oct 20, 2022
@damirka damirka self-assigned this Oct 20, 2022
@amnn
Copy link
Contributor

amnn commented Oct 20, 2022

I think it looks great! The only thing that gave me pause was peel_vec_vec_u8 which seems like a slippery slope. I've left a comment there with some suggestions.

Regarding BCS having store -- I think that's fine. It doesn't enable anything that people couldn't already do by converting to bytes and storing those.

crates/sui-framework/sources/bcs.move Outdated Show resolved Hide resolved
crates/sui-framework/sources/bcs.move Outdated Show resolved Hide resolved
}

/// 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 {
Copy link
Contributor

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>> {
Copy link
Contributor

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.

Copy link
Contributor

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.

@damirka damirka merged commit 5dd4a1d into main Oct 23, 2022
@damirka damirka deleted the ds/bcs-everywhere branch October 23, 2022 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants