Skip to content

chore: Remove abusive cloning #2663

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

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/BINARY_SIZE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Solana version: 1.17.0

| Program | Binary Size | +/- |
| ------- | ----------- | ------------------------ |
| bench | 764,032 | 🟢 **-389,704 (33.78%)** |
| bench | 735,800 | 🟢 **-417,936 (36.22%)** |

### Notable changes

Expand Down
48 changes: 24 additions & 24 deletions bench/COMPUTE_UNITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,53 @@ Solana version: 1.17.0
| accountInfo2 | 824 | 🟢 **-651 (44.14%)** |
| accountInfo4 | 1,319 | 🟢 **-645 (32.84%)** |
| accountInfo8 | 2,531 | 🟢 **-1,310 (34.11%)** |
| accountEmptyInit1 | 5,099 | 🟢 **-718 (12.34%)** |
| accountEmptyInit1 | 4,899 | 🟢 **-918 (15.78%)** |
| accountEmpty1 | 708 | 🟢 **-441 (38.38%)** |
| accountEmptyInit2 | 9,676 | 🟢 **-726 (6.98%)** |
| accountEmptyInit2 | 9,325 | 🟢 **-1,077 (10.35%)** |
| accountEmpty2 | 1,064 | 🟢 **-690 (39.34%)** |
| accountEmptyInit4 | 18,477 | 🟢 **-1,031 (5.29%)** |
| accountEmptyInit4 | 17,821 | 🟢 **-1,687 (8.65%)** |
| accountEmpty4 | 1,766 | 🟢 **-774 (30.47%)** |
| accountEmptyInit8 | 36,113 | 🟢 **-1,152 (3.09%)** |
| accountEmptyInit8 | 34,855 | 🟢 **-2,410 (6.47%)** |
| accountEmpty8 | 3,179 | 🟢 **-1,837 (36.62%)** |
| accountSizedInit1 | 5,187 | 🟢 **-737 (12.44%)** |
| accountSizedInit1 | 4,988 | 🟢 **-936 (15.80%)** |
| accountSized1 | 732 | 🟢 **-482 (39.70%)** |
| accountSizedInit2 | 9,851 | 🟢 **-829 (7.76%)** |
| accountSizedInit2 | 9,499 | 🟢 **-1,181 (11.06%)** |
| accountSized2 | 1,106 | 🟢 **-767 (40.95%)** |
| accountSizedInit4 | 18,876 | 🟢 **-1,094 (5.48%)** |
| accountSizedInit4 | 18,219 | 🟢 **-1,751 (8.77%)** |
| accountSized4 | 1,863 | 🟢 **-899 (32.55%)** |
| accountSizedInit8 | 36,832 | 🟢 **-1,290 (3.38%)** |
| accountSizedInit8 | 35,574 | 🟢 **-2,548 (6.68%)** |
| accountSized8 | 3,374 | 🟢 **-1,979 (36.97%)** |
| accountUnsizedInit1 | 5,275 | 🟢 **-777 (12.84%)** |
| accountUnsizedInit1 | 5,075 | 🟢 **-977 (16.14%)** |
| accountUnsized1 | 759 | 🟢 **-579 (43.27%)** |
| accountUnsizedInit2 | 10,090 | 🟢 **-839 (7.68%)** |
| accountUnsizedInit2 | 9,736 | 🟢 **-1,193 (10.92%)** |
| accountUnsized2 | 1,168 | 🟢 **-610 (34.31%)** |
| accountUnsizedInit4 | 19,281 | 🟢 **-1,058 (5.20%)** |
| accountUnsizedInit4 | 18,622 | 🟢 **-1,717 (8.44%)** |
| accountUnsized4 | 2,000 | 🟢 **-1,136 (36.22%)** |
| accountUnsizedInit8 | 37,378 | 🟢 **-1,718 (4.39%)** |
| accountUnsizedInit8 | 36,119 | 🟢 **-2,977 (7.61%)** |
| accountUnsized8 | 3,667 | 🟢 **-2,285 (38.39%)** |
| boxedAccountEmptyInit1 | 5,145 | 🟢 **-889 (14.73%)** |
| boxedAccountEmptyInit1 | 4,952 | 🟢 **-1,082 (17.93%)** |
| boxedAccountEmpty1 | 745 | 🟢 **-143 (16.10%)** |
| boxedAccountEmptyInit2 | 9,769 | 🟢 **-864 (8.13%)** |
| boxedAccountEmptyInit2 | 9,426 | 🟢 **-1,207 (11.35%)** |
| boxedAccountEmpty2 | 1,136 | 🟢 **-265 (18.92%)** |
| boxedAccountEmptyInit4 | 18,661 | 🟢 **-650 (3.37%)** |
| boxedAccountEmptyInit4 | 18,020 | 🟢 **-1,291 (6.69%)** |
| boxedAccountEmpty4 | 1,913 | 🟢 **-511 (21.08%)** |
| boxedAccountEmptyInit8 | 36,488 | 🟢 **-648 (1.74%)** |
| boxedAccountEmptyInit8 | 35,248 | 🟢 **-1,888 (5.08%)** |
| boxedAccountEmpty8 | 3,500 | 🟢 **-1,159 (24.88%)** |
| boxedAccountSizedInit1 | 5,222 | 🟢 **-908 (14.81%)** |
| boxedAccountSizedInit1 | 5,027 | 🟢 **-1,103 (17.99%)** |
| boxedAccountSized1 | 767 | 🟢 **-150 (16.36%)** |
| boxedAccountSizedInit2 | 9,925 | 🟢 **-903 (8.34%)** |
| boxedAccountSizedInit2 | 9,582 | 🟢 **-1,246 (11.51%)** |
| boxedAccountSized2 | 1,183 | 🟢 **-280 (19.14%)** |
| boxedAccountSizedInit4 | 18,973 | 🟢 **-730 (3.71%)** |
| boxedAccountSizedInit4 | 18,332 | 🟢 **-1,371 (6.96%)** |
| boxedAccountSized4 | 2,002 | 🟢 **-541 (21.27%)** |
| boxedAccountSizedInit8 | 37,111 | 🟢 **-808 (2.13%)** |
| boxedAccountSizedInit8 | 35,868 | 🟢 **-2,051 (5.41%)** |
| boxedAccountSized8 | 3,682 | 🟢 **-1,216 (24.83%)** |
| boxedAccountUnsizedInit1 | 5,304 | 🟢 **-936 (15.00%)** |
| boxedAccountUnsizedInit1 | 5,109 | 🟢 **-1,131 (18.13%)** |
| boxedAccountUnsized1 | 797 | 🟢 **-175 (18.00%)** |
| boxedAccountUnsizedInit2 | 10,089 | 🟢 **-959 (8.68%)** |
| boxedAccountUnsizedInit2 | 9,746 | 🟢 **-1,302 (11.78%)** |
| boxedAccountUnsized2 | 1,246 | 🟢 **-324 (20.64%)** |
| boxedAccountUnsizedInit4 | 19,303 | 🟢 **-835 (4.15%)** |
| boxedAccountUnsizedInit4 | 18,658 | 🟢 **-1,480 (7.35%)** |
| boxedAccountUnsized4 | 2,135 | 🟢 **-633 (22.87%)** |
| boxedAccountUnsizedInit8 | 37,770 | 🟢 **-1,030 (2.65%)** |
| boxedAccountUnsizedInit8 | 36,525 | 🟢 **-2,275 (5.86%)** |
| boxedAccountUnsized8 | 3,948 | 🟢 **-1,399 (26.16%)** |
| boxedInterfaceAccountMint1 | 2,085 | 🟢 **-211 (9.19%)** |
| boxedInterfaceAccountMint2 | 3,726 | 🟢 **-403 (9.76%)** |
Expand Down
52 changes: 33 additions & 19 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ pub fn generate_constraint_close(

pub fn generate_constraint_mut(f: &Field, c: &ConstraintMut) -> proc_macro2::TokenStream {
let ident = &f.ident;
let account_ref = generate_account_ref(f);
let error = generate_custom_error(ident, &c.error, quote! { ConstraintMut }, &None);
quote! {
if !#ident.to_account_info().is_writable {
if !#account_ref.is_writable {
return #error;
}
}
Expand Down Expand Up @@ -281,16 +282,11 @@ pub fn generate_constraint_has_one(

pub fn generate_constraint_signer(f: &Field, c: &ConstraintSigner) -> proc_macro2::TokenStream {
let ident = &f.ident;
let info = match f.ty {
Ty::AccountInfo => quote! { #ident },
Ty::Account(_) => quote! { #ident.to_account_info() },
Ty::InterfaceAccount(_) => quote! { #ident.to_account_info() },
Ty::AccountLoader(_) => quote! { #ident.to_account_info() },
_ => panic!("Invalid syntax: signer cannot be specified."),
};
let account_ref = generate_account_ref(f);

let error = generate_custom_error(ident, &c.error, quote! { ConstraintSigner }, &None);
quote! {
if !#info.is_signer {
if !#account_ref.is_signer {
return #error;
}
}
Expand Down Expand Up @@ -403,7 +399,7 @@ fn generate_constraint_realloc(
**__field_info.lamports.borrow_mut() = __field_info.lamports().checked_sub(__lamport_amt).unwrap();
}

#field.to_account_info().realloc(#new_space, #zero)?;
__field_info.realloc(#new_space, #zero)?;
__reallocs.insert(#field.key());
}
}
Expand All @@ -430,6 +426,8 @@ fn generate_constraint_init_group(
let from_account_info = f.from_account_info(Some(&c.kind), true);
let from_account_info_unchecked = f.from_account_info(Some(&c.kind), false);

let account_ref = generate_account_ref(f);

// PDA bump seeds.
let (find_pda, seeds_with_bump) = match &c.seeds {
None => (quote! {}, quote! {}),
Expand Down Expand Up @@ -547,7 +545,7 @@ fn generate_constraint_init_group(
// Checks that all the required accounts for this operation are present.
#optional_checks

let owner_program = AsRef::<AccountInfo>::as_ref(&#field).owner;
let owner_program = #account_ref.owner;
if !#if_needed || owner_program == &anchor_lang::solana_program::system_program::ID {
#payer_optional_check

Expand Down Expand Up @@ -618,7 +616,7 @@ fn generate_constraint_init_group(
// Checks that all the required accounts for this operation are present.
#optional_checks

let owner_program = AsRef::<AccountInfo>::as_ref(&#field).owner;
let owner_program = #account_ref.owner;
if !#if_needed || owner_program == &anchor_lang::solana_program::system_program::ID {
#payer_optional_check

Expand Down Expand Up @@ -796,7 +794,7 @@ fn generate_constraint_init_group(
// Checks that all the required accounts for this operation are present.
#optional_checks

let actual_field = #field.to_account_info();
let actual_field = #account_ref;
let actual_owner = actual_field.owner;

// Define the account space variable.
Expand Down Expand Up @@ -911,6 +909,7 @@ fn generate_constraint_associated_token(
) -> proc_macro2::TokenStream {
let name = &f.ident;
let name_str = name.to_string();
let account_ref = generate_account_ref(f);
let wallet_address = &c.wallet;
let spl_token_mint_address = &c.mint;

Expand All @@ -928,7 +927,7 @@ fn generate_constraint_associated_token(
let token_program_optional_check = optional_check_scope.generate_check(token_program);
quote! {
#token_program_optional_check
if #name.to_account_info().owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintAssociatedTokenTokenProgram.into()); }
if #account_ref.owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintAssociatedTokenTokenProgram.into()); }
}
}
None => quote! {},
Expand Down Expand Up @@ -967,6 +966,7 @@ fn generate_constraint_token_account(
accs: &AccountsStruct,
) -> proc_macro2::TokenStream {
let name = &f.ident;
let account_ref = generate_account_ref(f);
let mut optional_check_scope = OptionalCheckScope::new_with_field(accs, name);
let authority_check = match &c.authority {
Some(authority) => {
Expand All @@ -993,7 +993,7 @@ fn generate_constraint_token_account(
let token_program_optional_check = optional_check_scope.generate_check(token_program);
quote! {
#token_program_optional_check
if #name.to_account_info().owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintTokenTokenProgram.into()); }
if #account_ref.owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintTokenTokenProgram.into()); }
}
}
None => quote! {},
Expand All @@ -1013,6 +1013,7 @@ fn generate_constraint_mint(
accs: &AccountsStruct,
) -> proc_macro2::TokenStream {
let name = &f.ident;
let account_ref = generate_account_ref(f);

let decimal_check = match &c.decimals {
Some(decimals) => quote! {
Expand Down Expand Up @@ -1053,7 +1054,7 @@ fn generate_constraint_mint(
let token_program_optional_check = optional_check_scope.generate_check(token_program);
quote! {
#token_program_optional_check
if #name.to_account_info().owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintMintTokenProgram.into()); }
if #account_ref.owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintMintTokenProgram.into()); }
}
}
None => quote! {},
Expand Down Expand Up @@ -1192,13 +1193,13 @@ pub fn generate_constraint_executable(
f: &Field,
_c: &ConstraintExecutable,
) -> proc_macro2::TokenStream {
let name = &f.ident;
let name_str = name.to_string();
let name_str = f.ident.to_string();
let account_ref = generate_account_ref(f);

// because we are only acting on the field, we know it isnt optional at this point
// as it was unwrapped in `generate_constraint`
quote! {
if !#name.to_account_info().executable {
if !#account_ref.executable {
return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::ConstraintExecutable).with_account_name(#name_str));
}
}
Expand Down Expand Up @@ -1231,3 +1232,16 @@ fn generate_custom_error(
Err(#error)
}
}

fn generate_account_ref(field: &Field) -> proc_macro2::TokenStream {
let name = &field.ident;

match &field.ty {
Ty::AccountInfo => quote!(&#name),
Ty::Account(acc) if acc.boxed => quote!(AsRef::<AccountInfo>::as_ref(#name.as_ref())),
Ty::InterfaceAccount(acc) if acc.boxed => {
quote!(AsRef::<AccountInfo>::as_ref(#name.as_ref()))
}
_ => quote!(AsRef::<AccountInfo>::as_ref(&#name)),
}
}
13 changes: 7 additions & 6 deletions lang/syn/src/codegen/program/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
accounts.from.clone(),
accounts.to.clone(),
accounts.base.clone(),
accounts.system_program.to_account_info().clone(),
accounts.system_program.to_account_info(),
],
&[seeds],
)?;
Expand Down Expand Up @@ -209,15 +209,16 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
return Err(anchor_lang::error::ErrorCode::IdlAccountNotEmpty.into());
}

let new_account_space = accounts.idl.to_account_info().data_len().checked_add(std::cmp::min(
let idl_ref = AsRef::<AccountInfo>::as_ref(&accounts.idl);
let new_account_space = idl_ref.data_len().checked_add(std::cmp::min(
data_len
.checked_sub(accounts.idl.to_account_info().data_len())
.checked_sub(idl_ref.data_len())
.expect("data_len should always be >= the current account space"),
10_000,
))
.unwrap();

if new_account_space > accounts.idl.to_account_info().data_len() {
if new_account_space > idl_ref.data_len() {
let sysvar_rent = Rent::get()?;
let new_rent_minimum = sysvar_rent.minimum_balance(new_account_space);
anchor_lang::system_program::transfer(
Expand All @@ -229,10 +230,10 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
},
),
new_rent_minimum
.checked_sub(accounts.idl.to_account_info().lamports())
.checked_sub(idl_ref.lamports())
.unwrap(),
)?;
accounts.idl.to_account_info().realloc(new_account_space, false)?;
idl_ref.realloc(new_account_space, false)?;
}

Ok(())
Expand Down
Loading