From patchwork Wed Sep 10 15:21:48 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 2755 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-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 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 16:21:48 +0100 Message-ID: <001301cfcd0a$f0b62670$d2227350$@com> MIME-Version: 1.0 X-MC-Unique: 114091016214821501 Adhemerval Zanella wrote: > Hi, the patch looks ok. I also pushed a similar modification for powerpc based on same idea. > zero_fill: > - do > - *++s1 = '\0'; > - while (--n > 0); > + if (n >= 8) > + memset (s1 + 1, '\0', n); > + else > + do > + *++s1 = '\0'; > + while (--n > 0); > I wonder if this test is really worth, my opinion is just to keep it simple > 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 bad 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 == 0) goto last_chars; } - n = n - (s1 - s) - 1; - if (n == 0) - return s; - goto zero_fill; + s1++; + n = n - (s1 - s); + memset (s1, '\0', n); + return s; } last_chars: @@ -77,11 +77,7 @@ STRNCPY (char *s1, const char *s2, size_t n) } while (c != '\0'); - zero_fill: - do - *++s1 = '\0'; - while (--n > 0); - + memset (s1 + 1, '\0', n); return s; } libc_hidden_builtin_def (strncpy)