[Libguestfs] [PATCH] daemon: Move the useful 'is_zero' function into common code.

Richard W.M. Jones rjones at redhat.com
Wed Apr 19 15:50:38 UTC 2017


On Wed, Apr 19, 2017 at 03:40:49PM +0100, Daniel P. Berrange wrote:
> On Wed, Apr 19, 2017 at 09:35:16AM -0500, Eric Blake wrote:
> > On 04/19/2017 09:17 AM, Richard W.M. Jones wrote:
> > > This is largely a simple refactoring, but it combines another
> > > definition of this function from virt-builder which had a slightly
> > > different prototype.
> > > ---
> > 
> > > +/* Return true iff the buffer is all zero bytes.
> > > + *
> > > + * Note that gcc is smart enough to optimize this properly:
> > > + * http://stackoverflow.com/questions/1493936/faster-means-of-checking-for-an-empty-buffer-in-c/1493989#1493989
> > > + */
> > > +static inline int
> > > +is_zero (const char *buffer, size_t size)
> > > +{
> > > +  size_t i;
> > > +
> > > +  for (i = 0; i < size; ++i) {
> > > +    if (buffer[i] != 0)
> > > +      return 0;
> > > +  }
> > > +
> > > +  return 1;
> > > +}
> > 
> > This is still byte-at-a-time.

RPM uses -mtune=generic, and GCC doesn't optimize the loop in any way.
Literally just iterating over bytes in the most naive way possible.

With -mtune=broadwell it unrolls the loop (16 times if I call it on a
fixed size buffer).

> > Often, you can get even faster results by
> > exploiting libc's optimizations in memcmp (particularly when it is
> > comparing pointers with nice alignments), by manually checking that the
> > first 16 bytes are zero, then letting memcmp do the rest:
> > 
> > size_t i;
> > size_t limit = MIN(size, 16);
> > for (i = 0; i < limit; ++i)
> >   if (buffer[i])
> >     return 0;
> > if (size != limit)
> >   return !memcmp(buffer, buffer + 16, size - 16);
> > return 1;

This is a neat idea!

> If performance is critical, QEMU has a permissively licensed impl that
> uses sse/avx instructions to super-optimize it on x86 (util/bufferiszero.c).
> It is a large amount of code though, so only worth it if this is a notable
> bottleneck for libguestfs.

This would be a lot easier if C defined this as a standard function ...

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/




More information about the Libguestfs mailing list