]> err.no Git - systemd/commitdiff
libudev: util - optimize path_encode()
authorAlan Jenkins <alan-jenkins@tuffmail.co.uk>
Tue, 21 Oct 2008 10:12:57 +0000 (11:12 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Tue, 21 Oct 2008 11:09:31 +0000 (13:09 +0200)
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>
udev/lib/libudev-util.c

index 0ff121d774e12701c1f4fb822689222643bae4e7..6586d1c825e14c1b8c296f68edcbe4c16faddeb7 100644 (file)
@@ -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;
 }