r/C_Programming • u/Valorant_Steve • 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
r/C_Programming • u/Valorant_Steve • Mar 12 '24
Why is C so fast and is it possible to create a faster language than C?
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)andArr2(I+K)will be disjoint for any non-zeroK, without needing to care about whetherArr1andArr2might be the same array. While that might seem an obscure bit of knowledge to exploit, a compiler that knows thatIwill change by a certain non-zero amountKduring each loop iteration would know that the expressionArr2(I)on one loop iteration will accessArr2(I+K)on the next iteration if there is one; it thus won't aliasArr1(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:all pointers used within the body of the
src == destcase would be simultaneously based uponsrcand not based uponsrc, and simultaneously based upondestand not based upondest. 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 viadestas though they were performed usingsrc, while assuming accesses which it has decided to treat as though performed viasrcwon't interact with accesses viadest.