perf(FormulaDict): sort lazily instead of on every assignment - #452
Merged
Conversation
FormulaDict re-sorted its entire contents on every __setitem__, so a burst of writes -- populating a Solution, or rescaling its volume (which does in a loop) -- was O(n**2 log n). Defer the sort: mark the dict dirty on mutation and sort lazily at the iteration barrier (__iter__, and __repr__ which bypasses __iter__ in UserDict). Callers still observe the same descending-by-amount order, since keys()/values()/items()/list()/dict()/for all funnel through __iter__. Deletion preserves relative order, so it leaves the dirty flag untouched. Building all 23 presets drops from ~5843 re-sorts to ~94 (~62x fewer) and is ~20% faster in isolation; test_from_preset wall time falls ~87s -> ~72s. No behavior change: iteration/repr order is identical to eager sorting. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #452 +/- ##
==========================================
+ Coverage 87.75% 87.81% +0.06%
==========================================
Files 14 14
Lines 1935 1945 +10
Branches 337 338 +1
==========================================
+ Hits 1698 1708 +10
Misses 188 188
Partials 49 49 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
FormulaDict(backingSolution.components) re-sorted its entire contents on every__setitem__. A burst of writes — populating aSolution, or thevolumesetter'sself.components[s] *= factorloop — was therefore O(n² log n).This defers the sort: a mutation just marks the dict dirty, and the sort happens lazily at the iteration barrier (
__iter__, plus__repr__sinceUserDict.__repr__readsself.datadirectly). Callers still see the same descending-by-amount order, becausekeys()/values()/items()/list()/dict()/forall funnel through__iter__. Deleting a key preserves relative order, so it leaves the flag untouched.No behavior change — iteration and repr order are identical to the previous eager sorting; only the timing of the sort moves.
Benchmark — building all 23 presets from
test_from_presetFormulaDictre-sorts / full passpytest test_from_presetwall timeTesting
test_from_preset— 23/23 pass.test_component_subsets— directly asserts the descending-amount ordering contract ofcations/anions/neutrals; passes.test_formula_dict,test_charge_balance,test_get_amount,test_components_by_element(_nested),test_arithmetic_and_copy,test_from_dict_complex, and the serialization tests — all pass.🤖 Generated with Claude Code