Recently, I was going through the source code of Python

Arpit Bhayani

Arpit Bhayani

Sep 08, 2023 • 1 min read


Recently, I was going through the source code of Python and found something really interesting ⚡

Python caches small integers to keep its runtime efficient 🤯 Given that all programs are more likely to use smaller integers Python during its boot loads an array with all small integers.

Whenever someone refers to a small integer, instead of allocating a new object, Python returns the reference to the existing one. This dramatically reduces the number of heap allocations and keeps the runtime efficient.

I have written a detailed write-up about it on Duggup going in-depth of all the nuances and references in CPython source code.

Read - https://lnkd.in/gV7-c6Ac

If you know any interesting trick that Python employs to make things efficient, do share it on Duggup.

⚡ I keep writing and sharing these engineering nuggets, so if you are keen on learning them, follow along.

arpitbhayani.me/courses

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses