Talks - Vinícius Gubiani Ferreira: PEP 683: Immortal Objects - A new approach for memory managing

Explore PEP 683's immortal objects - a game-changing memory management approach in Python that bypasses GC and GIL, improving performance and memory efficiency.

Key takeaways
  • PEP 683 introduces “immortal objects” - objects that live throughout the entire program execution with an unchanging reference count

  • Immortal objects bypass Python’s garbage collector, reference counting, and Global Interpreter Lock (GIL), improving memory management and performance

  • Common immortal objects include None, True, False, empty strings, and small integers (-5 to 256)

  • Instagram created PEP 683 to address:

    • CPU cache invalidation issues
    • Data race conditions
    • Copy-on-write (COW) memory problems
    • Memory consumption in pre-forking web servers
  • Immortal objects are implemented by setting a very high reference count value in the object’s C structure

  • The change resulted in decreased memory consumption and increased shared memory efficiency as request numbers grew

  • Currently, creating custom immortal objects requires C-level modifications - no Python-level API exists yet

  • PEP 683 works alongside PEP 684 (making GIL optional) to improve Python’s parallel processing capabilities

  • Developers need to be careful with immortal objects as they effectively create memory leaks that persist until program termination

  • The implementation faced challenges with:

    • Platform compatibility across different OS/architectures
    • Backward compatibility
    • Performance optimization
    • Accidental immortalization of objects