4 ms·
>Is that really what's going on here? I'm in way too deep to be sure what's best for beginner programmers, but I feel like Python must surely optimise... For t
by faho 5y ago
>Is that really what's going on here? I'm in way too deep to be sure what's best for beginner programmers, but I feel like Python must surely optimise...
For these particular numbers, CPython has one optimization I know of: Small integers (from -5 to 256) are pre-initialized and shared.
See https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong
This is generally invisible to the python programmer, but leads to https://wsvincent.com/python-wat-integer-cache/ https://wsvincent.com/python-wat-integer-cache/
- tialaramex 5y agoThanks! That's a wild ride. On my system these "cached" integers seem to each be 32 byte objects, so, over 8kB of RAM is used by CPython to "cache" the integers -5 through 256 in this way. There's similar craziness over in String town. If I mint the exact same string a dozen times from a constant, those all have the same id, presumably Python has a similar "cache" of such constant strings. But if I assemble the same result string with concatenation, each of the identical strings has a different id (this is in 3.9) So, my model of what's going on in a Python program was completely wrong. But the simple pedagogic model in the article was also wrong, just not in a way that's going to trip up new Python programmers.
- faho 5y ago>presumably Python has a similar "cache" of such constant strings. Not really. You're hitting constant folding: https://arpitbhayani.me/blogs/constant-folding-python https://arpitbhayani.me/blogs/constant-folding-python This isn't a pre-made list of certain strings that should be cached, this is the compiler noticing that you mentioned the same constant a bunch of times. Also in general you would see a lot of things with the same id because python uses references all over the place. E.g. assignment never copies.