Adding Deep Immutability

Immutable objects become cyclic because we first create a mutable object graph and then make it immutable. Cycles are pretty common in Python — for example if you freeze the None object, you end up with several cycles.

The SCC additions are an optimisation when you are using free-threaded Python and necessary when you are using subinterpreters (because they are possible, just as you say). The reason for the latter is how subinterpreters do memory management. They assume they are in complete control of all the (non-immortal) objects they can reach, which will no longer be the case when immutable objects become shared between subinterpreters by reference.

We handle this by removing immutable objects from the GC’s of their creating subinterpreters. This means that immutable objects are managed completely using reference counting. So if we did not handle cycles, we would leak memory.

Hope this makes things clear!

1 Like