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

Decode with mem limit #616

Merged
merged 24 commits into from
Oct 9, 2024
Merged

Conversation

serban300
Copy link
Contributor

@serban300 serban300 commented Jul 24, 2024

Related to #609

Ended up with almost the same approach proposed by @ggwpez in #602, but made it type safe through a marker trait.

This is not a breaking change.

The inconvenience is that DecodeWithMemTracking will need to be implemented manually for each struct.

@serban300 serban300 marked this pull request as ready for review July 24, 2024 15:15
@serban300 serban300 requested review from koute, bkchr and ggwpez July 24, 2024 15:15
@acatangiu
Copy link

@bkchr @ggwpez this is important and becoming stale, please review

@gui1117
Copy link
Contributor

gui1117 commented Sep 9, 2024

The current implementation is enough for the usage or should we also implement it automatically in derive macro?

EDIT: I feel it shouldn't be difficult, except for skipped fields, either we can ignore them or we should ask optionally to give the allocation needed for the default field. like #[codec(skipped(allocation = 1000))]

Copy link
Contributor

@gui1117 gui1117 left a comment

Choose a reason for hiding this comment

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

overall looks good to me, some comments.

src/codec.rs Outdated Show resolved Hide resolved
src/codec.rs Outdated Show resolved Hide resolved
src/codec.rs Show resolved Hide resolved
@serban300
Copy link
Contributor Author

The current implementation is enough for the usage or should we also implement it automatically in derive macro?

EDIT: I feel it shouldn't be difficult, except for skipped fields, either we can ignore them or we should ask optionally to give the allocation needed for the default field. like #[codec(skipped(allocation = 1000))]

@gui1117 thank you very much for the review !

Yes, it would be nice to also implement it automatically. Normally it should be an auto trait, but this feature is still unstable so I couldn't use that.

Implementing it automatically in the derive macros would be the next best thing, but I didn't manage to do it. For example for:

struct Test 
{
    a: u32
}

I tried something that would expand to:

impl DecodeWithMemTracking for Test where u32: DecodeWithMemTracking {
}

Which generates compile errors.

So probably we'll need to implement it manually for each custom struct.

@gui1117
Copy link
Contributor

gui1117 commented Sep 10, 2024

Yes, it would be nice to also implement it automatically. Normally it should be an auto trait, but this feature is still unstable so I couldn't use that.

Implementing it automatically in the derive macros would be the next best thing, but I didn't manage to do it. For example for:

struct Test 
{
    a: u32
}

I tried something that would expand to:

impl DecodeWithMemTracking for Test where u32: DecodeWithMemTracking {
}

Which generates compile errors.

So probably we'll need to implement it manually for each custom struct.

I see we can't derive it automatically with Decode derive-macro, but we could provide a DecodeWithMem derive-macro.

This macro will do the bound same as Encode/Decode, i.e. bound only the generic types. and it will fail if some concrete types don't implement DecodeWithMemTracking.

It would still be more handy than manually write the code I guess.

Copy link
Contributor

@gui1117 gui1117 left a comment

Choose a reason for hiding this comment

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

I suggested some more types to implement the trait on.

But part from this it looks good to me.

src/mem_tracking.rs Show resolved Hide resolved
tests/mem_tracking.rs Show resolved Hide resolved
@serban300
Copy link
Contributor Author

I see we can't derive it automatically with Decode derive-macro, but we could provide a DecodeWithMem derive-macro.

This macro will do the bound same as Encode/Decode, i.e. bound only the generic types. and it will fail if some concrete types don't implement DecodeWithMemTracking.

It would still be more handy than manually write the code I guess.

I'm not sure if it's possible. Unless we add a mock method to the DecodeWithMemTracking trait and try to call it in the derived implementation. Which would be a bit of a hack. But even so, implementing it manually isn't so hard. It's just an empty marker trait. I'm not sure if deriving it would make things much easier.

@gui1117
Copy link
Contributor

gui1117 commented Sep 10, 2024

I see we can't derive it automatically with Decode derive-macro, but we could provide a DecodeWithMem derive-macro.
This macro will do the bound same as Encode/Decode, i.e. bound only the generic types. and it will fail if some concrete types don't implement DecodeWithMemTracking.
It would still be more handy than manually write the code I guess.

I'm not sure if it's possible. Unless we add a mock method to the DecodeWithMemTracking trait and try to call it in the derived implementation. Which would be a bit of a hack. But even so, implementing it manually isn't so hard. It's just an empty marker trait. I'm not sure if deriving it would make things much easier.

I see it now, indeed. :-/

Copy link
Contributor

@gui1117 gui1117 left a comment

Choose a reason for hiding this comment

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

Looks good to me.

I still think requiring to implement it manually can be painful. But if the types requiring this are not many then it good.

src/codec.rs Outdated Show resolved Hide resolved
src/codec.rs Outdated Show resolved Hide resolved
src/codec.rs Outdated Show resolved Hide resolved
src/mem_tracking.rs Show resolved Hide resolved
tests/mem_tracking.rs Show resolved Hide resolved
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Did you try this will work on Substrate? I mean you will need to ensure that Call implements DecodeWithMemTracking and all the types we are using there.

Also you should expose a function decode_with_mem_limit instead of exposing the MemLimitInput

src/codec.rs Outdated Show resolved Hide resolved
src/codec.rs Outdated Show resolved Hide resolved
@serban300
Copy link
Contributor Author

serban300 commented Sep 12, 2024

Did you try this will work on Substrate? I mean you will need to ensure that Call implements DecodeWithMemTracking and all the types we are using there.

Oh, you're right. I can patch polkadot-sdk locally and try this. Will do it and get back.

Also you should expose a function decode_with_mem_limit instead of exposing the MemLimitInput

Yes, will do this too.

This reverts commit 5b1b387.
src/codec.rs Outdated Show resolved Hide resolved
@serban300
Copy link
Contributor Author

I still think requiring to implement it manually can be painful. But if the types requiring this are not many then it good.

Yes, sorry, you're right. We need to be able to derive it since for types like RuntimeCall we can't implement it manually. Done.

@serban300
Copy link
Contributor Author

serban300 commented Sep 26, 2024

Did you try this will work on Substrate? I mean you will need to ensure that Call implements DecodeWithMemTracking and all the types we are using there.

Oh, you're right. I can patch polkadot-sdk locally and try this. Will do it and get back.

Done. This compiles for rococo-runtime: https://github.com/serban300/polkadot-sdk/tree/mem-limit-decode . It's very verbose but seems to be implemented for all the types that we need. Also had to add logic for deriving DecodeWithMemTracking.

Also you should expose a function decode_with_mem_limit instead of exposing the MemLimitInput

Yes, will do this too.

Done

@bkchr Addressed all the comments. PTAL !

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Some last nitpicks. Otherwise it already looks good :)

derive/src/utils.rs Outdated Show resolved Hide resolved
derive/src/utils.rs Outdated Show resolved Hide resolved
derive/src/lib.rs Outdated Show resolved Hide resolved
src/codec.rs Outdated Show resolved Hide resolved
src/codec.rs Outdated Show resolved Hide resolved
derive/src/decode.rs Outdated Show resolved Hide resolved
@serban300 serban300 requested a review from a team as a code owner October 1, 2024 07:03
src/btree_utils.rs Outdated Show resolved Hide resolved
src/compact.rs Outdated Show resolved Hide resolved
tests/mem_tracking.rs Show resolved Hide resolved
@serban300
Copy link
Contributor Author

@bkchr just wanted to check if you would like to take another look on the PR or if I could merge it as it is since it already has 2 approving reviews

src/compact.rs Outdated Show resolved Hide resolved
derive/src/decode.rs Show resolved Hide resolved
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Nice work 👍 Thank you for doing this!

derive/src/utils.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@serban300 serban300 merged commit be670ab into paritytech:master Oct 9, 2024
17 checks passed
@jsdw jsdw mentioned this pull request Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Status: Audited
Development

Successfully merging this pull request may close these issues.

5 participants