libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] Re: strlen in libmicrohttpd


From: Christian Grothoff
Subject: [libmicrohttpd] Re: strlen in libmicrohttpd
Date: Fri, 16 Jul 2010 20:40:58 +0200
User-agent: KMail/1.13.3 (Linux/2.6.32-trunk-686; KDE/4.4.4; i686; ; )

Dear Dmitriy,

I don't think this is a good idea.  First of all, using sizeof obfuscates what 
is going on.  Also, should the argument ever be changed (and it's a macro 
defined elsewhere), the code would break.  Finally, and most importantly, some 
(good, recent) compilers actually perform this optimization automatically:

http://www.strchr.com/sse2_optimised_strlen

Clearly there is no point in doing program transformations that uglify and 
possibly break the code if after compilation there is no difference.  For those 
using compilers that don't support this kind of optimization, I'd say talk to 
the compiler hackers and/or change the compiler -- after all, this would 
result in system-wide benefits as opposed to just getting some tiny local 
changes in one app.

Happy hacking,

Christian

On Friday 16 July 2010 12:22:36 you wrote:
> Hello Christian,
> 
> in sources, you often use function "strlen" to compute length of static
> string.
> I think, if you will use "sizeof", this increase speed of the code and
> decrease size of code.
> 
> Example:
> if (0 != strncasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding,
>                         strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))
> 
> Variant 1:
> if (0 != strncasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding,
>                         sizeof (MHD_HTTP_POST_ENCODING_FORM_URLENCODED) -
> 1))
> 
> Variant 2:
> #define STRLEN(x) (sizeof(x) - 1)
> if (0 != strncasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding,
>                         STRLEN(MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))
> 
> Thank you,
> Dmitriy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]