Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I count on my compiler to optimize strlen on const char *?

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.

like image 492
Jacko Avatar asked Apr 24 '11 13:04

Jacko


People also ask

How do you find the length of a const char array?

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.

Does Const optimize?

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.

How do you find the length of a const character in C++?

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.


2 Answers

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).

like image 173
Pascal Cuoq Avatar answered Sep 20 '22 11:09

Pascal Cuoq


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"
like image 23
R.. GitHub STOP HELPING ICE Avatar answered Sep 19 '22 11:09

R.. GitHub STOP HELPING ICE