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.
Use the strlen Function to Find Length of Char Array In some scenarios, char arrays that were initialized or stored as the null-terminated character strings can be measured for size using the strlen function, which is part of the C standard library string utilities.
const can't be optimized because there may be other mutable references to the same memory object. But for immutable references, there cannot be. It is possible to cast away const and immutable in D, but these are only allowed in system code, presumably where the programmer actually does know what he's doing.
Using C library function strlen() method: The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character.
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).
Don't write things like:
static const char* kFoo = "Bar";
You've created a variable named kFoo
that points to constant data. The compiler might be able to detect that this variable does not change and optimize it out, but if not, you've bloated your program's data segment.
Also don't write things like:
static const char *const kFoo = "Bar";
Now your variable kFoo
is const
-qualified and non-modifiable, but if it's used in position independent code (shared libraries etc.), the contents will still vary at runtime and thus it will add startup and memory cost to your program. Instead, use:
static const char kFoo[] = "Bar";
or even:
#define kFoo "Bar"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With