From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15786 invoked by alias); 10 Sep 2014 15:21:55 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 15771 invoked by uid 89); 10 Sep 2014 15:21:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,SPF_PASS autolearn=ham version=3.3.2 X-HELO: service87.mimecast.com From: "Wilco Dijkstra" To: Cc: References: In-Reply-To: Subject: RE: [PATCH] Improve performance of strncpy Date: Wed, 10 Sep 2014 15:21:00 -0000 Message-ID: <001301cfcd0a$f0b62670$d2227350$@com> MIME-Version: 1.0 X-MC-Unique: 114091016214821501 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable X-SW-Source: 2014-09/txt/msg00178.txt.bz2 Adhemerval Zanella wrote: > Hi, the patch looks ok. I also pushed a similar modification for powerpc = based on same idea.=20 > zero_fill: > - do > - *++s1 =3D '\0'; > - while (--n > 0); > + if (n >=3D 8) > + memset (s1 + 1, '\0', n); > + else > + do > + *++s1 =3D '\0'; > + while (--n > 0); > I wonder if this test is really worth, my opinion is just to keep it simp= le > and just call memset on both 'goto' in loop and after 'last_chars'. Yes, you're right, I timed it and there is actually little difference, while the code is now even simpler. New version below (not attaching results in b= ad characters due to various mail servers changing line endings). OK for commit? --- string/strncpy.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/string/strncpy.c b/string/strncpy.c index 0915e03..d5fa5be 100644 --- a/string/strncpy.c +++ b/string/strncpy.c @@ -57,10 +57,10 @@ STRNCPY (char *s1, const char *s2, size_t n) if (--n4 =3D=3D 0) goto last_chars; } - n =3D n - (s1 - s) - 1; - if (n =3D=3D 0) - return s; - goto zero_fill; + s1++; + n =3D n - (s1 - s); + memset (s1, '\0', n); + return s; } =20 last_chars: @@ -77,11 +77,7 @@ STRNCPY (char *s1, const char *s2, size_t n) } while (c !=3D '\0'); =20 - zero_fill: - do - *++s1 =3D '\0'; - while (--n > 0); - + memset (s1 + 1, '\0', n); return s; } libc_hidden_builtin_def (strncpy) --=20 1.7.9.5