apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Trawick <traw...@attglobal.net>
Subject Re: apr_strcat optimization(s)
Date Tue, 19 Jun 2001 17:38:08 GMT
Doug MacEachern <dougm@covalent.net> writes:

> the current apr_strcat does strlen() twice for each argument, this patch
> makes it happen just once.  ...
...
> --- srclib/apr/strings/apr_strings.c    2001/05/10 18:05:18     1.13
> +++ srclib/apr/strings/apr_strings.c    2001/06/19 17:12:01
> @@ -137,8 +137,9 @@
>      va_start(adummy, a);
>  
>      while ((argp = va_arg(adummy, char *)) != NULL) {
> -        strcpy(cp, argp);
> -        cp += strlen(argp);
> +        len = strlen(argp);
> +        strncpy(cp, argp, len);

better to memcpy() and then set *cp to '\0' when done... strncpy() is
more expensive because 

1) it has to check multiple conditions which you guaranteed won't be a
    problem
2) I'd wager that memcpy() is inlined by more compilers than strncpy()

More importantly, I think your patch is broken.  Likely I'm confused,
but I don't see how the terminating '\0' gets added in your patch.
strncpy() will bail  out before adding '\0' since it has exhausted the
len bytes it thinks are available in the buffer pointed to by cp.

> +        cp += len;
>      }

recap of what I think should happen:

 1) replace your strncpy with memcpy
 2) add code after the loop to '\0'-terminate the resulting string
 3) test, 'cause I sure didn't

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...


Mime
View raw message