News for hackers. Stuff that matters.
Re:Is Rust faster than C? by randomNumber7 on Wednesday January 14, 2026 @12:44PM (#46619285)
In C there is the "restrict" keyword to tell the compiler that there is no other pointer to the values accessed over a certain pointer.

If you do not use that the generated code can be quite suboptimal in certain cases.

Re:Is Rust faster than C? by throwaway2037 on Thursday January 15, 2026 @04:06AM (#46630003)

    > If you do not use that the generated code can be quite suboptimal in certain cases.
I believe you, but I don't understand it. Can you give a simple example to demonstrate your point?
Re:Is Rust faster than C? by bennettnate5 on Thursday January 15, 2026 @11:02AM (#46634513)
The simplest example is `memcpy(dst, src, len)` and similar iterative byte copying operations. If the function did not use noalias, the compiler wouldn't be free to optimize individual byte read/writes into register-sized writes, as the destination may overlap with the source. In practice this means 8x more CPU instructions per copy operation on a 64-bit machine.

Note that memcpy specifically may already be implemented this way under the hood because it requires noalias; but I imagine similar iterative copying operations can be optimized in a like manner ad-hoc when aliasing information is baked in like it is with Rust.

Re:Is Rust faster than C? by repstosb on Sunday January 18, 2026 @07:06AM (#46667148)
That's not a great example, since memcpy() already has all the information it needs to determine whether the regions overlap (src < dest + len && dst < src + len) and even where and by how much. So pretty much any quality implementation is already performing this test and selecting an appropriate code path, at the cost of a single branch rather than 8x as many memory operations.

The real purpose of restrict is to allow the compiler to cache a value that may be used many times in a loop (in memcpy(), each byte/word is used only once) in a register at the start of the loop, and not have to worry about repeatedly reaching back to memory to re-retrieve it because it might have been modified as a side effect of the loop body.

Re:Is Rust faster than C? by randomNumber7 on Friday January 16, 2026 @10:48AM (#46647668)
Say you have 2 pointers (that might overlap). You (or the compiler) keep one value read from the first pointer in a register, since the value is needed multiple times.

You then write access the second pointer. Now the value you kept in the register is invalidated since you might have overwritten it through the overlapping pointers.