-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Memory overhead in large grids due to __dict__ in Cell.__slots__ #3107
Copy link
Copy link
Closed
Description
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
- Redundant
__dict__:Cellincludes__dict__in slots, likely to supportcached_propertyandcache, but at a high memory cost. - Subclass Overhead: The
Gridclass creates dynamicGridCellsubclasses. Since these subclasses do not define their own__slots__, Python automatically gives them a__dict__even if the parent didn't have one. - Property Management: The
emptyattribute onCellcan clash with theemptyproperty layer inGridif slots are made strict, requiring a more robust setter/getter implementation.
Proposed Solution
- Strict Slots: Remove
__dict__fromCell.__slots__. - Manual Caching: Replace
functools.cacheandcached_propertyin Cell with a manual_neighborhood_cachemanaged within a slot. This maintains performance for spatial queries without requiring a__dict__. - Optimize Subclasses: Ensure
GridCellsubclasses are created with slots = () to preserve the memory-efficient structure of the parent. - Manage Properties: Refactor the
emptyattribute into a managed property (_empty slot+emptyproperty) to ensure compatibility withPropertyLayerdescriptors used in the Grid system.
Expected Outcome
- A reduction of ~300 bytes per
Cellinstance. - Improved scalability for high-resolution spatial models.
- Cleaner separation between core
Cellstate and Grid-level property layers.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels