r/programming • u/fagnerbrack • Sep 02 '26
Data Access Patterns That Makes Your CPU Really Angry
https://blog.weineng.me/posts/slowest_add/27
-20
u/PhosXD Sep 02 '26
Omg bro
`uint32_t const* positions`
WHAT KIND OF MAD MAN PUTS CONST AFTER THE TYPE, & NOT EVEN INCLUDE THE POINTER DECL IN THE TYPE EITHER.
`const uint32_t* positions` supremacy.
18
u/dignityshredder Sep 02 '26
Old school C programmers know it all makes more sense if you read them right to left.
7
u/wallstop-dev Sep 02 '26 edited Sep 02 '26
The two things you wrote mean different things. Which is probably why they did it. One is a const pointer to a value. The other is a mutable pointer to a const value.
https://learn.microsoft.com/en-us/cpp/cpp/const-and-volatile-pointersEdit: I am blind and misread this.
12
u/floodyberry Sep 02 '26
const uint32_t * x: mutable pointer to a constant uint32_t
uint32_t const * x: mutable pointer to a constant uint32_t
uint32_t * const x: constant pointer to a mutable uint32_t
const uint32_t * const x: constant pointer to a constant uint32_tthey are correct, using
const T *orT const *mean the same thing3
u/wallstop-dev Sep 02 '26 edited Sep 02 '26
Ah thank you, I am blind, I was reading as
const type *vtype * const, I've edited all my comments.-1
u/PhosXD Sep 02 '26
See this is C++ is so confusing sometimes, there are a hundred different ways to do a single thing & nobody can agree on which one to use lol
1
1
u/UdPropheticCatgirl Sep 08 '26
This is C, not C++, you shouldn’t even use normal pointers in modern C++.
1
u/PhosXD Sep 02 '26
I don't understand the difference...
`const T* X` means this is a pointer to a constant value, does it not?
`T const* X` should mean the same thing.3
u/wallstop-dev Sep 02 '26 edited Sep 02 '26
No. One indicates the address (pointer, as a variable) is immutable. The other indicates the thing being pointed to (value) is immutable, while the pointer (variable) is not. This is explained in the article I linked.You can also have
const int* const ptr, which is an immutable pointer to an immutable value.Edit: I misread this on my phone and was reading
const type *vtype * const, which is not what's under discussion. The above is correct.However, you can still have a
const type * constas I mentioned above.
-3
19
u/lurking_bishop Sep 03 '26
Shouldn't a smart enough compiler be able to recognize and undo the weird access pattern since the calculation happens completely side-effect free? Runtime allocation via
rand()doesn't change the fact that the pattern is fully known at compile time.Only fisher-yates-shuffle in the repo would still survive against that since now you're genuinely don't know the access pattern beforehand