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

313

u/pedersenk Mar 12 '24

Fortran is often cited as being faster than C in specific usecases (i.e heavy maths).

Really though, any language that can generate native machine code without any runtime error checking will reach equal performance.

C++, Rust, Ada, Objective-C, etc can produce almost identical instructions if you avoid their (useful) runtime safety features. This is the compromise that many developers have to make.

117

u/Fevorkillzz Mar 12 '24

Fortran is faster because it has stricter memory aliasing rules / move semantics than C allowing optimizers to do more for heavy math code

48

u/TribladeSlice Mar 12 '24

C code can now express these pointer aliasing rules now using restrict I think. I’m not sure if that can purely replicate all of Fortran’s aliasing rules semantics or not though.

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.

15

u/reini_urban Mar 12 '24

restrict is still broken. And strict-aliasing also.

A more high-level language could easily beat C compilers, but so far only C++, Rust, Fortran and pony did so. .

9

u/flatfinger Mar 12 '24

A lot depends upon the requirements of the tasks being performed, especially the question of whether any theoretically-possible ways of responding to invalid inputs would be considered "unacceptable". If the answer is "no", a C program which doesn't check array bounds may be faster than code in another language which would automatically check bounds, but C compiler's generated machine code would be suitable for a limited range of use cases. If a program is required to be free of things like Arbitrary Code Execution exploits, C source code would need to include more safety checks than would be needed in other languages, negating much of C's "speed advantage".

2

u/Attileusz Mar 12 '24

In a correct program you only need to check any input once. If you write vulnerable code it's your fault for introducing potentially undefined behaviour.

8

u/flatfinger Mar 12 '24

That depends upon what the program is supposed to do. If a program is expected to receive a mixture of valid and corrupted data, to be processed on a "best effort" basis (consider a device reading compressed video recorded on media that might not have been reliably fast enough to keep up with the data feed) a wide variety of displayed pixel patterns may be equally acceptable in response to corrupted data, validating every frame during playback before decompressing it would slow things down severely without offering much benefit.

2

u/Attileusz Mar 12 '24

If you write code that processes noise, and you somehow allow arbitrary code execution. That is entirely your fault. Let me make my statement more precise. You have to check the input at most once. If everything is valid input than no checks. If you need to validate you can validate only once. No need to bounds check every time you access an element at an index of an array.

3

u/spiderpig_spiderpig_ Mar 13 '24 edited Apr 01 '25

versed crawl dependent puzzled tart squeamish forgetful mourn deserted frame

This post was mass deleted and anonymized with Redact

→ More replies (1)

2

u/flatfinger Mar 14 '24

In many languages, integer addition, subtraction, shifts, and bitwise operations are guaranteed to always either yield a result (which, depending upon the application, may or may not be meaningful) with no side effects or (in some languages, depending upon configuration) trap in defined fashion in case of overflow. In such languages, programs that might receive arbitrary data that may or may not be meaningful may often safely ignore integer overflows.

Dialects of C where integer overflow must be avoided at all cost, even in calculations whose results would never be used in a manner that could affect program output nor any conditional branch, require programmers to expend more effort to ensure memory safety than dialects where overflows could be allowed to happen while processing meaningless data. This might be reasonable if the dialects that require extra effort offered major performance benefits, but in many cases they'll at best let programmers get the same performance that other dialects could achieve with less effort.

5

u/SultanOfSodomy Mar 12 '24

and Nim

2

u/reini_urban Mar 15 '24

Lol, nim still compiles to C

→ More replies (1)
→ More replies (2)

5

u/LogMasterd Mar 12 '24

Yeah C/C++ is as fast as Fortran if you avoid pointer aliasing. But Fortran is easier to make fast because it doesn’t have that issue

2

u/cosmic-parsley Mar 13 '24

Rust gets some of these aliasing rules benefits because &mut always gets treated as restrict (noalias). Not that you couldn’t get the same in C but I have never seen an “average” codebase make heavy use of restrict, it’s just too easy to misuse to be worth it. And of course Fortran still does a hair better https://www.reddit.com/r/C_Programming/s/JMd4rPCOpp.

I am not familiar with how move semantics play into Fortran optimizations though, care to share?

3

u/lightmatter501 Mar 15 '24

Rust current has most of that disabled because it turns out those optimization paths in LLVM were VERY buggy precisely because nobody uses them. Then Rust came along was like “what if almost every single mutable thing was noalias?” Rust is as close to fortran as it is mostly because of the gigantic amount of information it can feed the optimizer as a consequence of the ML-based type system, the same way that Haskell can get pretty close to C performance if you use enough types, just being much closer by default. My guess is the Rust will be able to slightly edge out fortran once that is fixed and it gets a “placement new” equivalent (a much needed feature) due to stronger overall type guarantees and the compiler having permission to mess with struct layouts as it pleases.

2

u/MorrisonLevi Jun 11 '24

Isn't this outdated? I thought Nikita and others bashed through enough bugs that Rust has enabled the noalias optimizations for around 3 years?

13

u/manicakes1 Mar 12 '24

Objective-C is dynamically dispatched and so is generally slower than pure C.

That said, objc_direct is a thing now, and it's possible to write fast Objective-C if you are avoiding things like subclassing. It's not the default behavior though.

8

u/pedersenk Mar 12 '24

Indeed. Likewise the C++ v-tables are also causing a performance hit.

If you avoid that kind of code, the identical instructions can be produced.

objc_direct is a new one to me. I will check that out :)

3

u/s-altece Mar 13 '24

__attribute__((objc_direct)), __attribute__((objc_direct_members)), and __attribute__((objc_suppress_direct)). In C2x mode (soon to be C23 mode), you can use the new C attribute syntax [[clang::objc_direct]], [[clang::objc_direct_members]], and [[clang::objc_suppress_direct]].

Direct message sends still share the nil receiver and class initialization semantics of non-direct message sends, so they’re still technically slower than C function calls, but still pretty performant. The main motivation though is actually to reduce binary size: you save a few bytes in the binary since direct methods aren’t added to the method table, and the compiler doesn’t emit a selector for the method unless the _cmd parameter is referenced in the method body. The space savings really add up when you start making private helper methods direct.

Note that direct methods always have “hidden” visibility, so they can only be referenced within the same linkage unit (i.e. you can only use a direct method within the module/library/binary it’s defined in), so you can’t use it in any of your public interfaces.