linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alan Jenkins <alan-jenkins@tuffmail.co.uk>
To: linux-hotplug@vger.kernel.org
Subject: [PATCH] util: Optimize path_encode
Date: Tue, 21 Oct 2008 10:12:57 +0000	[thread overview]
Message-ID: <48FDAB29.9090401@tuffmail.co.uk> (raw)

strncpy() shows up in profiling.
Since we already know the length, use memcpy() instead.

Measured 2% _user_ cpu time reduction on EeePC coldplug

Signed-off-by: Alan Jenkins <alan-jenkins@tuffmail.co.uk>

diff --git a/udev/lib/libudev-util.c b/udev/lib/libudev-util.c
index 0ff121d..6586d1c 100644
--- a/udev/lib/libudev-util.c
+++ b/udev/lib/libudev-util.c
@@ -114,7 +114,6 @@ size_t util_path_encode(char *s, size_t len)
 	char t[(len * 3)+1];
 	size_t i, j;
 
-	t[0] = '\0';
 	for (i = 0, j = 0; s[i] != '\0'; i++) {
 		if (s[i] = '/') {
 			memcpy(&t[j], "\\x2f", 4);
@@ -127,8 +126,11 @@ size_t util_path_encode(char *s, size_t len)
 			j++;
 		}
 	}
-	t[j] = '\0';
-	strncpy(s, t, len);
+	if (len = 0)
+		return j;
+	i = (j < len - 1) ? j : len - 1;
+	memcpy(s, t, i);
+	s[i] = '\0';
 	return j;
 }
 



             reply	other threads:[~2008-10-21 10:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-21 10:12 Alan Jenkins [this message]
2008-10-21 11:12 ` [PATCH] util: Optimize path_encode Kay Sievers
2008-10-21 21:41 ` Alan Jenkins

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=48FDAB29.9090401@tuffmail.co.uk \
    --to=alan-jenkins@tuffmail.co.uk \
    --cc=linux-hotplug@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).