linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] util: Optimize path_encode
@ 2008-10-21 10:12 Alan Jenkins
  2008-10-21 11:12 ` Kay Sievers
  2008-10-21 21:41 ` Alan Jenkins
  0 siblings, 2 replies; 3+ messages in thread
From: Alan Jenkins @ 2008-10-21 10:12 UTC (permalink / raw)
  To: linux-hotplug

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



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

* Re: [PATCH] util: Optimize path_encode
  2008-10-21 10:12 [PATCH] util: Optimize path_encode Alan Jenkins
@ 2008-10-21 11:12 ` Kay Sievers
  2008-10-21 21:41 ` Alan Jenkins
  1 sibling, 0 replies; 3+ messages in thread
From: Kay Sievers @ 2008-10-21 11:12 UTC (permalink / raw)
  To: linux-hotplug

On Tue, Oct 21, 2008 at 12:12, Alan Jenkins <alan-jenkins@tuffmail.co.uk> wrote:
> strncpy() shows up in profiling.
> Since we already know the length, use memcpy() instead.
>
> Measured 2% _user_ cpu time reduction on EeePC coldplug

Applied.

Thanks,
Kay

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

* Re: [PATCH] util: Optimize path_encode
  2008-10-21 10:12 [PATCH] util: Optimize path_encode Alan Jenkins
  2008-10-21 11:12 ` Kay Sievers
@ 2008-10-21 21:41 ` Alan Jenkins
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Jenkins @ 2008-10-21 21:41 UTC (permalink / raw)
  To: linux-hotplug

Kay Sievers wrote:
> On Tue, Oct 21, 2008 at 12:12, Alan Jenkins <alan-jenkins@tuffmail.co.uk> wrote:
>   
>> strncpy() shows up in profiling.
>> Since we already know the length, use memcpy() instead.
>>
>> Measured 2% _user_ cpu time reduction on EeePC coldplug
>>     
>
> Applied.
>
> Thanks,
> Kay
>   

Ah... just realised it wasn't the normal C string length inefficiency. 
The problem was strncpy() doesn't stop after writing the terminating
NUL; by definition it goes on to zero the entire buffer.

I spy another use of strncpy in udev_device_add_property_from_string(),
which is responsible for another ~1% user cpu time...

BTW I'm still polishing threaded udevd in my spare time.  It has 4
TODOs; I will post it soon anyway, but I need to write a few explanatory
words first.  I have coarse-grained patch descriptions, but there are a
couple of obvious questions I would like to pre-answer :).

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

end of thread, other threads:[~2008-10-21 21:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-21 10:12 [PATCH] util: Optimize path_encode Alan Jenkins
2008-10-21 11:12 ` Kay Sievers
2008-10-21 21:41 ` Alan Jenkins

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