diff --git a/CHANGELOG.md b/CHANGELOG.md index 953df7ff99..06e41408e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ The minor version will be incremented upon a breaking change and the patch versi - cli: Fix template code shouldn't escape ([#3210](https://github.com/coral-xyz/anchor/pull/3210)). - idl: Fix using `address` constraint with non-const expressions ([#3216](https://github.com/coral-xyz/anchor/pull/3216)). - idl: Fix using full path types with `Program` ([#3228](https://github.com/coral-xyz/anchor/pull/3228)). +- lang: Use closures for `init` constraints to reduce the stack usage of `try_accounts` ([#2939](https://github.com/coral-xyz/anchor/pull/2939)). ### Breaking diff --git a/bench/BINARY_SIZE.md b/bench/BINARY_SIZE.md index 8c2718d54e..7d4315b0dc 100644 --- a/bench/BINARY_SIZE.md +++ b/bench/BINARY_SIZE.md @@ -16,12 +16,14 @@ The programs and their tests are located in [/tests/bench](https://github.com/co Solana version: 1.18.17 -| Program | Binary Size | - | -| ------- | ----------- | --------------------- | -| bench | 787,968 | 🟢 **-3,040 (0.38%)** | +| Program | Binary Size | - | +| ------- | ----------- | ------------------------ | +| bench | 1,096,096 | 🔴 **+305,088 (38.57%)** | ### Notable changes +- lang: Use closures for `init` constraints to reduce the stack usage of `try_accounts`. Note that the binary size increase is exaggerated for the bench program because it uses unusual number of `init` constraints. Impact for normal programs will be a lot less severe. ([#2939](https://github.com/coral-xyz/anchor/pull/2939)). + --- ## [0.30.1] diff --git a/bench/COMPUTE_UNITS.md b/bench/COMPUTE_UNITS.md index 8df52c30a2..bb4c3ccff2 100644 --- a/bench/COMPUTE_UNITS.md +++ b/bench/COMPUTE_UNITS.md @@ -16,95 +16,95 @@ The programs and their tests are located in [/tests/bench](https://github.com/co Solana version: 1.18.17 -| Instruction | Compute Units | - | -| --------------------------- | ------------- | -------------------- | -| accountInfo1 | 573 | 🟢 **-28 (4.66%)** | -| accountInfo2 | 899 | 🟢 **-24 (2.60%)** | -| accountInfo4 | 1,561 | 🟢 **-22 (1.39%)** | -| accountInfo8 | 2,957 | 🟢 **-18 (0.61%)** | -| accountEmptyInit1 | 4,976 | 🟢 **-58 (1.15%)** | -| accountEmpty1 | 649 | 🟢 **-3 (0.46%)** | -| accountEmptyInit2 | 9,590 | 🟢 **-97 (1.00%)** | -| accountEmpty2 | 1,015 | 🟢 **-1 (0.10%)** | -| accountEmptyInit4 | 18,323 | 🟢 **-178 (0.96%)** | -| accountEmpty4 | 1,740 | 🔴 **+3 (0.17%)** | -| accountEmptyInit8 | 35,827 | 🟢 **-342 (0.95%)** | -| accountEmpty8 | 3,193 | 🔴 **+7 (0.22%)** | -| accountSizedInit1 | 5,070 | 🟢 **-36 (0.71%)** | -| accountSized1 | 690 | 🔴 **+22 (3.29%)** | -| accountSizedInit2 | 9,750 | 🟢 **-78 (0.79%)** | -| accountSized2 | 1,069 | 🔴 **+23 (2.20%)** | -| accountSizedInit4 | 18,677 | 🟢 **-160 (0.85%)** | -| accountSized4 | 1,834 | 🔴 **+27 (1.49%)** | -| accountSizedInit8 | 36,426 | 🟢 **-335 (0.91%)** | -| accountSized8 | 3,357 | 🔴 **+31 (0.93%)** | -| accountUnsizedInit1 | 5,190 | 🟢 **-9 (0.17%)** | -| accountUnsized1 | 747 | 🔴 **+45 (6.41%)** | -| accountUnsizedInit2 | 10,033 | 🟢 **-45 (0.45%)** | -| accountUnsized2 | 1,165 | 🔴 **+49 (4.39%)** | -| accountUnsizedInit4 | 19,134 | 🟢 **-125 (0.65%)** | -| accountUnsized4 | 2,004 | 🔴 **+51 (2.61%)** | -| accountUnsizedInit8 | 37,046 | 🟢 **-285 (0.76%)** | -| accountUnsized8 | 3,679 | 🔴 **+53 (1.46%)** | -| boxedAccountEmptyInit1 | 5,078 | 🔴 **+14 (0.28%)** | -| boxedAccountEmpty1 | 740 | 🔴 **+69 (10.28%)** | -| boxedAccountEmptyInit2 | 9,697 | 🟢 **-24 (0.25%)** | -| boxedAccountEmpty2 | 1,125 | 🔴 **+73 (6.94%)** | -| boxedAccountEmptyInit4 | 18,477 | 🟢 **-105 (0.57%)** | -| boxedAccountEmpty4 | 1,886 | 🔴 **+75 (4.14%)** | -| boxedAccountEmptyInit8 | 36,059 | 🟢 **-270 (0.74%)** | -| boxedAccountEmpty8 | 3,435 | 🔴 **+78 (2.32%)** | -| boxedAccountSizedInit1 | 5,157 | 🔴 **+38 (0.74%)** | -| boxedAccountSized1 | 780 | 🔴 **+94 (13.70%)** | -| boxedAccountSizedInit2 | 9,842 | 🟢 **-3 (0.03%)** | -| boxedAccountSized2 | 1,180 | 🔴 **+95 (8.76%)** | -| boxedAccountSizedInit4 | 18,736 | 🟢 **-89 (0.47%)** | -| boxedAccountSized4 | 1,974 | 🔴 **+100 (5.34%)** | -| boxedAccountSizedInit8 | 36,563 | 🟢 **-261 (0.71%)** | -| boxedAccountSized8 | 3,593 | 🔴 **+103 (2.95%)** | -| boxedAccountUnsizedInit1 | 5,271 | 🔴 **+64 (1.23%)** | -| boxedAccountUnsized1 | 839 | 🔴 **+118 (16.37%)** | -| boxedAccountUnsizedInit2 | 10,040 | 🔴 **+25 (0.25%)** | -| boxedAccountUnsized2 | 1,277 | 🔴 **+120 (10.37%)** | -| boxedAccountUnsizedInit4 | 19,108 | 🟢 **-52 (0.27%)** | -| boxedAccountUnsized4 | 2,142 | 🔴 **+123 (6.09%)** | -| boxedAccountUnsizedInit8 | 37,283 | 🟢 **-213 (0.57%)** | -| boxedAccountUnsized8 | 3,903 | 🔴 **+127 (3.36%)** | -| boxedInterfaceAccountMint1 | 1,502 | 🔴 **+130 (9.48%)** | -| boxedInterfaceAccountMint2 | 2,423 | 🔴 **+130 (5.67%)** | -| boxedInterfaceAccountMint4 | 4,256 | 🔴 **+135 (3.28%)** | -| boxedInterfaceAccountMint8 | 7,950 | 🔴 **+139 (1.78%)** | -| boxedInterfaceAccountToken1 | 2,198 | 🔴 **+142 (6.91%)** | -| boxedInterfaceAccountToken2 | 3,803 | 🔴 **+143 (3.91%)** | -| boxedInterfaceAccountToken4 | 7,004 | 🔴 **+146 (2.13%)** | -| boxedInterfaceAccountToken8 | 13,434 | 🔴 **+150 (1.13%)** | -| interfaceAccountMint1 | 1,626 | 🔴 **+154 (10.46%)** | -| interfaceAccountMint2 | 2,788 | 🔴 **+157 (5.97%)** | -| interfaceAccountMint4 | 5,110 | 🔴 **+159 (3.21%)** | -| interfaceAccountMint8 | 9,749 | 🔴 **+161 (1.68%)** | -| interfaceAccountToken1 | 2,296 | 🔴 **+166 (7.79%)** | -| interfaceAccountToken2 | 4,096 | 🔴 **+168 (4.28%)** | -| interfaceAccountToken4 | 7,692 | 🔴 **+171 (2.27%)** | -| interface1 | 774 | 🔴 **+174 (29.00%)** | -| interface2 | 923 | 🔴 **+178 (23.89%)** | -| interface4 | 1,214 | 🔴 **+181 (17.52%)** | -| interface8 | 1,799 | 🔴 **+183 (11.32%)** | -| program1 | 782 | 🔴 **+186 (31.21%)** | -| program2 | 927 | 🔴 **+190 (25.78%)** | -| program4 | 1,210 | 🔴 **+191 (18.74%)** | -| program8 | 1,779 | 🔴 **+195 (12.31%)** | -| signer1 | 779 | 🔴 **+199 (34.31%)** | -| signer2 | 1,074 | 🔴 **+202 (23.17%)** | -| signer4 | 1,657 | 🔴 **+203 (13.96%)** | -| signer8 | 2,826 | 🔴 **+208 (7.94%)** | -| systemAccount1 | 802 | 🔴 **+210 (35.47%)** | -| systemAccount2 | 1,108 | 🔴 **+214 (23.94%)** | -| systemAccount4 | 1,713 | 🔴 **+216 (14.43%)** | -| systemAccount8 | 2,926 | 🔴 **+219 (8.09%)** | -| uncheckedAccount1 | 785 | 🔴 **+222 (39.43%)** | -| uncheckedAccount2 | 1,061 | 🔴 **+225 (26.91%)** | -| uncheckedAccount4 | 1,604 | 🔴 **+226 (16.40%)** | -| uncheckedAccount8 | 2,699 | 🔴 **+231 (9.36%)** | +| Instruction | Compute Units | - | +| --------------------------- | ------------- | --------------------- | +| accountInfo1 | 573 | 🟢 **-28 (4.66%)** | +| accountInfo2 | 899 | 🟢 **-24 (2.60%)** | +| accountInfo4 | 1,561 | 🟢 **-22 (1.39%)** | +| accountInfo8 | 2,957 | 🟢 **-18 (0.61%)** | +| accountEmptyInit1 | 5,014 | 🟢 **-20 (0.40%)** | +| accountEmpty1 | 649 | 🟢 **-3 (0.46%)** | +| accountEmptyInit2 | 9,155 | 🟢 **-532 (5.49%)** | +| accountEmpty2 | 1,015 | 🟢 **-1 (0.10%)** | +| accountEmptyInit4 | 17,465 | 🟢 **-1,036 (5.60%)** | +| accountEmpty4 | 1,740 | 🔴 **+3 (0.17%)** | +| accountEmptyInit8 | 34,115 | 🟢 **-2,054 (5.68%)** | +| accountEmpty8 | 3,193 | 🔴 **+7 (0.22%)** | +| accountSizedInit1 | 5,107 | 🔴 **+1 (0.02%)** | +| accountSized1 | 690 | 🔴 **+22 (3.29%)** | +| accountSizedInit2 | 9,313 | 🟢 **-515 (5.24%)** | +| accountSized2 | 1,069 | 🔴 **+23 (2.20%)** | +| accountSizedInit4 | 17,812 | 🟢 **-1,025 (5.44%)** | +| accountSized4 | 1,834 | 🔴 **+27 (1.49%)** | +| accountSizedInit8 | 34,706 | 🟢 **-2,055 (5.59%)** | +| accountSized8 | 3,357 | 🔴 **+31 (0.93%)** | +| accountUnsizedInit1 | 5,227 | 🔴 **+28 (0.54%)** | +| accountUnsized1 | 746 | 🔴 **+44 (6.27%)** | +| accountUnsizedInit2 | 9,599 | 🟢 **-479 (4.75%)** | +| accountUnsized2 | 1,163 | 🔴 **+47 (4.21%)** | +| accountUnsizedInit4 | 18,276 | 🟢 **-983 (5.10%)** | +| accountUnsized4 | 2,000 | 🔴 **+47 (2.41%)** | +| accountUnsizedInit8 | 35,328 | 🟢 **-2,003 (5.37%)** | +| accountUnsized8 | 3,671 | 🔴 **+45 (1.24%)** | +| boxedAccountEmptyInit1 | 5,106 | 🔴 **+42 (0.83%)** | +| boxedAccountEmpty1 | 740 | 🔴 **+69 (10.28%)** | +| boxedAccountEmptyInit2 | 9,268 | 🟢 **-453 (4.66%)** | +| boxedAccountEmpty2 | 1,125 | 🔴 **+73 (6.94%)** | +| boxedAccountEmptyInit4 | 17,619 | 🟢 **-963 (5.18%)** | +| boxedAccountEmpty4 | 1,886 | 🔴 **+75 (4.14%)** | +| boxedAccountEmptyInit8 | 34,346 | 🟢 **-1,983 (5.46%)** | +| boxedAccountEmpty8 | 3,435 | 🔴 **+78 (2.32%)** | +| boxedAccountSizedInit1 | 5,189 | 🔴 **+70 (1.37%)** | +| boxedAccountSized1 | 780 | 🔴 **+94 (13.70%)** | +| boxedAccountSizedInit2 | 9,411 | 🟢 **-434 (4.41%)** | +| boxedAccountSized2 | 1,180 | 🔴 **+95 (8.76%)** | +| boxedAccountSizedInit4 | 17,879 | 🟢 **-946 (5.03%)** | +| boxedAccountSized4 | 1,974 | 🔴 **+100 (5.34%)** | +| boxedAccountSizedInit8 | 34,842 | 🟢 **-1,982 (5.38%)** | +| boxedAccountSized8 | 3,593 | 🔴 **+103 (2.95%)** | +| boxedAccountUnsizedInit1 | 5,300 | 🔴 **+93 (1.79%)** | +| boxedAccountUnsized1 | 838 | 🔴 **+117 (16.23%)** | +| boxedAccountUnsizedInit2 | 9,609 | 🟢 **-406 (4.05%)** | +| boxedAccountUnsized2 | 1,275 | 🔴 **+118 (10.20%)** | +| boxedAccountUnsizedInit4 | 18,251 | 🟢 **-909 (4.74%)** | +| boxedAccountUnsized4 | 2,138 | 🔴 **+119 (5.89%)** | +| boxedAccountUnsizedInit8 | 35,562 | 🟢 **-1,934 (5.16%)** | +| boxedAccountUnsized8 | 3,895 | 🔴 **+119 (3.15%)** | +| boxedInterfaceAccountMint1 | 1,502 | 🔴 **+130 (9.48%)** | +| boxedInterfaceAccountMint2 | 2,423 | 🔴 **+130 (5.67%)** | +| boxedInterfaceAccountMint4 | 4,256 | 🔴 **+135 (3.28%)** | +| boxedInterfaceAccountMint8 | 7,950 | 🔴 **+139 (1.78%)** | +| boxedInterfaceAccountToken1 | 2,198 | 🔴 **+142 (6.91%)** | +| boxedInterfaceAccountToken2 | 3,803 | 🔴 **+143 (3.91%)** | +| boxedInterfaceAccountToken4 | 7,004 | 🔴 **+146 (2.13%)** | +| boxedInterfaceAccountToken8 | 13,434 | 🔴 **+150 (1.13%)** | +| interfaceAccountMint1 | 1,626 | 🔴 **+154 (10.46%)** | +| interfaceAccountMint2 | 2,788 | 🔴 **+157 (5.97%)** | +| interfaceAccountMint4 | 5,110 | 🔴 **+159 (3.21%)** | +| interfaceAccountMint8 | 9,749 | 🔴 **+161 (1.68%)** | +| interfaceAccountToken1 | 2,296 | 🔴 **+166 (7.79%)** | +| interfaceAccountToken2 | 4,096 | 🔴 **+168 (4.28%)** | +| interfaceAccountToken4 | 7,692 | 🔴 **+171 (2.27%)** | +| interface1 | 774 | 🔴 **+174 (29.00%)** | +| interface2 | 923 | 🔴 **+178 (23.89%)** | +| interface4 | 1,214 | 🔴 **+181 (17.52%)** | +| interface8 | 1,799 | 🔴 **+183 (11.32%)** | +| program1 | 782 | 🔴 **+186 (31.21%)** | +| program2 | 927 | 🔴 **+190 (25.78%)** | +| program4 | 1,210 | 🔴 **+191 (18.74%)** | +| program8 | 1,779 | 🔴 **+195 (12.31%)** | +| signer1 | 779 | 🔴 **+199 (34.31%)** | +| signer2 | 1,074 | 🔴 **+202 (23.17%)** | +| signer4 | 1,657 | 🔴 **+203 (13.96%)** | +| signer8 | 2,826 | 🔴 **+208 (7.94%)** | +| systemAccount1 | 802 | 🔴 **+210 (35.47%)** | +| systemAccount2 | 1,108 | 🔴 **+214 (23.94%)** | +| systemAccount4 | 1,713 | 🔴 **+216 (14.43%)** | +| systemAccount8 | 2,926 | 🔴 **+219 (8.09%)** | +| uncheckedAccount1 | 785 | 🔴 **+222 (39.43%)** | +| uncheckedAccount2 | 1,061 | 🔴 **+225 (26.91%)** | +| uncheckedAccount4 | 1,604 | 🔴 **+226 (16.40%)** | +| uncheckedAccount8 | 2,699 | 🔴 **+231 (9.36%)** | ### Notable changes diff --git a/bench/STACK_MEMORY.md b/bench/STACK_MEMORY.md index 21621c8ee8..1d3abfeac8 100644 --- a/bench/STACK_MEMORY.md +++ b/bench/STACK_MEMORY.md @@ -108,6 +108,8 @@ Solana version: 1.18.17 ### Notable changes +- lang: Use closures for `init` constraints to reduce the stack usage of `try_accounts` ([#2939](https://github.com/coral-xyz/anchor/pull/2939)). + --- ## [0.30.1] diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 0d75c01a91..f210a0680e 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -562,7 +562,7 @@ fn generate_constraint_init_group( // Define the bump and pda variable. #find_pda - let #field: #ty_decl = { + let #field: #ty_decl = ({ #[inline(never)] || { // Checks that all the required accounts for this operation are present. #optional_checks @@ -596,8 +596,8 @@ fn generate_constraint_init_group( return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::ConstraintTokenTokenProgram).with_account_name(#name_str).with_pubkeys((*owner_program, #token_program.key()))); } } - pa - }; + Ok(pa) + }})()?; } } InitKind::AssociatedToken { @@ -633,7 +633,7 @@ fn generate_constraint_init_group( // Define the bump and pda variable. #find_pda - let #field: #ty_decl = { + let #field: #ty_decl = ({ #[inline(never)] || { // Checks that all the required accounts for this operation are present. #optional_checks @@ -671,8 +671,8 @@ fn generate_constraint_init_group( return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::AccountNotAssociatedTokenAccount).with_account_name(#name_str)); } } - pa - }; + Ok(pa) + }})()?; } } InitKind::Mint { @@ -888,7 +888,7 @@ fn generate_constraint_init_group( // Define the bump and pda variable. #find_pda - let #field: #ty_decl = { + let #field: #ty_decl = ({ #[inline(never)] || { // Checks that all the required accounts for this operation are present. #optional_checks @@ -980,8 +980,8 @@ fn generate_constraint_init_group( return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::ConstraintMintTokenProgram).with_account_name(#name_str).with_pubkeys((*owner_program, #token_program.key()))); } } - pa - }; + Ok(pa) + }})()?; } } InitKind::Program { owner } | InitKind::Interface { owner } => { @@ -1033,7 +1033,7 @@ fn generate_constraint_init_group( // Define the bump variable. #find_pda - let #field = { + let #field = ({ #[inline(never)] || { // Checks that all the required accounts for this operation are present. #optional_checks @@ -1078,8 +1078,8 @@ fn generate_constraint_init_group( } // Done. - pa - }; + Ok(pa) + }})()?; } } } diff --git a/tests/bench/bench.json b/tests/bench/bench.json index d07a4db634..406144b31d 100644 --- a/tests/bench/bench.json +++ b/tests/bench/bench.json @@ -933,61 +933,61 @@ "solanaVersion": "1.18.17", "result": { "binarySize": { - "bench": 787968 + "bench": 1096096 }, "computeUnits": { "accountInfo1": 573, "accountInfo2": 899, "accountInfo4": 1561, "accountInfo8": 2957, - "accountEmptyInit1": 4976, + "accountEmptyInit1": 5014, "accountEmpty1": 649, - "accountEmptyInit2": 9590, + "accountEmptyInit2": 9155, "accountEmpty2": 1015, - "accountEmptyInit4": 18323, + "accountEmptyInit4": 17465, "accountEmpty4": 1740, - "accountEmptyInit8": 35827, + "accountEmptyInit8": 34115, "accountEmpty8": 3193, - "accountSizedInit1": 5070, + "accountSizedInit1": 5107, "accountSized1": 690, - "accountSizedInit2": 9750, + "accountSizedInit2": 9313, "accountSized2": 1069, - "accountSizedInit4": 18677, + "accountSizedInit4": 17812, "accountSized4": 1834, - "accountSizedInit8": 36426, + "accountSizedInit8": 34706, "accountSized8": 3357, - "accountUnsizedInit1": 5190, - "accountUnsized1": 747, - "accountUnsizedInit2": 10033, - "accountUnsized2": 1165, - "accountUnsizedInit4": 19134, - "accountUnsized4": 2004, - "accountUnsizedInit8": 37046, - "accountUnsized8": 3679, - "boxedAccountEmptyInit1": 5078, + "accountUnsizedInit1": 5227, + "accountUnsized1": 746, + "accountUnsizedInit2": 9599, + "accountUnsized2": 1163, + "accountUnsizedInit4": 18276, + "accountUnsized4": 2000, + "accountUnsizedInit8": 35328, + "accountUnsized8": 3671, + "boxedAccountEmptyInit1": 5106, "boxedAccountEmpty1": 740, - "boxedAccountEmptyInit2": 9697, + "boxedAccountEmptyInit2": 9268, "boxedAccountEmpty2": 1125, - "boxedAccountEmptyInit4": 18477, + "boxedAccountEmptyInit4": 17619, "boxedAccountEmpty4": 1886, - "boxedAccountEmptyInit8": 36059, + "boxedAccountEmptyInit8": 34346, "boxedAccountEmpty8": 3435, - "boxedAccountSizedInit1": 5157, + "boxedAccountSizedInit1": 5189, "boxedAccountSized1": 780, - "boxedAccountSizedInit2": 9842, + "boxedAccountSizedInit2": 9411, "boxedAccountSized2": 1180, - "boxedAccountSizedInit4": 18736, + "boxedAccountSizedInit4": 17879, "boxedAccountSized4": 1974, - "boxedAccountSizedInit8": 36563, + "boxedAccountSizedInit8": 34842, "boxedAccountSized8": 3593, - "boxedAccountUnsizedInit1": 5271, - "boxedAccountUnsized1": 839, - "boxedAccountUnsizedInit2": 10040, - "boxedAccountUnsized2": 1277, - "boxedAccountUnsizedInit4": 19108, - "boxedAccountUnsized4": 2142, - "boxedAccountUnsizedInit8": 37283, - "boxedAccountUnsized8": 3903, + "boxedAccountUnsizedInit1": 5300, + "boxedAccountUnsized1": 838, + "boxedAccountUnsizedInit2": 9609, + "boxedAccountUnsized2": 1275, + "boxedAccountUnsizedInit4": 18251, + "boxedAccountUnsized4": 2138, + "boxedAccountUnsizedInit8": 35562, + "boxedAccountUnsized8": 3895, "boxedInterfaceAccountMint1": 1502, "boxedInterfaceAccountMint2": 2423, "boxedInterfaceAccountMint4": 4256,