Re: [PATCH v2] statx: optimize copy of struct statx to userspace

From: Florian Weimer
Date: Mon Mar 13 2017 - 06:27:47 EST


On 03/13/2017 05:34 AM, Andreas Dilger wrote:
Not that it is a huge deal either way, but I'd think it is harder for the
compiler to optimize across a function call boundary like memset() vs. a
struct initialization in the same function where it can see that all but
a few of the fields are being overwritten immediately before they are used.

GCC treats memset as a function call only if options such as -ffreestanding or -fno-builtin are enabled, or if memset is redefined in a header file. Does the kernel do this?

I don't think the designated initializer is any less clear to the reader
that the struct is zeroed out compared to using memset(). Possibly the
best compromise is to use a designated initializer that specifies all of
the known fields, and leaves it to the compiler to initialize unset fields
or padding.

GCC will not always initialize padding if you specify a designated initializer because padding values are unspeficied and their value does not matter from a language point of view.

Thanks,
Florian