liveBook · Manning

Object caching and reuse

As explained in Fast Python: High performance techniques for large datasets

Object caching and reuse is a memory-management technique that retains existing objects for repeated use instead of repeatedly creating equivalent objects.

Object caching and reuse retains equivalent objects instead of allocating duplicates. CPython can reuse repeated small values, reducing memory costs when data contains few distinct objects, but behavior depends on implementation, version, and data pattern. section 2.4.1

Testing string reuse

Constructing identical strings with constant expressions may produce the same object, while constructing them through a variable can produce distinct objects. Compare identity with id() and content with ==; equal values do not guarantee reused objects.

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Explains memory effects and identity experiments, but stops short of guaranteeing allocator behavior across implementations or versions.
Examples
Uses repeated character data, string identity comparisons, object ids, and memory-size measurements as concrete probes.
What it assumes
Assumes familiarity with Python objects, strings, lists, and memory-size inspection.
What you can do after
Evaluate reuse with id() and memory-size measurements while avoiding assumptions about undocumented allocation policies.
How it connects
Connects object reuse with CPython allocation policies, object identity, string construction, and memory estimation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.