Просмотр исходного кода

lang: Eliminate variable allocations that build up stack space for token extension code generation (#2913)

Sundeep Charan Ramkumar 1 год назад
Родитель
Сommit
827c986618
3 измененных файлов с 17 добавлено и 36 удалено
  1. 2 0
      CHANGELOG.md
  2. 1 1
      lang/Cargo.toml
  3. 14 35
      lang/syn/src/codegen/accounts/constraints.rs

+ 2 - 0
CHANGELOG.md

@@ -16,6 +16,8 @@ The minor version will be incremented upon a breaking change and the patch versi
 
 ### Fixes
 
+- syn: Eliminate variable allocations that build up stack space for token extension code generation ([#2913](https://github.com/coral-xyz/anchor/pull/2913)).
+
 ### Breaking
 
 ## [0.30.0] - 2024-04-15

+ 1 - 1
lang/Cargo.toml

@@ -60,4 +60,4 @@ bytemuck = "1"
 solana-program = "1.16"
 thiserror = "1"
 # TODO: Remove. This crate has been added to fix a build error with the 1.16.0 release.
-getrandom = { version = "0.2", features = ["custom"] }
+getrandom = { version = "0.2", features = ["custom"] }

+ 14 - 35
lang/syn/src/codegen/accounts/constraints.rs

@@ -886,67 +886,46 @@ fn generate_constraint_init_group(
                             for e in extensions {
                                 match e {
                                     ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::GroupPointer => {
-                                        let cpi_program = #token_program.to_account_info();
-                                        let accounts = ::anchor_spl::token_interface::GroupPointerInitialize {
+                                        ::anchor_spl::token_interface::group_pointer_initialize(anchor_lang::context::CpiContext::new(#token_program.to_account_info(), ::anchor_spl::token_interface::GroupPointerInitialize {
                                             token_program_id: #token_program.to_account_info(),
                                             mint: #field.to_account_info(),
-                                        };
-                                        let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
-                                        ::anchor_spl::token_interface::group_pointer_initialize(cpi_ctx, #group_pointer_authority, #group_pointer_group_address)?;
+                                        }), #group_pointer_authority, #group_pointer_group_address)?;
                                     },
                                     ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::GroupMemberPointer => {
-                                        let cpi_program = #token_program.to_account_info();
-                                        let accounts = ::anchor_spl::token_interface::GroupMemberPointerInitialize {
+                                        ::anchor_spl::token_interface::group_member_pointer_initialize(anchor_lang::context::CpiContext::new(#token_program.to_account_info(), ::anchor_spl::token_interface::GroupMemberPointerInitialize {
                                             token_program_id: #token_program.to_account_info(),
                                             mint: #field.to_account_info(),
-                                        };
-                                        let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
-                                        ::anchor_spl::token_interface::group_member_pointer_initialize(cpi_ctx, #group_member_pointer_authority, #group_member_pointer_member_address)?;
+                                        }), #group_member_pointer_authority, #group_member_pointer_member_address)?;
                                     },
                                     ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::MetadataPointer => {
-                                        let cpi_program = #token_program.to_account_info();
-                                        let accounts = ::anchor_spl::token_interface::MetadataPointerInitialize {
+                                        ::anchor_spl::token_interface::metadata_pointer_initialize(anchor_lang::context::CpiContext::new(#token_program.to_account_info(), ::anchor_spl::token_interface::MetadataPointerInitialize {
                                             token_program_id: #token_program.to_account_info(),
                                             mint: #field.to_account_info(),
-                                        };
-                                        let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
-                                        ::anchor_spl::token_interface::metadata_pointer_initialize(cpi_ctx, #metadata_pointer_authority, #metadata_pointer_metadata_address)?;
+                                        }), #metadata_pointer_authority, #metadata_pointer_metadata_address)?;
                                     },
                                     ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::MintCloseAuthority => {
-                                        let cpi_program = #token_program.to_account_info();
-                                        let accounts = ::anchor_spl::token_interface::MintCloseAuthorityInitialize {
+                                        ::anchor_spl::token_interface::mint_close_authority_initialize(anchor_lang::context::CpiContext::new(#token_program.to_account_info(), ::anchor_spl::token_interface::MintCloseAuthorityInitialize {
                                             token_program_id: #token_program.to_account_info(),
                                             mint: #field.to_account_info(),
-                                        };
-                                        let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
-                                        ::anchor_spl::token_interface::mint_close_authority_initialize(cpi_ctx, #close_authority)?;
+                                        }), #close_authority)?;
                                     },
                                     ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::TransferHook => {
-                                        let cpi_program = #token_program.to_account_info();
-                                        let accounts = ::anchor_spl::token_interface::TransferHookInitialize {
+                                        ::anchor_spl::token_interface::transfer_hook_initialize(anchor_lang::context::CpiContext::new(#token_program.to_account_info(), ::anchor_spl::token_interface::TransferHookInitialize {
                                             token_program_id: #token_program.to_account_info(),
                                             mint: #field.to_account_info(),
-                                        };
-                                        let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
-                                        ::anchor_spl::token_interface::transfer_hook_initialize(cpi_ctx, #transfer_hook_authority, #transfer_hook_program_id)?;
+                                        }), #transfer_hook_authority, #transfer_hook_program_id)?;
                                     },
                                     ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::NonTransferable => {
-                                        let cpi_program = #token_program.to_account_info();
-                                        let accounts = ::anchor_spl::token_interface::NonTransferableMintInitialize {
+                                        ::anchor_spl::token_interface::non_transferable_mint_initialize(anchor_lang::context::CpiContext::new(#token_program.to_account_info(), ::anchor_spl::token_interface::NonTransferableMintInitialize {
                                             token_program_id: #token_program.to_account_info(),
                                             mint: #field.to_account_info(),
-                                        };
-                                        let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
-                                        ::anchor_spl::token_interface::non_transferable_mint_initialize(cpi_ctx)?;
+                                        }))?;
                                     },
                                     ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::PermanentDelegate => {
-                                        let cpi_program = #token_program.to_account_info();
-                                        let accounts = ::anchor_spl::token_interface::PermanentDelegateInitialize {
+                                        ::anchor_spl::token_interface::permanent_delegate_initialize(anchor_lang::context::CpiContext::new(#token_program.to_account_info(), ::anchor_spl::token_interface::PermanentDelegateInitialize {
                                             token_program_id: #token_program.to_account_info(),
                                             mint: #field.to_account_info(),
-                                        };
-                                        let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
-                                        ::anchor_spl::token_interface::permanent_delegate_initialize(cpi_ctx, #permanent_delegate.unwrap())?;
+                                        }), #permanent_delegate.unwrap())?;
                                     },
                                     _ => {} // do nothing
                                 }