Repository navigation
Skip gc.collect() in Network.run() when unused-object warnings are disabled - #1861
Conversation
…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 brian-team#1823, supersedes brian-team#1843. Co-authored-by: Cursor <cursoragent@cursor.com>
Thanks for the review and for updating the comments and issue references! I appreciate you taking the time to look it over. Are there any other tasks or issues I should focus on as a priority right now? |
I'm currently trying to wrap up things to do a release (it's been way too long…) and I am quite busy with some other things, but I'll give it a thought and get back to you! Thanks for the offer to help out 😊 |
Recreates #1860, which was closed automatically on 12 Aug when the fork
hosting its branch was detached during an account cleanup on my side —
my mistake, apologies for the noise.
The head commit is byte-identical to the original PR (
f9291df8be).All prior context and discussion: #1860
Skips the explicit
gc.collect()at the start ofNetwork.run()when thelogging.warn_for_unused_objectspreference is disabled.Motivation
The only purpose of this collection is to trigger
BrianObject.__del__for objects that were created but never included in a network, so that the "was never included in a network" warning can be raised. That warning is already gated byprefs.logging.warn_for_unused_objectsinsideBrianObject.__del__, so when the preference isFalsethe collection has no user visible effect, but it still has a fixed cost: a full pass over all tracked objects (about 140k of them right afterimport brian2, roughly 80 ms per call on my machine).This fixed cost dominates short runs. On Windows (CPython 3.14), 500 consecutive
run(0*ms)calls with the warning disabled spent about 37.9 s out of 40 s (more than 90%) insidegc.collect(). Since #1823 points out that many tests end with arun(0*ms)just to trigger code generation, and since the test runner already setslogging.warn_for_unused_objects = False(brian2/tests/__init__.py), the whole test suite benefits directly from this change: on a small Linux machine (1 vCPU, CPython 3.12),gc.collect()accounted for roughly 45 to 50% of the wall time of thecodegen_independentrun oftest_neurongroup.pybefore this change, and only a few percent after. These numbers are indicative and machine dependent; happy to rerun benchmarks if useful.Behaviour
True, so the collection happens exactly as before.run(). Users who rely on the eager collection for memory reasons can callgc.collect()themselves.test_unused_object_warningandtest_magic_unused_objectpass unchanged (they enable the preference explicitly).Changes
brian2/core/network.py: make the collection conditional and explain why in the comment.brian2/utils/logger.py: document the link in the description of the preference (this also updates the generated preference documentation).brian2/tests/test_network.py: addtest_conditional_gc_collect, a regression test checking viaunittest.mockthat the collection happens if and only if the preference is enabled.Out of scope
MagicNetwork.after_run()also callsgc.collect()after every magicrun(). That call frees objects that were kept alive by the magic network, so it is not purely warning related and is left untouched here; it could be revisited as part of Replace integration tests by unit tests #1823.cpp_standalonerun overhead, which is a separate topic.Supersedes #1843: I had opened the same core change from a branch that has since been deleted, and closed that PR before review. Compared to #1843, this version adds the regression test and the documentation update for the preference.