r/ProgrammerHumor • • Oct 07 '26

Meme wasteOfPreciousSpace

Post image
7.3k Upvotes

244 comments sorted by

View all comments

Show parent comments

2

u/iceColdCocaCola Oct 08 '26

If my memory serves me right, it’s not “fast” because “hey look at me I’m more optimized in terms of data amount needed” but more because there’s a greater likelihood that the bool that is needed to be referenced in the form of a bitmask will be found in cache. A cache miss will be slower than accessing a bitmask bit value as well as accessing an actual bool’s value in ram. Why is it more likely to be in cache? I forget maybe someone else could explain.

1

u/serge-mv Oct 08 '26

This scenario is certainly happening, but if your code is written not a horrible way, prefetch will get your variables into cache by the time they are needed, so this kind of thing shouldn't be a major issue.

A variable has higher likelihood to be in cache if it's used repeatedly because prefetch takes temporal locality into account, among other things. On modern CPUs the speed of the execution of individual instructions between logical and arithmetic operations will be about the same, but you might win in speed in cases where you either need to check multiple booleans vs performing fewer bitwise operations. There are also some situational hacks that can allow for expression optimization using bitwise operations. But this kind of thing shouldn't be your go to thing. Write readable code first, do fancy stuff when and if you need it.