Repository navigation
Conversation
This was referenced Aug 11, 2026
mstimberg
pushed a commit
that referenced
this pull request
Sep 21, 2026
…s are enabled The explicit garbage collection at the start of Network.run() only exists to raise the warning from BrianObject.__del__ for objects that were never included in a network, and that warning is already gated by the logging.warn_for_unused_objects preference. Skip the collection when the preference is disabled, document the link in the preference description, and add a regression test. First step towards #1823, supersedes #1843. Co-authored-by: Cursor <[email protected]>
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.
Context & Strategy
This PR is a first, iterative step towards addressing the broader test suite speed issue (#1823). Because a comprehensive solution to the test suite speed will take time, my strategy is to tackle the bottlenecks in measured, incremental stages. This allows us to validate early wins, raise awareness of specific performance traps, and ensure zero regressions before moving on to larger architectural discussions (like unit vs. integration tests).
What
Network.run()callsgc.collect()unconditionally at the start of every run (core/network.py:1110). Its only purpose is to force__del__on unused Brian objects to fire immediately, so the "object was never run" warning can be emitted.That warning is itself gated by
prefs.logging.warn_for_unused_objects(checked first thing in__del__, seecore/base.py) — so when that preference isFalse,gc.collect()does nothing observable. This PR simply makes the call conditional on the same preference.Why this is behavior-preserving
__del__already checksprefs.logging.warn_for_unused_objectsbefore doing anything. Skipping thegc.collect()call when that preference isFalsecannot change anything the user observes — it only changes when unreferenced objects get swept, not whether a warning fires (it never would have).The test harness itself sets
warn_for_unused_objects = Falsefor the codegen_independent/numpy/cython test runs, so this provides a pure speedup there with zero behavior change. Furthermore,test_unused_object_warningandtest_magic_unused_objectboth explicitly set the preference toTruebefore testing, and continue to pass unmodified.Measurements (and why they disagree across machines)
We measured this on two independent setups, using the official
brian2.test()preference/device plumbing to match CI conditions.gc.collect()share of codegen_independent suitetest_neurongroup.py)test_synapses.py) → ~1–2% on a tight run(0*ms)looprun(0*ms), warn=Falsegc.collect(): 37.9s of ~40s total (>90%)These numbers disagree, and we think we know why:
gc.collect()'s cost here isn't proportional to anything the test does — it's the fixed cost of scanning everything already tracked by the GC once brian2 (and numpy/cython) is imported. On our Linux box:That ~80ms baseline is paid on every
gc.collect()call regardless of how many test-created objects exist. The relative impact of this fix will vary by machine/Python version, so please read "45-50%" as the multiplier we could verify, not a universal constant.Next Steps & Out of Scope
The broader plan: I want to gather real numbers on
cpp_standalone(which dominates total suite time by roughly an order of magnitude over numpy/cython in our measurements) before proposing the next steps for the larger #1823 conversation.Unrelated failing tests: While testing this, we hit an apparently pre-existing issue. Running the full
codegen_independentmarker set fortest_network.pyin one session intermittently fails 4 tests (test_network_schedule_change,test_schedule_warning,test_scheduling_summary_magic,test_scheduling_summary) and can hang ontest_magic_network. This is reproducible onmasteras well. Looks like leftover device/clock state leaking. I didn't want to scope-creep this PR with an unrelated fix.