Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

implement BoundedEncodedLen #8720

Merged
12 commits merged into from
May 4, 2021
Merged

implement BoundedEncodedLen #8720

12 commits merged into from
May 4, 2021

Conversation

coriolinus
Copy link
Contributor

Work moved from paritytech/parity-scale-codec#267

In support of #8719

@coriolinus coriolinus self-assigned this May 4, 2021
@github-actions github-actions bot added the A0-please_review Pull request needs code review. label May 4, 2021
@coriolinus coriolinus added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. labels May 4, 2021
@coriolinus coriolinus mentioned this pull request May 4, 2021
20 tasks
@coriolinus coriolinus added the D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit. label May 4, 2021
};
}

impl_tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
Copy link
Member

Choose a reason for hiding this comment

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

Please use impl_for_tuples for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok: 0204e2f.

frame/support/src/traits/bounded_encoded_length.rs Outdated Show resolved Hide resolved
$(
impl BoundedEncodedLen for $t {
fn max_encoded_len() -> usize {
// a compact encoding of a primitive can take 1 byte more than its actual size
Copy link
Member

Choose a reason for hiding this comment

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

But a primitive will never be compact encoded automatically?

You would need to implement the trait for Compact<T>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. d249f57

coriolinus and others added 3 commits May 4, 2021 11:31
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
S: Get<u32>,
BoundedVec<T, S>: Encode,
{
fn max_encoded_len() -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

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

the least significant comment: pity not to see SomeTrait { fn some_trait() }, why not trait MaxEncodedLen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason really; I like MaxEncodedLen because it's shorter and agrees with the trait's function. af0b814

T: BoundedEncodedLen,
{
// The compact encoding of a type usually requires fewer bytes, but can occupy 1 additional
// byte in the worst case.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be curious to know exactly which case takes 1 extra byte?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general, our Compact encoding uses two bits to indicate how many bytes follow. Therefore, we need an extra byte for all cases where T::count_ones() > T::BIT_WIDTH - 2. u8 example.

Copy link
Contributor

Choose a reason for hiding this comment

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

2^14 takes 4 bytes to be encoded in a compact way whereas it takes only 2 bytes to be encoded as a regular u16

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

LGTM

Turns out that u16 doesn't play nicely with the pattern; those values
take two extra bytes, where all other cases take one. :(
u64 => 9;
// https://github.com/paritytech/parity-scale-codec/blob/f0341dabb01aa9ff0548558abb6dcc5c31c669a1/src/compact.rs#L413
u128 => 17;
);
Copy link
Contributor

@gui1117 gui1117 May 4, 2021

Choose a reason for hiding this comment

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

we could add a test that ensures Compact(u8::max_value()).encode().len() is correctly 2 bytes etc... but anyway values are good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, agree. 8b8576d.

@coriolinus
Copy link
Contributor Author

bot merge

@ghost
Copy link

ghost commented May 4, 2021

Waiting for commit status.

@ghost ghost merged commit 6bfdaba into master May 4, 2021
@ghost ghost deleted the prgn-boundedencodedlen branch May 4, 2021 13:57
@athei
Copy link
Member

athei commented May 5, 2021

I like this. It should be feasible to write a derive macro in order to have this for composite data structures, right?

@coriolinus
Copy link
Contributor Author

coriolinus commented May 5, 2021 via email

nazar-pc pushed a commit to autonomys/substrate that referenced this pull request Aug 8, 2021
* implement BoundedEncodedLen

* update header

* update imports

* use impl_for_tuples instead of a custom macro

* remove redundant where clause

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* impl for Compact<T>

* impl BoundedEncodedLen for BoundedVec (paritytech#8727)

* impl BoundedEncodedLen for bool

* explicitly implement BoundedEncodedLen for each Compact form

Turns out that u16 doesn't play nicely with the pattern; those values
take two extra bytes, where all other cases take one. :(

* rename BoundedEncodedLen -> MaxEncodedLen

* add tests of compact encoded length

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants