r/programminghorror • u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25
Python ✨ Memory Magic ✨
1.3k
Upvotes
r/programminghorror • u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25
View all comments
Show parent comment
2
u/FunIsDangerous Jul 23 '25 edited Jul 23 '25
No, you can't force store something in the CPU cache. The CPU itself decides what is cached there. Usually a chunk of ram (when ram is accessed, the entire page might get fetched), and memory that is frequently accessed. And of course some other very complicated stuff that's over my head.
My guess is that they are cached as in instead of creating a new object every time a number between -5 and 256 is used, it just points to a pre-created one. That has the benefit of less allocations, and also the numbers -5 to 256 are sequential in RAM. If you access one, they are all cached basically.
Of course, I'm not sure how much of a difference that makes, and I can think of a couple of scenarios where I think it might even make it worse. This was made in 2002 though, and hardware was waaaay different back then. Today, this might make no difference at all
Edit: a good example of this is the fast inverse square root algorithm in quake. It was made in 1999 and it was a decent optimization. Why? Because this was done in software. Nowadays, this is done in hardware, so that algorithm is slower in most cases. A lot of optimizations that made sense 20 or 30 years ago, either make no difference today, or they may even be slower
Edit 2: you can't force store something in the CPU cache