public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Orestes Leal Rodriguez <lukes357@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: [PATCH] small update for strlen, strnlen, use less cpu instructions
Date: Tue, 16 Jun 2015 04:40:55 -0500	[thread overview]
Message-ID: <557FEF27.80404@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 201 bytes --]

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.




[-- Attachment #2: string.c.patch --]
[-- Type: text/plain, Size: 1142 bytes --]

--- 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 <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
 



             reply	other threads:[~2015-06-16  8:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-16  9:40 Orestes Leal Rodriguez [this message]
2015-06-16 10:03 ` [PATCH] small update for strlen, strnlen, use less cpu instructions Mihai Donțu
  -- strict thread matches above, loose matches on Subject: below --
2015-06-16 11:41 Alexey Dobriyan
2015-06-16 11:43 ` Richard Weinberger
2015-06-16  9:36 Orestes Leal Rodriguez
2015-06-16  9:28 Orestes Leal Rodriguez
2015-06-16 10:37 ` Richard Weinberger
2015-06-16 18:56   ` Luke Skywalker
2015-06-16 19:06     ` Darth Vader
2015-06-16  9:26 Luke Skywalker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=557FEF27.80404@gmail.com \
    --to=lukes357@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox