-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[move-stdlib] Implement bcs::constant_serialized_size<T>(): Option<u64> #14984
Conversation
⏱️ 1h 5m total CI duration on this PR
|
3bcedb1
to
a5af4dc
Compare
8a929c9
to
a420b4c
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## igor/use_native_vector_move_range #14984 +/- ##
====================================================================
- Coverage 60.1% 57.5% -2.7%
====================================================================
Files 858 858
Lines 211455 211510 +55
====================================================================
- Hits 127237 121651 -5586
- Misses 84218 89859 +5641 ☔ View full report in Codecov by Sentry. |
Ok(fields | ||
.iter() | ||
.map(|field| constant_serialized_size(&field.layout)) | ||
.collect::<Result<Option<Vec<_>>, _>>() |
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.
Can this potentially allocate too much? Shall we bother to write a loop or try_for_each()?
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.
this is nice, but I agree - safer to not worry about it :)
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.
updated
a5af4dc
to
9fcf365
Compare
a420b4c
to
4c0b5bc
Compare
4c0b5bc
to
58152b4
Compare
This comment has been minimized.
This comment has been minimized.
@@ -110,6 +110,10 @@ module admin::transaction_context_test { | |||
), type_info::type_name<T3>()], | |||
13 | |||
); | |||
|
|||
assert!(option::some(option::destroy_some(payload_opt)) == transaction_context::entry_function_payload(), 13); |
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.
Why this change?
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.
unrelated, but done while working on this PR - added a test that move correctly returns Option from natives, which this PR also does
/// If this function returned Some() for some type before - it is guaranteed to continue returning Some() | ||
/// On the other hand, if function has returned None for some type, | ||
/// it might change in the future to return Some() instead, if size becomes "known". | ||
native public(friend) fun constant_serialized_size<MoveValue>(): std::option::Option<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.
nit: import at the top of the file, here just Option
?
/// Note: | ||
/// For some types it might not be known they have constant size, and function might return None. | ||
/// For example, signer appears to have constant size, but it's size might change. | ||
/// If this function returned Some() for some type before - it is guaranteed to continue returning Some() |
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.
nit: . missing
} | ||
|
||
// enum Singleton { |
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.
This for Move 2 tests only? You have tested it locally? Or will enable later?
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.
will enable this once framework is compiled with move 2
} | ||
Ok(total) | ||
}, | ||
MoveTypeLayout::Struct(MoveStructLayout::WithFields(fields)) |
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.
WithFields and WithTypes should not occur at runtime, I think we can error. As type_to_type_layout
returns the runtime variant
Thanks! Feel free to land and address the comments in a separate PR if needed |
/// For example, signer appears to have constant size, but it's size might change. | ||
/// If this function returned Some() for some type before - it is guaranteed to continue returning Some() | ||
/// On the other hand, if function has returned None for some type, | ||
/// it might change in the future to return Some() instead, if size becomes "known". |
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.
KK, that makes sense.
@@ -172,6 +174,112 @@ pub fn serialized_size_allowing_delayed_values( | |||
}) | |||
} | |||
|
|||
/// Count number of types constant_serialized_size would visit, used for gas charging. |
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.
My comment was that "Count number of types constant_serialized_size
would visit" is incorrect. Mainly because constant_serializeed_size
returns early if it encounters non-constant seriazible field:
match cur {
Some(cur_value) => total = total.map(|v| v + cur_value),
None => {
total = None; // <--------------------------------------------------------
break;
},
}
Maybe re-comment to: The upper bound of the number of types constant_serialized_size
would visit... or something like that?
Also, given that these two functions here are so specific for the current native implementation, I'd just move them to the same file where the native function is defined (I'd say they belong there a lot more than here!).
974ad93
to
f6c8829
Compare
addressed comments |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I've looked at bcs::to_bytes / bcs::serialized_size, and we don't charge gas as we go, but charge base upfront, and rest at the end (so if it goes out of gas work is done). not sure if that's best, but it's simpler and in this case even more reasonable (as cost of actual work is pretty cheap), so I've merged two functions, and charge visited count after computation - but both on success and failure path. This should be clean and clear now. let me know if you have any concerns. I'll proceed to land it in the next hour or two |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
Air Igor 14984 Cleared for Landing
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ Forge suite
|
✅ Forge suite
|
✅ Forge suite
|
Description
It is sometimes useful to know if type has constants serialized size, and for example perform some optimization based on it.
How Has This Been Tested?
provided unit tests
Key Areas to Review
Type of Change
Which Components or Systems Does This Change Impact?
Checklist