From patchwork Mon Jan 12 15:09:22 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 4622 Received: (qmail 20511 invoked by alias); 12 Jan 2015 15:09:44 -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 20498 invoked by uid 89); 12 Jan 2015 15:09:42 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: service87.mimecast.com From: "Wilco Dijkstra" To: Subject: [PATCH] Improve stpncpy performance Date: Mon, 12 Jan 2015 15:09:22 -0000 Message-ID: <001b01d02e79$bf944470$3ebccd50$@com> MIME-Version: 1.0 X-MC-Unique: 115011215093802501 Like strncpy, this patch improves stpncpy performance by using strnlen/memcpy/memset rather than a byte loop. Performance on bench-stpncpy is ~2x faster on average. ChangeLog: 2015-01-12 Wilco Dijkstra wdijkstr@arm.com * string/stpncpy.c (stpncpy): Improve performance using __strnlen/memcpy/memset. --- string/stpncpy.c | 59 ++++++-------------------------------------------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/string/stpncpy.c b/string/stpncpy.c index fad747e..50521aa 100644 --- a/string/stpncpy.c +++ b/string/stpncpy.c @@ -15,7 +15,7 @@ License along with the GNU C Library; if not, see . */ -/* This is almost copied from strncpy.c, written by Torbjorn Granlund. */ +#include #ifdef HAVE_CONFIG_H # include @@ -41,59 +41,12 @@ weak_alias (__stpncpy, stpncpy) char * STPNCPY (char *dest, const char *src, size_t n) { - char c; - char *s = dest; - - if (n >= 4) - { - size_t n4 = n >> 2; - - for (;;) - { - c = *src++; - *dest++ = c; - if (c == '\0') - break; - c = *src++; - *dest++ = c; - if (c == '\0') - break; - c = *src++; - *dest++ = c; - if (c == '\0') - break; - c = *src++; - *dest++ = c; - if (c == '\0') - break; - if (--n4 == 0) - goto last_chars; - } - n -= dest - s; - goto zero_fill; - } - - last_chars: - n &= 3; - if (n == 0) + size_t size = __strnlen (src, n); + memcpy (dest, src, size); + dest += size; + if (size == n) return dest; - - for (;;) - { - c = *src++; - --n; - *dest++ = c; - if (c == '\0') - break; - if (n == 0) - return dest; - } - - zero_fill: - while (n-- > 0) - dest[n] = '\0'; - - return dest - 1; + return memset (dest, '\0', n - size); } #ifdef weak_alias libc_hidden_def (__stpncpy)