Skip to content

Add non-dup/known-size functionality to the lazy parser for cu optimization#352

Open
figaro-sol wants to merge 24 commits intoanza-xyz:mainfrom
figaro-sol:small_oracle
Open

Add non-dup/known-size functionality to the lazy parser for cu optimization#352
figaro-sol wants to merge 24 commits intoanza-xyz:mainfrom
figaro-sol:small_oracle

Conversation

@figaro-sol
Copy link

By letting the user inform the lazy account parser about whether the next account is known not to be a duplicate and if the size is known, users can get much better code generation. llvm is rather bad at propagating 'if condition then unreachable_unchecked' backwards. For example, in the CU bench,

let authority = match context.next_account() {
      Ok(MaybeAccount::Account(acc)) => acc,
      Ok(MaybeAccount::Duplicated(_)) => unsafe { core::hint::unreachable_unchecked() },
      Err(_) => hard_exit("Bad authority account"),
};

if authority.data_len() != 0 {
    unsafe { core::hint::unreachable_unchecked() }
}

has at best marginal effect on CUs of the updater (40->39 CUs). The asm generated for account parsing + size/signer includes many dup checks and full offset calculations like

ja +0x21 <entrypoint+0x198>
ldxdw r3, [r1 + 0x58]
add64 r1, 0x8
add64 r2, r3
add64 r2, 0x285f
and64 r2, -0x8
ldxb r3, [r1 + 0x1]
jne r3, 0x0, -0x10 <entrypoint+0x48>

and

mov64 r2, r1
add64 r2, 0x10
ldxb r3, [r1 + 0x8]
jeq r3, 0xff, +0xc <entrypoint+0x90>
mov64 r1, 0x0
ldxb r3, [r1 + 0x1]
jeq r3, 0x0, +0x10 <entrypoint+0xc8>
mov64 r3, r2
add64 r3, 0x8
ldxb r4, [r2 + 0x0]
jeq r4, 0xff, +0x10 <entrypoint+0xe8>

The version using the new gates:

let Ok(authority) = (unsafe {
    context.next_account_guarded(&AssumeNeverDup::new(), &CheckLikeType::<()>::new())
}) else {
    hard_exit("Bad authority account");
};q

used only 26 CUs. The generated code fully constant-propagates the offset computation and removes dup checks giving

ldxdw r2, [r1 + 0x0]
jne r2, 0x2, +0x1c <entrypoint+0xf0>
ldxdw r2, [r1 + 0x58]
jne r2, 0x0, +0x27 <entrypoint+0x158>
ldxb r2, [r1 + 0x9]
jeq r2, 0x0, +0x1c <entrypoint+0x110>
ldxdw r2, [r1 + 0x28b8]
jne r2, 0x28, +0x27 <entrypoint+0x178>
---- authority check, etc

This change also adds assume-stye assertions about the alignment of the instruction data as part of the process. this helps libraries like bytemuck elide alignment checks.

Checked and unchecked variants of the dup/data gates are added as well. the checked variants still provide a significant performance improvement since the compiler can usually infer account offsets with the check in place.

I've included the cu benchmark code I used into the PR. not sure if it's desired or not, happy to remove.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant