From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56671) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZpIKK-0004se-0b for qemu-devel@nongnu.org; Thu, 22 Oct 2015 12:02:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZpIKG-0003NR-QJ for qemu-devel@nongnu.org; Thu, 22 Oct 2015 12:02:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55090) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZpIKG-0003Mw-J5 for qemu-devel@nongnu.org; Thu, 22 Oct 2015 12:02:48 -0400 References: <1445522453-14450-1-git-send-email-P@draigBrady.com> <5628F4BC.2040502@redhat.com> <5628F634.6040809@redhat.com> <5628FE20.80802@draigBrady.com> <56290152.7010408@redhat.com> From: Eric Blake Message-ID: <562908A6.3000307@redhat.com> Date: Thu, 22 Oct 2015 10:02:46 -0600 MIME-Version: 1.0 In-Reply-To: <56290152.7010408@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="o4kkmaEs7AMSsvVwacErhXmRAwWX7agMP" Subject: Re: [Qemu-devel] [PATCH] copy, dd: simplify and optimize NUL bytes detection List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini , =?UTF-8?Q?P=c3=a1draig_Brady?= , coreutils@gnu.org Cc: Rusty Russell , "qemu-devel@nongnu.org" This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --o4kkmaEs7AMSsvVwacErhXmRAwWX7agMP Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 10/22/2015 09:31 AM, Paolo Bonzini wrote: > Only if your machine cannot do unaligned loads. If it can, you can > align the length instead of the buffer. memcmp will take care of > aligning the buffer (with some luck it won't have to, e.g. if buf is > 0x12340002 and length =3D 4094). On x86 unaligned "unsigned long" load= s > are basically free as long as they don't cross a cache line. >=20 >> BTW Rusty has a benchmark framework for this as referenced from: >> http://rusty.ozlabs.org/?p=3D560 >=20 > I missed his benchmark framework so I wrote another one, here it is: > https://gist.githubusercontent.com/bonzini/9a95b0e02d1ceb60af9e/raw/7bc= 42ddccdb6c42fea3db58e0539d0443d0e6dc6/memeqzero.c I see a bug in there: static __attribute__((noinline)) bool memeqzero4_paolo(const void *data, size_t length) { const unsigned char *p =3D data; unsigned long word; while (__builtin_expect(length & (sizeof(word) - 1), 0)) { if (*p) return false; p++; length--; } while (__builtin_expect(length & (16 - sizeof(word)), 0)) { memcpy(&word, p, sizeof(word)); if (word) return false; p +=3D sizeof(word); length -=3D sizeof(word); } /* Now we know that's zero, memcmp with self. */ return length =3D=3D 0 || memcmp(data, p, length) =3D=3D 0; } If length is already aligned on entry, then you are calling memcmp(data, data, length) which is trivially 0 for all input, rather than checking for actual NUL bytes. You MUST check at least one byte manually before handing off to memcmp(), and having the distance between data and p be a multiple of a cache-line (well, blindly picking 16 as Rusty did is a close approximation) will probably let the libc memcmp() run a lot faster than if memcmp() has to deal with unaligned pointers (where it can optimize for one of the two reads to be aligned, but the other read is unaligned - even if the two reads are close enough to be hitting the same cache line, you are still suffering from some performance slowdowns)= =2E --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --o4kkmaEs7AMSsvVwacErhXmRAwWX7agMP Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJWKQimAAoJEKeha0olJ0NqgAAH+wdVLAOaJ/M6SSEg+rEU2A7d +MKfBdsAOSuvsvjj4ybqUIyh8byLsRyOPevVGPPDmmlhqg28cncFYouF0+twrTY7 rOAmrf89h8IMF4pdlec8xSIK62AQqP7s3Kgi2dhfwWrS+uw7UaJ7V2jsgk7gW0q6 rE3NJBNckrXMDoV5dpU9BgQ13uRmAdxOrPsqCH6QlraQdZZMmtpdvoJRWgwKHxfn 4xcf8VVAnDoMM42EQg7Xx56edOXUeDMIuYtl/N66zTFaHAx4rxdwX4NUDde1717p m5Q/EevNaTX9IiLjWByck5AYUxPCwjdeg3nftfeuzlImkKP8FRsf6tGnBJCPaHE= =cQGL -----END PGP SIGNATURE----- --o4kkmaEs7AMSsvVwacErhXmRAwWX7agMP--