perf: fix v1 build-memory regressions (exact-join reindex + Variable×const routing)#804
Merged
Merged
Conversation
_apply_constant_op_v1 / _add_constant_v1 resolve join=None to "exact", then _align_constant returned needs_data_reindex=True unconditionally — so a plain `coeffs * expr` ran xr.align + self.data.reindex_like (two full-dataset deepcopies) even though the factor was already broadcast to self.coords, making the exact-join alignment a no-op. first_mismatched_dim is the §8 exact check (order/label-strict via indexes.equals), so when it finds no mismatch the align changes nothing: return needs_data_reindex=False and take the cheap assign() path, skipping both copies. v1 build peak: isolated multiply -56%; full-build v1/legacy median 1.20x -> 1.00x (legacy unchanged — its default path already returned False). Full suite: 7629 passed under both semantics. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1a3f2be rerouted Variable.__mul__(DataArray) through `to_linexpr() * other` so the §5/§8 checks fire — but that materialises a unit `1*var` expression and then multiplies it, doubling the peak on multiply-heavy builds in BOTH semantics (kvl_cycles 129→168 MB, sparse_network, masked, sos, knapsack — the CodSpeed regressions on #717). Move the checks into the one-step path instead: to_linexpr(other) now enforces §8 (mismatched coefficient coords raise v1 / warn legacy), §11 (aux-coord conflict), alongside the existing §5 NaN check, so Variable.__mul__ can fold the coefficient in at construction with no intermediate expression. Build peak vs merge-base master (1dbde37): kvl_cycles 168→129 MB (parity) in both modes; all varying-data specs 1.00×; legacy/master and v1/master medians 1.00×. Full suite 7629 passed; §5/§8/§11 alignment tests green under both semantics. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Note
AI-assisted (Claude Code): investigation, fixes, and this description; reviewed by me.
Stacked on #717 (base
feat/arithmetic-convention). Fixes the v1 build-memory regressions the semantics work introduced — including the ones CodSpeed flags on #717 (kvl_cycles,sparse_network,masked,sos,knapsack).Two fixes
_align_constant, v1). A plaincoeffs*exprresolvedjoin="exact"and reindexed the whole dataset (a full deepcopy) even though the factor was already broadcast to the expression's coords. Returnneeds_data_reindex=Falsewhenfirst_mismatched_dimconfirms the exact match. → v1/master 1.31× → 1.01×.Variable × constantinto one construction step (both modes).1a3f2bereroutedVariable.__mul__(DataArray)throughto_linexpr() * other, which materialises a unit1*varexpression and then multiplies it — doubling peak on multiply-heavy builds in both semantics. Moved §5/§8/§11 intoto_linexpr(other)so the one-step path is semantics-correct without the intermediate. → clears the CodSpeed regressions.Result — peak vs merge-base master
1dbde37legacy/master 1.00× · v1/master 1.00× — every build spec at parity in both modes. Full suite 7629 passed; §5/§8/§11 alignment tests green under both semantics.
method
Attributed with pytest-benchmem (
--benchmark-memory-profile+ memray); the both-mode regression wasgit bisect-ed to1a3f2be; the master baseline was validated against CodSpeed #717's BASE numbers.