public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16 18:50 Orestes Leal Rodriguez
  2015-06-16 22:16 ` Joe Perches
  0 siblings, 1 reply; 6+ messages in thread
From: Orestes Leal Rodriguez @ 2015-06-16 18:50 UTC (permalink / raw)
  To: linux-kernel; +Cc: mihai.dontu

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



^ permalink raw reply related	[flat|nested] 6+ messages in thread
* [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16 18:51 Orestes Leal Rodriguez
  0 siblings, 0 replies; 6+ messages in thread
From: Orestes Leal Rodriguez @ 2015-06-16 18:51 UTC (permalink / raw)
  To: linux-kernel

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



^ permalink raw reply related	[flat|nested] 6+ messages in thread
* [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions
@ 2015-06-16 22:51 Orestes Leal Rodriguez
  2015-06-17 20:07 ` Richard Weinberger
  0 siblings, 1 reply; 6+ messages in thread
From: Orestes Leal Rodriguez @ 2015-06-16 22:51 UTC (permalink / raw)
  To: bp; +Cc: richard.weinberger, linux-kernel

 > Use the force^Wcheckpatch.pl.
This is the output of checkpatch.pl:
output of checkpatch: total: 0 errors, 0 warnings, 42 lines checked
/root/string.c.patch has no obvious style problems and is ready for 
submission

 > You need to explain that in the commit message, my young padawan.
Very small update to strlen and strnlen that now use less cpu 
instructions by using a counter to avoid memory address
arithmetic, which cause that the compiler adds more machine
instructions for computing the length of the string just before
returning from the functions, the old machine code is like the
following:

  mov    -0x4(%ebp),%edx
  mov    0x8(%ebp),%eax
  sub    %eax,%edx
  mov    %edx,%eax
  leave
  ret


now in the new versions the value is not calculated anymore,
instead  he value of the counter is put on eax after the
condition inside the loop no longer holds, and then return:

  mov    -0x4(%ebp),%eax
  leave
  ret

With this a few cpu instructions are saved.


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

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



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-06-19  3:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-16 18:50 [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions Orestes Leal Rodriguez
2015-06-16 22:16 ` Joe Perches
  -- strict thread matches above, loose matches on Subject: below --
2015-06-16 18:51 Orestes Leal Rodriguez
2015-06-16 22:51 Orestes Leal Rodriguez
2015-06-17 20:07 ` Richard Weinberger
2015-06-19  3:11   ` Orestes Leal Rodriguez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox