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

[to be discussed] lang: macro that calculates type size at compile time #1647

Open
paul-schaaf opened this issue Mar 18, 2022 · 5 comments
Open
Labels

Comments

@paul-schaaf
Copy link
Contributor

paul-schaaf commented Mar 18, 2022

TO BE DISCUSSED

Problem

space calculation via derive(Default) is gone in favour of size declarations at compile time.

This adds a little bit of work for users because they have to specify size in init even for types that are not dynamic.

Solution

derive(size) macro on types which expand into an impl with a const variable that defines the type's size by reading its inner types' lengths.

#[derive(size)]
pub struct MyData {
   pub value: u64,
   pub other_value: u8,
   pub authority: Pubkey
}

would result in the following added code:

impl MyData {
    pub const SIZE: usize = 8 + 1 + 32;
}
@Henry-E
Copy link
Contributor

Henry-E commented Mar 19, 2022

This would be much appreciated.

Perhaps there could also be an additional macro to indicate the maximum length of a dynamic type.

e.g.

#[derive(size)]
pub struct MyData {
   pub value: u64,
   pub other_value: u8,
   pub authority: Pubkey,
   #[max_len = 10]
   pub this_seed: Vec<u8>
}
impl MyData {
    pub const SIZE: usize = 8 + 1 + 32 + 10;
}

Naturally it's also important that this be able to work recursively for structs containing custom types.

e.g.

#[derive(size)]
pub struct MyData {
   #[max_len = 10]
   pub custom_accounts: Vec<CustomAccount>
}

#[derive(size)]
pub struct CustomAccount {
   pub value: u64,
   pub other_value: u8,
}
impl MyData {
    pub const SIZE: usize = 10 * ( 8 + 1);
}

@Arrowana
Copy link
Contributor

If it works for the most common dynamic length type Vec but also work out of the box for Option<T> that would be nice.

Another request, can we make this happen by default on the #[account] macro at the top and bail to ask user to implement SIZE/LEN or add more macro on specific fields? But then we might have issues with size definition in nested structs...

@callensm
Copy link
Member

callensm commented Jun 7, 2022

this feature is now much harder to achieve consistently since the introduction of account space reallocation. static assertions on dynamic type sizing isn't feasible anymore and the data fields within an account struct are no longer indicative of the actual space allocated to the account.

@armaniferrante
Copy link
Member

This would have to refer not to the account's data slice size, but to the minimum size to required to store the type, which would still help for things like default space allocation.

@callensm
Copy link
Member

callensm commented Jun 7, 2022

This would have to refer not to the account's data slice size, but to the minimum size to required to store the type, which would still help for things like default space allocation.

i guess thats true. once the account has been initialized with the default space allocation, the default space calculation isn't very relevant afterwards. could be misleading if people use it for data size comp filters on program account reads though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants