In my SAX xml parsing callback (XCode 4, LLVM), I am doing a lot of calls to this type of code:
static const char* kFoo = "Bar";
void SaxCallBack(char* sax_string,.....)
{
if ( strcmp(sax_string, kFoo, strlen(kFoo) ) == 0)
{
}
}
Is it safe to assume that strlen(kFoo) is optimized by the compiler?
(The Apple sample code had pre-calculated strlen(kFoo), but I think this is error prone for large numbers of constant strings.)
Edit: Motivation for optimizing: parsing my SVG map on iPod touch 2G takes 5 seconds (!) using NSXMLParser. So, I want to switch to lib2xml, and optimize the string comparisons.
If by "LLVM" you mean clang, then yes, you can count on clang -O
to optimize the strlen
away. Here is what the code for your function looks like:
_SaxCallBack:
Leh_func_begin1:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
leaq L_.str1(%rip), %rsi
movl $3, %edx
callq _strncmp
...
I changed the strcmp
into strncmp
, but the third argument has indeed been replaced by the immediate $3
.
Note that gcc 4.2.1 -O3 does not optimize this strlen
call, and that you can only expect it to work in the precise conditions of your question (especially, the string and the call to strlen
must be in the same file).