Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1166296

[PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions

Path csiph.com!aioe.org!bofh.it!news.nic.it!robomod
From Orestes Leal Rodriguez <lukes357@gmail.com>
Newsgroups linux.kernel
Subject [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions
Date Tue, 16 Jun 2015 20:00:02 +0200
Message-ID <pC3js-1f2-21@gated-at.bofh.it> (permalink)
X-Original-To linux-kernel@vger.kernel.org
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :content-type:content-transfer-encoding; bh=Q7CI9fHs1lVu/ApSPRVwxgAoSkNW7gx80rBMJwODXNw=; b=CXbuSAdl0mLtdjK0Lpha17GQK+YEH6pnb285qQz6vV/vkvXVy1QYF6bi19aKYeX+/s B9oIE23PjuMa13gAhkWnRZgAGgLo/ph7L71vSiQfpIMOhehxLKRiTvX0uHzvP1W+Zvl9 0XB7A+whHyaVh0TP73CB8niGdcOsXeMihU2bzK+a1K5RiS4uTKSmg8tzfBvfuojZ4ghi gg0JzMJqvgAhnOdF8Dt245LI3y1ufbwF7mvEo+/3lbQe3HNR/NUsKBJEAWUP9mtS/YlB 9wVpLfSSgyDRKiPSOoYr1mppbpB8t5H2cxDHRrRaeBrFLFM3EGH/Gcsc7uJPT0YNPNA/ MUpA==
X-Received by 10.52.129.100 with SMTP id nv4mr1209599vdb.43.1434477514365; Tue, 16 Jun 2015 10:58:34 -0700 (PDT)
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
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 63
Organization linux.* mail to news gateway
X-Original-Cc mihai.dontu@gmail.com
X-Original-Date Tue, 16 Jun 2015 13:50:16 -0500
X-Original-Message-ID <55806FE8.9030302@gmail.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref aioe.org linux.kernel:1166296

Show key headers only | View raw


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.

Signed-off-by: Orestes Leal Rodriguez <lukes357@gmail.com>
---

diff --git a/lib/string.c" "b/lib/string.c
index 992bf30..c873436 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -17,6 +17,10 @@
   * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
   *                    Matthew Hawkins <matt@mh.dropbear.id.au>
   * -  Kissed strtok() goodbye
+ *
+ * * Tuesday June 16 2015, Orestes Leal Rodriguez <lukes357@gmail.com>
+ * - strlen, strnlen: by using a single counter we use less cpu 
instructions
+ *   by avoiding substracting the memory addresses before return
   */

  #include <linux/types.h>
@@ -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


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

[PATCH 1/1] lib: small update for strlen, strnlen, use less  cpu instructions Orestes Leal Rodriguez <lukes357@gmail.com> - 2015-06-16 20:00 +0200
  Re: [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu  instructions Joe Perches <joe@perches.com> - 2015-06-17 00:20 +0200

csiph-web