Python Object Interning

RMAG news

This is an interesting thing i have noticed recently asked in an interview…

Object interning

is a technique used in Python to optimize memory usage and improve performance by reusing immutable objects. In Python, objects such as integers (-5 to 256), strings, and tuples are immutable, meaning their values cannot be changed after they are created. Object interning takes advantage of this immutability to reuse objects with the same value rather than creating new ones, thus saving memory and speeding up certain operations.

Here’s why object interning is useful along with some examples in Python:

Memory Optimization:

When Python interns objects with the same value, it ensures that only one copy of the object exists in memory. This saves memory, especially when dealing with large numbers of small immutable objects.

a = 10
b = 10
print(a is b) # True, since a and b point to the same memory location

Performance Improvement:

Object interning can improve the performance of equality comparisons (e.g., ==) and identity comparisons (e.g., is) by comparing memory addresses directly rather than comparing values.

a = 1000
b = 1000
print(a is b) # False, since a and b are different objects
print(a == b) # True, since their values are equal

Caching Immutable Objects:

Python caches small integers (-5 to 256) and some strings, which are commonly used, to further optimize memory usage and performance.

a = 100
b = 100
print(a is b) # True, since a and b are cached and share the same memory

String Interning:

Python interns small strings (length <= 20 characters) automatically. However, you can manually intern strings using the intern() function from the sys module.

import sys

a = hello
b = hello
print(a is b) # True, since a and b are interned

a = sys.intern(hello)
b = sys.intern(hello)
print(a is b) # True, since a and b are explicitly interned

Overall, object interning is a powerful optimization technique in Python that helps reduce memory consumption and improve the performance of certain operations, especially when dealing with immutable objects like integers and strings. However, it’s important to be aware of its limitations and use it judiciously, as not all objects can be interned, and excessive interning can lead to unintended memory leaks.

Leave a Reply

Your email address will not be published. Required fields are marked *