Skip to content

Commit abad29c

Browse files
committed
♻️ Optimizing apply_impl with less Vecs
1 parent b9d9cc8 commit abad29c

File tree

1 file changed

+11
-24
lines changed
  • crates/programs/world/src

1 file changed

+11
-24
lines changed

crates/programs/world/src/lib.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub mod world {
287287
args,
288288
&ctx.accounts.system_program,
289289
None,
290-
ctx.remaining_accounts.to_vec(),
290+
ctx.remaining_accounts,
291291
)?;
292292
Ok(())
293293
}
@@ -338,7 +338,7 @@ pub mod world {
338338
args,
339339
&ctx.accounts.system_program,
340340
Some(&ctx.accounts.session_token),
341-
ctx.remaining_accounts.to_vec(),
341+
ctx.remaining_accounts,
342342
)?;
343343
Ok(())
344344
}
@@ -389,7 +389,7 @@ fn apply_impl<'info>(
389389
args: Vec<u8>,
390390
system_program: &Program<'info, System>,
391391
session_token: Option<&Account<'info, session_keys::SessionToken>>,
392-
mut remaining_accounts: Vec<AccountInfo<'info>>,
392+
remaining_accounts: &[AccountInfo<'info>],
393393
) -> Result<()> {
394394
if !authority.is_signer && authority.key != &ID {
395395
return Err(WorldError::InvalidAuthority.into());
@@ -403,18 +403,10 @@ fn apply_impl<'info>(
403403
return Err(WorldError::SystemNotApproved.into());
404404
}
405405

406-
let mut pairs = Vec::with_capacity(remaining_accounts.len() / 2);
407-
while remaining_accounts.len() >= 2 {
408-
let program = remaining_accounts.remove(0);
409-
if program.key() == ID {
410-
break;
411-
}
412-
let component = remaining_accounts.remove(0);
413-
pairs.push((program, component));
414-
}
406+
let index = remaining_accounts.iter().position(|x| x.key() == ID).unwrap_or(remaining_accounts.len());
415407

416408
// Authority check against component metadata (partial deserialize)
417-
for (_, component) in &pairs {
409+
for component in remaining_accounts[..index].iter().skip(1).step_by(2) {
418410
let data_ref = component.try_borrow_data()?;
419411
// Expect at least Anchor discriminator (8) + BoltMetadata (32)
420412
if data_ref.len() < 8 + 32 {
@@ -459,14 +451,6 @@ fn apply_impl<'info>(
459451
}
460452
}
461453

462-
let mut components_accounts = pairs
463-
.iter()
464-
.map(|(_, component)| component)
465-
.cloned()
466-
.collect::<Vec<_>>();
467-
components_accounts.append(&mut remaining_accounts);
468-
let remaining_accounts = components_accounts;
469-
470454
// Create the buffer account only if it does not already exist.
471455
// Subsequent applies reuse the same PDA and only reallocate its data.
472456
if buffer.lamports() == 0 {
@@ -487,7 +471,8 @@ fn apply_impl<'info>(
487471
)?;
488472
}
489473

490-
for (program, component) in &pairs {
474+
for pair in remaining_accounts[..index].chunks(2) {
475+
let [program, component] = pair else { continue };
491476
buffer.realloc(component.data_len(), false)?;
492477
{
493478
let mut data = buffer.try_borrow_mut_data()?;
@@ -519,12 +504,14 @@ fn apply_impl<'info>(
519504
}
520505
}
521506

507+
let cpi_remaining_accounts = remaining_accounts[..index].iter().skip(1).step_by(2).chain(remaining_accounts[index..].iter().skip(1)).cloned().collect::<Vec<_>>();
522508
bolt_system::cpi::bolt_execute(
523-
cpi_context.with_remaining_accounts(remaining_accounts),
509+
cpi_context.with_remaining_accounts(cpi_remaining_accounts),
524510
args,
525511
)?;
526512

527-
for (program, component) in &pairs {
513+
for pair in remaining_accounts[..index].chunks(2) {
514+
let [program, component] = pair else { continue };
528515
buffer.realloc(component.data_len(), false)?;
529516
{
530517
let mut data = buffer.try_borrow_mut_data()?;

0 commit comments

Comments
 (0)