Skip to content

Commit 8faa8f0

Browse files
committed
* added stack space optimization using closures,
1 parent 940de79 commit 8faa8f0

File tree

2 files changed

+117
-9
lines changed

2 files changed

+117
-9
lines changed

lang/syn/src/codegen/accounts/constraints.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ fn generate_constraint_init_group(
545545
// Define the bump and pda variable.
546546
#find_pda
547547

548-
let #field: #ty_decl = {
548+
let #field: #ty_decl = (||{
549549
// Checks that all the required accounts for this operation are present.
550550
#optional_checks
551551

@@ -579,8 +579,8 @@ fn generate_constraint_init_group(
579579
return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::ConstraintTokenTokenProgram).with_account_name(#name_str).with_pubkeys((*owner_program, #token_program.key())));
580580
}
581581
}
582-
pa
583-
};
582+
Ok(pa)
583+
})()?;
584584
}
585585
}
586586
InitKind::AssociatedToken {
@@ -616,7 +616,7 @@ fn generate_constraint_init_group(
616616
// Define the bump and pda variable.
617617
#find_pda
618618

619-
let #field: #ty_decl = {
619+
let #field: #ty_decl = (||{
620620
// Checks that all the required accounts for this operation are present.
621621
#optional_checks
622622

@@ -652,8 +652,8 @@ fn generate_constraint_init_group(
652652
return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::AccountNotAssociatedTokenAccount).with_account_name(#name_str));
653653
}
654654
}
655-
pa
656-
};
655+
Ok(pa)
656+
})()?;
657657
}
658658
}
659659
InitKind::Mint {
@@ -1012,7 +1012,7 @@ fn generate_constraint_init_group(
10121012
// Define the bump variable.
10131013
#find_pda
10141014

1015-
let #field = {
1015+
let #field = (||{
10161016
// Checks that all the required accounts for this operation are present.
10171017
#optional_checks
10181018

@@ -1057,8 +1057,8 @@ fn generate_constraint_init_group(
10571057
}
10581058

10591059
// Done.
1060-
pa
1061-
};
1060+
Ok(pa)
1061+
})()?;
10621062
}
10631063
}
10641064
}

tests/zero-copy/programs/zero-copy/src/lib.rs

+108
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,114 @@ pub struct CreateBar<'info> {
101101
space = Bar::LEN + 8
102102
)]
103103
bar: AccountLoader<'info, Bar>,
104+
#[account(
105+
init,
106+
payer = authority, owner = ID,
107+
space = Bar::LEN + 8
108+
)]
109+
bar_3: AccountLoader<'info, Bar>,
110+
#[account(
111+
init,
112+
payer = authority, owner = ID,
113+
space = Bar::LEN + 8
114+
)]
115+
bar_4: AccountLoader<'info, Bar>,
116+
#[account(
117+
init,
118+
payer = authority, owner = ID,
119+
space = Bar::LEN + 8
120+
)]
121+
bar_5: AccountLoader<'info, Bar>,
122+
#[account(
123+
init,
124+
payer = authority, owner = ID,
125+
space = Bar::LEN + 8
126+
)]
127+
bar_6: AccountLoader<'info, Bar>,
128+
#[account(
129+
init,
130+
payer = authority, owner = ID,
131+
space = Bar::LEN + 8
132+
)]
133+
bar_7: AccountLoader<'info, Bar>,
134+
#[account(
135+
init,
136+
payer = authority, owner = ID,
137+
space = Bar::LEN + 8
138+
)]
139+
bar_8: AccountLoader<'info, Bar>,
140+
#[account(
141+
init,
142+
payer = authority, owner = ID,
143+
space = Bar::LEN + 8
144+
)]
145+
bar_9: AccountLoader<'info, Bar>,
146+
#[account(
147+
init,
148+
payer = authority, owner = ID,
149+
space = Bar::LEN + 8
150+
)]
151+
bar_10: AccountLoader<'info, Bar>,
152+
#[account(
153+
init,
154+
payer = authority, owner = ID,
155+
space = Bar::LEN + 8
156+
)]
157+
bar_11: AccountLoader<'info, Bar>,
158+
#[account(
159+
init,
160+
payer = authority, owner = ID,
161+
space = Bar::LEN + 8
162+
)]
163+
bar_12: AccountLoader<'info, Bar>,
164+
#[account(
165+
init,
166+
payer = authority, owner = ID,
167+
space = Bar::LEN + 8
168+
)]
169+
bar_13: AccountLoader<'info, Bar>,
170+
#[account(
171+
init,
172+
payer = authority, owner = ID,
173+
space = Bar::LEN + 8
174+
)]
175+
bar_14: AccountLoader<'info, Bar>,
176+
#[account(
177+
init,
178+
payer = authority, owner = ID,
179+
space = Bar::LEN + 8
180+
)]
181+
bar_15: AccountLoader<'info, Bar>,
182+
#[account(
183+
init,
184+
payer = authority, owner = ID,
185+
space = Bar::LEN + 8
186+
)]
187+
bar_16: AccountLoader<'info, Bar>,
188+
#[account(
189+
init,
190+
payer = authority, owner = ID,
191+
space = Bar::LEN + 8
192+
)]
193+
bar_17: AccountLoader<'info, Bar>,
194+
#[account(
195+
init,
196+
payer = authority, owner = ID,
197+
space = Bar::LEN + 8
198+
)]
199+
bar_18: AccountLoader<'info, Bar>,
200+
#[account(
201+
init,
202+
payer = authority, owner = ID,
203+
space = Bar::LEN + 8
204+
)]
205+
bar_19: AccountLoader<'info, Bar>,
206+
#[account(
207+
init,
208+
payer = authority, owner = ID,
209+
space = Bar::LEN + 8
210+
)]
211+
bar_20: AccountLoader<'info, Bar>,
104212
#[account(mut)]
105213
authority: Signer<'info>,
106214
foo: AccountLoader<'info, Foo>,

0 commit comments

Comments
 (0)