Skip to content

Memory overhead in large grids due to __dict__ in Cell.__slots__ #3107

@falloficarus22

Description

@falloficarus22

Description

Currently, the Cell class (and by extension, the dynamically created GridCell subclasses) carries significant memory overhead that limits the scalability of Mesa models with large environments.
While Cell uses __slots__, it currently includes "__dict__" in the slots list. In Python, including __dict__ in slots defeats much of the memory benefit because it still initializes a dynamic dictionary for every instance. On modern Python versions, this adds approximately 250–300 bytes of overhead per cell.
In a model with 1,000,000 cells (e.g., a 1000x1000 grid), this translates to ~300MB of unnecessary RAM usage just for the empty dictionaries.

Issues

  1. Redundant __dict__: Cell includes __dict__ in slots, likely to support cached_property and cache, but at a high memory cost.
  2. Subclass Overhead: The Grid class creates dynamic GridCell subclasses. Since these subclasses do not define their own __slots__, Python automatically gives them a __dict__ even if the parent didn't have one.
  3. Property Management: The empty attribute on Cell can clash with the empty property layer in Grid if slots are made strict, requiring a more robust setter/getter implementation.

Proposed Solution

  1. Strict Slots: Remove __dict__ from Cell.__slots__.
  2. Manual Caching: Replace functools.cache and cached_property in Cell with a manual _neighborhood_cache managed within a slot. This maintains performance for spatial queries without requiring a __dict__.
  3. Optimize Subclasses: Ensure GridCell subclasses are created with slots = () to preserve the memory-efficient structure of the parent.
  4. Manage Properties: Refactor the empty attribute into a managed property (_empty slot + empty property) to ensure compatibility with PropertyLayer descriptors used in the Grid system.

Expected Outcome

  • A reduction of ~300 bytes per Cell instance.
  • Improved scalability for high-resolution spatial models.
  • Cleaner separation between core Cell state and Grid-level property layers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions