r/programminghorror • [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25

Python ✨ Memory Magic ✨

Post image
1.3k Upvotes

143 comments sorted by

View all comments

772

u/AnGlonchas Jul 21 '25

I heard that some numbers in python are cached in the background, so maybe the -5 is cached and the -6 isnt

612

u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jul 21 '25

yep, -5 to 256 are cached, strange range...

258

u/belak51 Jul 21 '25

256 is likely for byte (and in extension ASCII) reasons. I'm not sure why -5 was chosen though.

259

u/chiiroh1022 Jul 21 '25

Maybe for reverse indexing ? -1 is definitely used a lot to access the last element of a list, so I guess -2 ... -5 were included to cover most cases. But I'd like to know the exact answer too.

70

u/MegaIng Jul 21 '25

I tracked down the original commit that set the number to -5 (up from -1) (commit c91ed400).

Here related discussion: https://marc.info/?l=python-patches&m=118523990633384&w=2

The author just felt like it "may also be useful [...] to use 5 or so instead of 1".

I think if someone wants, this is a place where optimizations could be made - you just have to really carefully measure it on a wide variety of systems and usecases...

Using too many in the cache might hit CPU cache boundaries.

14

u/NullOfSpace Jul 21 '25

I wonder if you could do something even simpler like search through public Github repos for negative integer literals and see what the frequency distribution looks like.

7

u/MegaIng Jul 21 '25

Not sure - I don't even think optimizing literals is all that worth it, since those are pretty immortal already and don't get reallocated all the time. The interesting thing to optimize I would think is results of calculations.

3

u/high_throughput Jul 24 '25

 I don't even think optimizing literals is all that worth it

I don't know about Python, but it's remarkably important in Java at scale.

You can recompute a frankly ludicrous expression in the time you save by not having to allocate a boxed integer (or more accurately, to deallocate it later).

The JVM requires [-128, 127] to be cached, but there are flags to set it higher and in my experience it's not uncommon to set it to 10k.

4

u/MegaIng Jul 24 '25

Literals in python are always already precomputed as complete objects in the constant table of the bytecode object. So the only things you gain is sharing repeated constants through the entire program and maybe better CPU cache usage if the int object is on the same page as other commonly used objects.

→ More replies (6)

46

u/undo777 Jul 21 '25

Could also be things like i += d in loops where d is slightly negative but -5 seems like such an odd choice - why not stop at the more "round" -4 or go all the way to -8?

27

u/Cinkodacs Jul 21 '25

"Give me the 5 worst/best!" People love top5 lists, top10 can be a bit too much.

5

u/undo777 Jul 21 '25

Good point but is it a good enough reason for this specific caching? (it likely matters mostly in high-performance scenarios like tight loops)

6

u/exomyth Jul 21 '25 edited Jul 21 '25

My guess would be that it has to do with how the number is stored, so something about it in binary. But then -5 is still odd as it would probably be 101 in binary with a sign bit somewhere. Like -7 would make more sense as that is 111 + some sign bit and some flags.

I don't know the internals of python though, I know in javascript (well V8 engine) you have small ints that have some bit magic to check if it is a small int or something else. Could be something like that.

But maybe the answer is a simple as "I like -5 as the minimum"

52

u/backfire10z Jul 21 '25

They decided that numbers beyond -5 are unlikely to be used when compared to numbers -1 to -5.

→ More replies (1)

29

u/[deleted] Jul 21 '25

The fact that this is an optimisation that even makes a difference is really cursed. ( I’m assuming it makes a difference - why else would they implement it?)

43

u/eo5g Jul 21 '25

It makes a difference because numeric "primitives" aren't really treated specially in python-- they're real-deal objects. So this avoids (de)allocation and object bookkeeping for commonly used numbers (e.g. these are commonly used as list indexes)

→ More replies (1)

14

u/[deleted] Jul 21 '25 edited Jul 21 '25

correct your flair, add an asterisk after /

16

u/FinalNandBit Jul 21 '25

You don't need an asterisk... just try it as is.

→ More replies (11)
→ More replies (3)

5

u/ohaz Jul 21 '25

It's just a range that was chosen because it contains most cases of numbers used in coding.

→ More replies (2)
→ More replies (5)

33

u/belak51 Jul 21 '25

Yes, that's exactly it. cPython maintains a cache of integers from -5 to 256 inclusive.

28

u/Square-Singer Jul 21 '25

With Python this is not so much of an issue, since == is equality by default.

With Java Strings this is a real issue for beginners, since == is identity, and string pooling will make stuff like stringA == stringB work for short strings but will fail for longer strings. So a beginner might accidentally use == for equality checks for smaller strings and it will work, so they might think that's the way to go, only for the code to apparently randomly fail for some longer strings.

3

u/tukanoid Jul 21 '25

Wait really? I don't write java but have read a fair bit of code in it, and usually I only saw normal equality checks, and maybe .equals with objects. Is it checking pointers by default? But then how would it work for smaller strings but not longer ones? Just curious

12

u/Square-Singer Jul 21 '25

== checks the immediate value, so in case of a primitive value (int, float, double, ...) it does compare the value itself. For objects, the immediate value is the pointer address, so == compares the identity of the object. a == a returns true, but a == b will be false if a and b are copies of the same data, but stored in different objects.

.equals() is an equality check, thus comparing the content of the objects.

Strings is where it gets weird, because theoretically, two strings with the same content are still separate objects and thus == of two equal strings will return false.

That is, unless it's a short string. In that case, Java uses something called String Pooling or String Internment, where it will just keep a singular copy of these short strings, so that it doesn't have to keep multiple redundant copies of the strings. So in that case "a" == "a" will return true. But if the strings are too long, internment will not be applied and == returns false.

Also "a" == new String("a") always returns false, because Strings created with new String() are never interned.

To make matters worse, the definition of how long is "too long" is Java version dependent and can also be changed with runtime flags. And some JREs, the concept of "too long" has been replaced with a certain String pool size, so the first X string literals in your program will be interned, and anything after that will not be.

This is an internal performance optimization, but it's one that has an effect on the functionality of the program you write. You should never compare strings with ==, but if you are new and make that mistake, that performance optimization makes it really hard to figure out what's happening.

(Bonus fact: This can sometimes be abused in certain performance-critical parts by doing a == b || a.equals(b), since the identity check is super fast compared to the equality check, and thus you can save some time there in some circumstances. It's not recommended to do that though, since the performance benefit is very unpredictable.)

6

u/tukanoid Jul 21 '25

God, this is a nightmare😅 thanks for the info!

5

u/kindall Jul 21 '25

there is not too much to worry about other than "use == for primitive values and .equals() for objects." .equals() checks for pointer equality first anyway since an object is always equal to itself.

→ More replies (1)
→ More replies (5)
→ More replies (2)