From patchwork Wed Aug 20 12:44:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 2452 Received: (qmail 5005 invoked by alias); 20 Aug 2014 12:44:26 -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 4914 invoked by uid 89); 20 Aug 2014 12:44:26 -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: Subject: [PATCH] Improve performance of strncpy Date: Wed, 20 Aug 2014 13:44:10 +0100 Message-ID: <000401cfbc74$758a9b80$609fd280$@com> MIME-Version: 1.0 X-MC-Unique: 114082013442300601 Hi, This patch improves strncpy performance by using memset to clear memory after the string when the buffer is much larger than the copied string. This is better as memset is significantly faster than a simple byte-loop. On bench-strncpy it is ~25% faster. ChangeLog: 2014-08-20 Wilco Dijkstra * string/strncpy.c (strncpy): Improve performance by using memset. --- string/strncpy.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/string/strncpy.c b/string/strncpy.c index 0915e03..604417c 100644 --- a/string/strncpy.c +++ b/string/strncpy.c @@ -78,9 +78,12 @@ STRNCPY (char *s1, const char *s2, size_t n) while (c != '\0'); zero_fill: - do - *++s1 = '\0'; - while (--n > 0); + if (n >= 8) + memset (s1 + 1, '\0', n); + else + do + *++s1 = '\0'; + while (--n > 0); return s; }