linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] smaller strlen()
Date: Fri, 26 Aug 2016 23:01:20 +0300	[thread overview]
Message-ID: <20160826200120.GA1852@p183.telecom.by> (raw)

gcc prefers "*s++" style code for some reason, doesn't unroll loop
condition check once. Kernel strings are small but they aren't of 0
length, so that additional branch was almost never taken.

	$ ./scripts/bloat-o-meter ../vmlinux-000 ../obj/vmlinux
	strlen         30      26      -4
	strlcpy        71      64      -7
	strlcat       120      99     -21

strlcpy() and strlcat() are collateral damage :^)

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 lib/string.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

--- a/lib/string.c
+++ b/lib/string.c
@@ -476,11 +476,11 @@ EXPORT_SYMBOL(strim);
  */
 size_t strlen(const char *s)
 {
-	const char *sc;
+	const char *s0 = s;
 
-	for (sc = s; *sc != '\0'; ++sc)
+	while (*s++)
 		/* nothing */;
-	return sc - s;
+	return s - s0 - 1;
 }
 EXPORT_SYMBOL(strlen);
 #endif

             reply	other threads:[~2016-08-26 20:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-26 20:01 Alexey Dobriyan [this message]
2016-08-26 22:23 ` [PATCH] smaller strlen() Joe Perches

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=20160826200120.GA1852@p183.telecom.by \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --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;
as well as URLs for NNTP newsgroup(s).