r/C_Programming • • Mar 12 '24

Discussion Why is C so fast and is it possible to create a faster language than C?

Why is C so fast and is it possible to create a faster language than C?

143 Upvotes

136 comments sorted by

View all comments

Show parent comments

40

u/flatfinger Mar 12 '24 edited Mar 12 '24

In FORTRAN, one could easily code a function which is usable to either transform array values "in place" on a single array, or to write transformed values to a "destination" array which is disjoint from the source. A compiler would be entitled to benefit from assuming that Arr1(I) and Arr2(I+K) will be disjoint for any non-zero K, without needing to care about whether Arr1 and Arr2 might be the same array. While that might seem an obscure bit of knowledge to exploit, a compiler that knows that I will change by a certain non-zero amount K during each loop iteration would know that the expression Arr2(I) on one loop iteration will access Arr2(I+K) on the next iteration if there is one; it thus won't alias Arr1(I). In C, a function which receives source and destination arguments via restrict-qualified pointers would only be able to process the latter use case. The way the "formal specification of restrict` is written, if a function were to try to do something like:

    void test(double *restrict src, double const *restrict dest)
    {
      if (src == dest)
      { ... code that **only** uses dest ... }
      else
      { ... code that uses disjoint src and dest ... }

all pointers used within the body of the src == dest case would be simultaneously based upon src and not based upon src, and simultaneously based upon dest and not based upon dest. If compilers reliably treated such ambiguity as indicating that all possibilities must be accommodated (inhibiting optimizations) that might be tolerable, but clang and gcc instead would assume that they may simultaneously treat any arbitrary subset of actions performed via dest as though they were performed using src, while assuming accesses which it has decided to treat as though performed via src won't interact with accesses via dest.

9

u/TribladeSlice Mar 12 '24

Now this is an answer

4

u/grimonce Mar 12 '24

I think I don't understand this answer and I need to give it a second read.

1

u/pkkm Mar 13 '24

Oh, I hadn't thought of that. What a strange edge case, you'd think that it would be impossible to violate restrict if you only ever use a given chunk of memory through one pointer.

Writing your function this way would work though, right?

void test(const double *src, double *dest) {
    if (src == dest) {
	    // Code that only uses dest goes here.
    } else {
	    const double *restrict src_r = src;
	    double *restrict dest_r = dest;

	    // Code that uses disjoint pointers goes here.
    }
}

3

u/flatfinger Mar 13 '24

That would probably work, but I don't think there was ever any intention that pointer comparisons have a side effect of causing a pointer expression that would have been based on a restrict-qualified pointer not being recognized as such. Technically, given:

int x[10];
int test(int *restrict p, int *q)
{
  *p = 1;
  x[p==q] = 2;
  return *p;
}

should allow for the possibility that p and q might both point to x[1]. In that case, because replacing p with a pointer to a copy of x[1] would cause the lvalue expression x[p==q] to refer to a different address, the lvalue expression x[p==q] woul be "based upon" p. The real problem is that the Standard wasn't willing to simply recognize "based upon" as a transitive relation with expressions of easily-recognized forms, and accept that pointers formed by means such as integer-to-pointer conversion might need to be treated as "potentially based upon" any pointers whose representations have been inspected.