Re: [PATCH] copy,dd: simplify and optimize NUL bytes detection
From:
Eric Blake
Subject:
Re: [PATCH] copy,dd: simplify and optimize NUL bytes detection
Date:
Thu, 22 Oct 2015 08:37:48 -0600
User-agent:
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0
[adding qemu]
On 10/22/2015 08:00 AM, Pádraig Brady wrote:
> * src/system.h (is_nul): Reimplement with a version
> that doesn't require a sentinel after the buffer,
> and which calls down to (the system optimized) memcmp.
> Performance analyzed at http://rusty.ozlabs.org/?p=560
> /* Return whether the buffer consists entirely of NULs.
> - Note the word after the buffer must be non NUL. */
> + From CCAN by Rusty Russell <address@hidden>
> + released under CC0 (Public domain). */
>
> static inline bool _GL_ATTRIBUTE_PURE
> is_nul (void const *buf, size_t bufsize)
> {
> + const unsigned char *p = buf;
> + size_t len;
> +
> + /* Check first 16 bytes manually. */
> + for (len = 0; len < 16; len++)
> + {
> + if (! bufsize)
> + return true;
> + if (*p)
> + return false;
> + p++;
> + bufsize--;
> + }
> +
> + /* Now we know that's zero, memcmp with self. */
> + return memcmp (buf, p, bufsize) == 0;
> }
Cool trick of using a suitably-aligned overlap-to-self check to then
trigger platform-specific speedups without having to rewrite them by
hand! qemu is doing a similar check in util/cutils.c:buffer_is_zero()
that could probably benefit from the same idea.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc Description: OpenPGP digital signature