0

I'm reading High Performance Python by Micha Gorelick. In chapter 3 on tuples vs lists he claims that tuples of size 1-20 are cached by the Python runtime. I did not find an answer to this on the first page of a Google search, nor Python's docs. Can someone provide an official Python source that confirms this claim?

Quotes from book

"Tuples are cached by the Python runtime, which means that we don't need to talk to the kernal to reserve memory every time we want to use one."

"For tuples of sizes 1-20, however, when they are no longer in use the space isn't immediately given back to the system, but rather saved for future use."

Galen
  • 1,128
  • 1
  • 14
  • 31
  • There is a cache of tuples (and afaik lists) used for literals, and the *cache* might match the size of 20. Caching "tuples of size 1-20" seems excessive, as there are possibly tons of them. Can you cite the claim for those of us without access to the book? – MisterMiyagi Jan 18 '21 at 18:26
  • @MisterMiyagi Okay, I'll add the exact quotes soon. – Galen Jan 18 '21 at 18:27
  • Does this answer your question? [How is tuple implemented in CPython?](https://stackoverflow.com/questions/14135542/how-is-tuple-implemented-in-cpython) – MisterMiyagi Jan 18 '21 at 18:34
  • It doesn't directly answer my question because my question requires an official Python source, however it indirectly provided links to what I was looking for. – Galen Jan 18 '21 at 18:47
  • Since this is an implementation detail of CPython, there likely is no official Python source. – MisterMiyagi Jan 18 '21 at 19:04
  • The [source code](https://github.com/python/cpython/blob/master/Objects/tupleobject.c) of the implementation detail is an acceptable source. – Galen Jan 18 '21 at 19:47

0 Answers0