From patchwork Sun May 24 17:10:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ondrej Bilka X-Patchwork-Id: 6908 Received: (qmail 33032 invoked by alias); 24 May 2015 17:10:55 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 32801 invoked by uid 89); 24 May 2015 17:10:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.3 required=5.0 tests=AWL, BAYES_40, FREEMAIL_FROM, SPF_NEUTRAL autolearn=no version=3.3.2 X-HELO: popelka.ms.mff.cuni.cz Date: Sun, 24 May 2015 19:10:36 +0200 From: =?utf-8?B?T25kxZllaiBCw61sa2E=?= To: libc-alpha@sourceware.org Cc: Wilco Dijkstra Subject: Re: [PATCH 4/* v2] Optimize strchrnul more Message-ID: <20150524171036.GA20947@domone> References: <20150524150715.GA31589@domone> <20150524163214.GA28053@domone> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20150524163214.GA28053@domone> User-Agent: Mutt/1.5.20 (2009-06-14) On Sun, May 24, 2015 at 06:32:14PM +0200, Ondřej Bílka wrote: > Hi, > this is nontrivial optimization of string inlines. > First it decreases icache pressure as you don't need strchr. > Just realized that optimization there is silly way to find terminating zero. On x64 rawmemchr is around 50% slower than strlen so add rawmemchr special case that does just that. * string/bits/string2.h (strchrnul, rawmemchr): Add inline (strchr): Optimize. diff --git a/string/bits/string2.h b/string/bits/string2.h index 2fe67b3..8f1eb04 100644 --- a/string/bits/string2.h +++ b/string/bits/string2.h @@ -108,18 +108,39 @@ __STRING2_COPY_TYPE (8); #endif -/* Return pointer to C in S. */ -#ifndef _HAVE_STRING_ARCH_strchr +#ifndef _HAVE_STRING_ARCH_rawmemchr extern void *__rawmemchr (const void *__s, int __c); # if __GNUC_PREREQ (3, 2) -# define strchr(s, c) \ +# define __rawmemchr(s, c) \ + (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \ + && (c) == '\0' \ + ? s + strlen (s) \ + : __rawmemchr (s, c))) +# endif +#endif + + + +#ifndef _HAVE_STRING_ARCH_strchrnul +# if __GNUC_PREREQ (3, 2) +# define strchrnul(s, c) \ (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \ && (c) == '\0' \ ? (char *) __rawmemchr (s, c) \ - : __builtin_strchr (s, c))) + : strchrnul (s, c))) # endif #endif +/* Return pointer to C in S. */ +#ifndef _HAVE_STRING_ARCH_strchr +# if __GNUC_PREREQ (3, 2) +# define strchr(s, c) \ + (__extension__ ({ char *__r = strchrnul (s, c); \ + *__r == c ? __r : NULL; })) +# endif +#endif + + /* Copy SRC to DEST, returning pointer to final NUL byte. */ #ifdef __USE_GNU # if !defined _HAVE_STRING_ARCH_stpcpy || defined _FORCE_INLINES