Surgical performance pass on hot paths#24
Merged
Merged
Conversation
Targeted, behaviour-preserving optimisations from the review: - manipulation/add.py + change.py: resolve equation tokens through a shared (name, compartment) -> metabolite index (_build_met_index) instead of re-scanning model.metabolites per token. Bulk reaction add/change by name drops from O(R*k*M) to O(R*k); the index is updated as new mets are created so cross-token and cross-reaction dedup is preserved. - reconstruction/homology/homology.py: replace DataFrame.apply(axis=1) in the ortholog filter with a comprehension over the columns (membership is already O(1); avoids per-row Series construction). - analysis/sampling.py: build the random objective with optlang add() instead of sum(), which re-canonicalises the expression on every term (O(n^2)). Adds a cross-reaction metabolite-dedup regression test for the add path.
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.
Tier 3 of the raven-python review: a surgical performance pass — verified hot-path
footguns only, no redesign and no behaviour change.
Changes
manipulation/add.py+manipulation/change.py—add_reactions_from_equationsand
change_reaction_equationsresolved each equation token by re-scanningmodel.metabolitesfor a name /name[comp]match, i.e.O(R*k*M)for R reactionsx k tokens on an M-metabolite model. Both now resolve through a shared
(name, compartment) -> metaboliteindex (_build_met_index), dropping the bulkpath to
O(R*k). The index is seeded from the current mets (first match wins,exactly mirroring the old linear scan) and updated as new mets are created, so the
existing cross-token / cross-reaction dedup is preserved.
reconstruction/homology/homology.py— the ortholog filter usedDataFrame.apply(lambda r: ..., axis=1), which builds a Series per row. Replacedwith a comprehension over the two columns (the
model_genesvalues are alreadysets, so membership is O(1)); matches the
zip(..., strict=True)idiom already useda few lines below.
analysis/sampling.py— the random objective was assembled withsum(terms),which re-canonicalises the optlang expression on every term (
O(n^2)— the footgundocumented and avoided everywhere else in the package). Switched to
optlang.symbolics.add(terms), matchinginit/ftinit.pyet al.Not changed (verified no-op)
The plan's fourth perf item (trim
model.copy()in the gap-fill / task loops) isalready done or load-bearing:
init/taskfill.py:_feasiblewas converted towith model:(see its comment — that copy had dominated gap-fill runtime), and theremaining copies in
gapfilling/fill.pybuild or return new models. Nothing safe leftto trim.
Tests / verification
Adds a cross-reaction metabolite-dedup regression test (the invariant the index must
preserve).
pytest(619 passed),ruff check, andmypy(ad-hoc on the changedsources) all green; no behaviour change on the existing suite.