M
M
MrBman2019-01-04 12:38:30
C++ / C#
MrBman, 2019-01-04 12:38:30

And clang again: Why (during optimization) the implementation on pointers turned out to be worse than using the [ ] operator?

In general, the essence is this, there is a code:

int str_cmp1(const char *s, const char *t)
{
    while (*s == *t && *s != '\0')
        ++s, ++t;
    return *s - *t;
}

int str_cmp2(const char *s, const char *t)
{
  int  i;
    for (i = 0; s[i] == t[i] && s[i] != '\0'; ++i)
        ;
  return s[i] - t[i];
}

Why did lang optimize the second option better?
View this example (Compiler Explorer):

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mercury13, 2019-01-04
@Mercury13

Another thing is interesting here. The compiler turns the first version into the second - base-index addressing is very fast, and adding one twice is not the best option. In addition, it detects parallel loops and arranges the same index for them.
By the way, look at the interest for the sake of gcc -Os.

R
res2001, 2019-01-04
@res2001

In the first case, the first iteration of the loop is executed outside the loop (hence the larger size). In the second variant, all iterations are performed in a loop.
The code inside the loop is the same. The execution speed will be the same.
The only difference is that the first option is a few bytes larger.
By the way, gcc shows identical code in both cases.
I offer another option:

int str_cmp1(const char *s, const char *t)
{
 while (*s == *t++ && *s != '\0')  ++s;
    return *s - *t;
}

M
Mikhail Potanin, 2019-01-10
@potan

The compiler cannot understand that pointer variables are no longer used anywhere (for example, that one of them does not point to the second) and maintains their current value, which interferes with optimization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question