Repository navigation
perf: store Solution attributes in slots and tighten the memory gate - #5827
Merged
Merged
Conversation
Contributor
|
| Project | PyBaMM |
| Branch | a2/solution-compact-attributes |
| Testbed | bare-metal |
🐰 View full continuous benchmarking report in Bencher
⚠️ WARNING: Truncated view!The full continuous benchmarking report exceeds the maximum length allowed on this platform.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5827 +/- ##
=======================================
Coverage 98.09% 98.09%
=======================================
Files 346 346
Lines 35323 35354 +31
=======================================
+ Hits 34649 34680 +31
Misses 674 674 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
martinjrobins
requested changes
Oct 1, 2026
martinjrobins
left a comment
Contributor
There was a problem hiding this comment.
is it possible to remove the dict if we are using slots? I don't think anyone else should be adding attributes to this class
martinjrobins
approved these changes
Oct 1, 2026
martinjrobins
left a comment
Contributor
There was a problem hiding this comment.
thanks @BradyPlanden !
rtimms
approved these changes
Oct 1, 2026
An experiment keeps four solutions per step (the step, its cycle and the cycle's first and last states), and each carried a full-size __dict__. Solution set more instance attributes than CPython's 30-key shared layout holds, and Python 3.14 pre-fills those keys from __static_attributes__, so the layout also depended on which solutions a process had made before. Slots give every solution the same compact layout. first_state and last_state cache in slots instead of through cached_property, __dict__ stays for attributes callers attach, and __setstate__ loads solutions pickled before Solution had slots.
idaklu's only CasADi rpath is ../casadi, which holds for a wheel beside casadi in site-packages but not for an editable install, whose extension lives in the build tree. import pybammsolvers then failed to load libcasadi unless casadi had been imported first. An editable build also searches the environment's CasADi directory.
With solutions in slots the 20-to-5 cycle ratio is about 1.25 on Python 3.13 and 1.28 on 3.14 when run after the rest of the memory suite, so a 1.4 gate no longer caught a regression the size of main's 1.39.
SolutionBase's slots took _data, set_up_time and solve_time out of the instance dict, but only Solution restored a pre-slots pickle. An older EISSolution put them in __dict__, where the slots hid them, so its impedance, frequencies, data and total_time raised AttributeError. SolutionBase now restores every pickled attribute through setattr, and Solution keeps only its own defaults and renamed state caches.
BradyPlanden
force-pushed
the
a2/solution-compact-attributes
branch
from
October 1, 2026 14:08
7dd26c8 to
423ed38
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Stacked on #5782, whose
Memory testsjob failedtest_cycle_count_memory_scalingwith a 20-to-5 cycle memory ratio of 1.403 against a 1.4 gate.Profiling with
tracemallocon Python 3.13.15 Linux (x86_64 and aarch64, with CI'spybammsolverswheels) showed that the solver statistics #5782 adds cost 72 B per step, oneSolverStatistics. That is within run-to-run noise: main and #5782 both measured 1.39–1.40, so the gate was already at the edge on main. Most of each step's memory wasSolutionoverhead. An experiment keeps four solutions per step (the step, its cycle, and the cycle's cached first and last states), and each carried a full-size__dict__(1,584 B on 3.13).Solutionset more instance attributes than CPython's 30-key shared layout holds, and Python 3.14 pre-fills those keys from__static_attributes__. Whether a solution's attributes fit therefore also depended on which solutions the process had made before, so the test's ratio moved with test order.SolutionandSolutionBasedeclare__slots__, so every solution gets the same compact layout whatever the Python version or process history.__dict__stays, so callers can still attach their own attributes.first_stateandlast_statecache in slots instead of throughfunctools.cached_property, which needs__dict__.Solution.__setstate__loads solutions pickled before this change: a plain dict state, withoutsolver_statistics, with the cached states under their property names.test_cycle_count_memory_scaling's gate drops from 1.4 to 1.3, so a regression to main's level fails it.pybammsolversinstall, whichuv synccreates, can be imported on its own.idaklu's only CasADi rpath was../casadi, which holds for a wheel but not for the build tree an editable install loads from, soimport pybammsolversfailed to loadlibcasadiunlesscasadihad been imported first. Editable builds also search the environment's CasADi directory; wheels are unchanged.Run after the rest of the memory suite, as CI runs it:
Type of change
Add an entry under
# [Unreleased]in CHANGELOG.md, in one of## Breaking changes,## Deprecated,## Features, or## Bug fixes(include PR number; breaking and deprecation entries need a one-line migration note). Internal-only PRs — refactor, docs, CI, tests — can skip this. See RELEASE.md for the full policy.Important checks:
Please confirm the following before marking the PR as ready for review:
nox -s pre-commitnox -s testsnox -s doctests