Re: [PATCH] riscv: lib: optimize strlen loop efficiency

From: Paul Walmsley

Date: Wed Jan 14 2026 - 21:03:20 EST


On Thu, 18 Dec 2025, Feng Jiang wrote:

> Optimize the generic strlen implementation by using a pre-decrement
> pointer. This reduces the loop body from 4 instructions to 3 and
> eliminates the unconditional jump ('j').
>
> Old loop (4 instructions, 2 branches):
> 1: lbu t0, 0(t1); beqz t0, 2f; addi t1, t1, 1; j 1b
>
> New loop (3 instructions, 1 branch):
> 1: addi t1, t1, 1; lbu t0, 0(t1); bnez t0, 1b
>
> This change improves execution efficiency and reduces branch pressure
> for systems without the Zbb extension.

Looks reasonable; do you have any benchmarks on hardware that you can
share? Any reason why this patch stands alone and isn't rolled up as part
of your "optimize string function" series?


- Paul