From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933801AbbFPJUy (ORCPT ); Tue, 16 Jun 2015 05:20:54 -0400 Received: from mail-yh0-f44.google.com ([209.85.213.44]:34257 "EHLO mail-yh0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756737AbbFPIof (ORCPT ); Tue, 16 Jun 2015 04:44:35 -0400 Message-ID: <557FEE38.9070901@gmail.com> Date: Tue, 16 Jun 2015 04:36:56 -0500 From: Orestes Leal Rodriguez User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0 SeaMonkey/2.30 MIME-Version: 1.0 To: linux-kernel@vger.kernel.org Subject: [PATCH] small update for strlen, strnlen, use less cpu instructions Content-Type: multipart/mixed; boundary="------------000602040601010307070202" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a multi-part message in MIME format. --------------000602040601010307070202 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit very small update to strlen and strnlen that now use less cpu instructions by using a counter to avoid the memory addresses substraction to find the length of the string. Orestes Leal Rodriguez. --------------000602040601010307070202 Content-Type: text/plain; charset=windows-1252; name="string.c.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="string.c.patch" --- lib/string.c.orig 2015-06-15 23:59:32.768346193 -0400 +++ lib/string.c 2015-06-16 00:11:45.791381539 -0400 @@ -17,6 +17,10 @@ * * Sat Feb 09 2002, Jason Thomas , * Matthew Hawkins * - Kissed strtok() goodbye + * + * * Tuesday June 16 2015, Orestes Leal Rodriguez + * - strlen, strnlen: by using a single counter we use less cpu instructions + * by avoiding substracting the memory addresses before return */ #include @@ -401,11 +405,11 @@ EXPORT_SYMBOL(strim); */ size_t strlen(const char *s) { - const char *sc; + size_t sz = 0; - for (sc = s; *sc != '\0'; ++sc) - /* nothing */; - return sc - s; + for (; *s++ != '\0'; sz++) + /*empty */; + return sz; } EXPORT_SYMBOL(strlen); #endif @@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen); */ size_t strnlen(const char *s, size_t count) { - const char *sc; + size_t sz = 0; - for (sc = s; count-- && *sc != '\0'; ++sc) - /* nothing */; - return sc - s; + for (; count-- && *s++ != '\0'; sz++) + /* empty */; + return sz; } + EXPORT_SYMBOL(strnlen); #endif --------------000602040601010307070202--