News for hackers. Stuff that matters.
Re:Making Graphics Like it's 1993 by kmill on Tuesday June 09, 2026 @07:07PM (#48469064)
I'd be surprised if the compiler didn't make that optimisation on its own.
Re:Making Graphics Like it's 1993 by canyp on Tuesday June 09, 2026 @10:27PM (#48470592)
Possibly, but always check the assembly.

The even faster version, opts aside, would be to initialize the pointer at y*screenRect.w and ++ at every loop to avoid the addressing arithmetic.

Re:Making Graphics Like it's 1993 by kmill on Wednesday June 10, 2026 @02:37AM (#48472304)
Certainly check the assembly, but loop invariant code motion and strength reduction are basic optimizations. C compilers tend to be good at optimizing indexing patterns even at -O1.

Take a look, GCC and Clang go further than these suggestions by adding screenRect.w to the pointer each iteration to avoid the multiplication: https://godbolt.org/z/YfroqK7T6

Writing anything but pixels[y*screenRect.w + x] in an attempt to be faster, without checking the assembly first, is obfuscation.

(For what it's worth, you can beat the compiler by using *pixels++. I didn't profile the code to check it actually was faster in practice however.)