Skip to content

use a more optimal loop implementation#131

Open
nagisa wants to merge 1 commit intosolana-program:mainfrom
nagisa:perffix
Open

use a more optimal loop implementation#131
nagisa wants to merge 1 commit intosolana-program:mainfrom
nagisa:perffix

Conversation

@nagisa
Copy link

@nagisa nagisa commented Feb 26, 2026

In platform-tools v1.53 inlining decisions of Iterator::next prevent LLVM from optimizing this loop into an unrolled index-based version of it.

So where in 1.51 we'd see

INLINE_MEMMOVE($from, $to, 32 bytes)
jeq $length, 1, exit
INLINE_MEMMOVE($from, $to, 32 bytes)
jeq $length, 2, exit
...

In 1.53 we'd instead get a much more CU-heavy pointer-comparison based loop instead.

lsh64 $index, 3
ldxdw r4, [r1 + 0]
add64 r1, $index
INLINE_MEMMOVE($from, $to, 32)
ldxdw r6, [r10 - 48]
jeq r1, r2, exit
; ...

Since we want an index-based loop here for optimal results, might as well write it that way directly instead of relying on optimizations to trigger.

In platform-tools v1.53 inlining decisions of Iterator::next prevent
LLVM from optimizing this loop into an unrolled index-based version of
it.

So where in 1.51 we'd see

```asm
INLINE_MEMMOVE($from, $to, 32 bytes)
jeq $length, 1, exit
INLINE_MEMMOVE($from, $to, 32 bytes)
jeq $length, 2, exit
...
```

In 1.53 we'd instead get a much more CU-heavy pointer-comparison based
loop instead.

```asm
lsh64 $index, 3
ldxdw r4, [r1 + 0]
add64 r1, $index
INLINE_MEMMOVE($from, $to, 32)
ldxdw r6, [r10 - 48]
jeq r1, r2, exit
; ...
```

Since we want an index-based loop here for optimal results, might as
well write it that way directly instead of relying on optimizations to
trigger.

Co-authored-by: Lucas Ste <38472950+LucasSte@users.noreply.github.com>
@nagisa
Copy link
Author

nagisa commented Feb 26, 2026

cc @LucasSte

Please test @febo

@LucasSte LucasSte requested a review from febo February 26, 2026 18:37
@LucasSte
Copy link

cc @LucasSte

Please test @febo

On v1.51 I see 192 CUs (down from 193 comparing to master) and on v1.54 I see 186. Interesting, that there appears to be a regression in Rust, no?

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.

2 participants