-
Notifications
You must be signed in to change notification settings - Fork 94
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
Implement MaxEncodedLen
for all types that don't implement it and return 0
#465
Comments
parity-scale-codec/src/codec.rs Lines 251 to 258 in 86e5162
|
You want to implement the trait incorrectly since you rely on an implementation detail of another trait function? Are you trying to optimize memory usage or what? Maybe rather add a |
@ggwpez yes, I just described the problem that you can't get the amount of memory to pre-allocate on the stack. Your suggestion like adding such an API is fine too: trait MaybeMaxEncodedLen: Encode {
fn max_encoded_len() -> Option<usize> { None }
}
#[derive(Encode)]
struct S;
// derive macro expands to:
impl MaybeMaxEncodedLen for S {} //when MaxEncodedLen is not present in the derive
impl MaybeMaxEncodedLen for S { //when MaxEncodedLen is present in the derive
fn max_encoded_len() -> Option<usize> { Some(42) }
} |
Yeah, making Maybe something like this would be better? (I'm not convinced myself; just throwing out the idea.) pub trait EncodedLenBounds {
const ENCODED_LEN_BOUNDS: (usize, usize);
}
#[inline(never)] // <- Necessary to prevent stack overflow.
fn encode_on_stack<T: EncodedLenBounds>(value: T) {
let mut buffer = [0; T::ENCODED_LEN_BOUNDS.1];
todo!()
}
fn encode<T: EncodedLenBounds>(value: T) {
if T::ENCODED_LEN_BOUNDS.1 < 0x4000 {
encode_on_stack(value)
} else {
todo!();
}
} |
@koute why then do we need a minimum in this case? Also now we don't have |
Making
Well, it's not going to be easy, but it should be possible. |
@koute btw, I'm not sure about Why does #[derive(Encode)]
struct S { s: String, }
// in this case we have owned String type inside struct
// therefore, we cannot calculate the minimum and maximum size, since the string type is allocated on the heap |
Because there's always going to be a minimum.
The problem with
You can. The minimum's going to be |
@koute currently you cannot do |
Сoncept
For example, we have some type of data that can be encoded. We also have a function in the smart contract to send the encoded value to some other address.
If we add an implementation of
MaxEncodedLen
for all types that do not implement and return0
in themax_encoded_len()
method, then in this case thesend(...)
function will have the opportunity to choose where to encode the data type. Ifmax_encoded_len() == 0
then encoding is done on the heap, otherwise on the stack.The text was updated successfully, but these errors were encountered: