lkml.org 
[lkml]   [2006]   [Dec]   [10]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: optimalisation for strlcpy (lib/string.c)
Folkert van Heusden wrote:
> Hi,
>
> Like the other patch (by that other person), I think it is faster to not
> do a strlen first.
> E.g. replace this:
> ize_t strlcpy(char *dest, const char *src, size_t size)
> {
> size_t ret = strlen(src);
>
> if (size) {
> size_t len = (ret >= size) ? size - 1 : ret;
> memcpy(dest, src, len);
> dest[len] = '\0';
> }
> return ret;
> }
> by this:
> size_t strlcpy(char *dest, const char *src, size_t size)
> {
> char *tmp = dest;
>
> for(;;)
> {
> *dest = *src;
> if (!*src)
> break;
>
> if (--size == 0)
> break;
>
> dest++;
> src++;
> }
>
> *dest = 0x00;
>
> return dest - tmp;
> }
> patch:
> diff -uNrBbd lib/string.c string-new.c
> --- lib/string.c 2006-11-04 02:33:58.000000000 +0100
> +++ string-new.c 2006-12-10 22:22:08.000000000 +0100
> @@ -121,14 +121,24 @@
> */
> size_t strlcpy(char *dest, const char *src, size_t size)
> {
> - size_t ret = strlen(src);
> + char *tmp = dest;
>
> - if (size) {
> - size_t len = (ret >= size) ? size - 1 : ret;
> - memcpy(dest, src, len);
> - dest[len] = '\0';
> + for(;;)
> + {
> + *dest = *src;
> + if (!*src)
> + break;
> +
> + if (--size == 0)
> + break;
> +
> + dest++;
> + src++;
> }
> - return ret;
> +
> + *dest = 0x00;
> +
> + return dest - tmp;
> }
> EXPORT_SYMBOL(strlcpy);
> #endif
>
>
> I've tested the speed difference with this:
> http://www.vanheusden.com/misc/kernel-strlcpy-opt-test.c
> and the speed difference is quite a bit on a P4: 28% faster.
>
>
> Signed-off by: Folkert van Heusden <folkert@vanheusden.com>
>
>
> Folkert van Heusden

The two do not do exactly the same. The first one handles 'size == 0'
(should not happen?) safely while the other does not.

--
Eyal Lebedinsky (eyal@eyal.emu.id.au) <http://samba.org/eyal/>
attach .zip as .dat
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2006-12-10 22:53    [W:0.062 / U:5.528 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site