D perfomance

random random at spaml.de
Wed Apr 29 16:19:55 UTC 2020


On Wednesday, 29 April 2020 at 12:37:29 UTC, IGotD- wrote:
>
> In the strcmp example, shouldn't the compiler be able to do the 
> same optimizations as you would use restrict because both 
> pointers are declared const and the content do not change?

Good question. My strcmp example is actually really bad, because 
if you never write through any pointer it doesn't make a 
difference ;) The way it is written is still interesting.

I made a quick test case to evaluate the influence of const:

https://godbolt.org/z/qRwFa9
https://godbolt.org/z/iEj7LV
https://godbolt.org/z/EMqDDy

int test(int * x, int * y, <const?> int * <restrict?> z)
{
     *y = *z;
     *x = *z;
     return *z;
}

As you can see from the compiler output const doesn't improve the 
optimization.
I think the compiler can't optimize it because const doesn't give 
you real guarantees in C.
You could just call the function like this.

int a;
test(&a, &a, &a);

"One mans constant is an other mans variable."







More information about the Digitalmars-d mailing list