RE: First kernel patch (optimization)
From: David Laight
Date: Thu Sep 17 2015 - 04:46:46 EST
From: Jaime Arrocha
> Sent: 17 September 2015 02:50
..
> One interesting observation I found was that in O0 and O2, it does make
> a call to strlen while in O1 it calculates
> the length of the string using:
>
You want an 'xor %rcx,%rcx' here.
> repnz scas %es:(%rdi),%al
> not %rcx
> sub $0x2,%rcx
>
> Why does it do that? Is the code above faster? If yes, why not do it in
> O2 too?
Because 'repnz scasb' is slow, especially on some cpu types.
It may win for -Os on 32 bit systems.
Pentium 4 netburst have about 40 clocks setup for all the 'rep' instructions,
later cpus are better but you might still be talking double figures.
On 64 bit cpu there are much faster ways of detecting a zero byte in a
64 bit word by using shifts and masks - so the function call can be a win.
David