Re: how many memset(,0,) calls in kernel ?

From: Willy Tarreau
Date: Mon Sep 13 2021 - 12:09:56 EST


On Mon, Sep 13, 2021 at 04:03:09PM +0000, David Laight wrote:
> > 36: b9 06 00 00 00 mov $0x6,%ecx
> > 3b: 4c 89 e7 mov %r12,%rdi
> > 3e: f3 ab rep stos %eax,%es:(%rdi)
> >
> > The last line does exactly "memset(%rdi, %eax, %ecx)". Just two bytes
> > for some code that modern processors are even able to optimize.
>
> Hmmm I'd bet that 6 stores will be faster on ~everything.
> 'modern' processors do better than some older ones [1], but 6
> writes isn't enough to get into the really fast paths.
> So you'll still take a few cycles of setup.

The exact point is, here it's up to the compiler to decide thanks to
its builtin what it considers best for the target CPU. It already
knows the fixed size and the code is emitted accordingly. It may
very well be a call to the memset() function when the size is large
and a power of two because it knows alternate variants are available
for example.

The compiler might even decide to shrink that area if other bytes
are written just after the memset(), leaving only holes touched by
memset().

Willy