Skip to content

perf: fix v1 build-memory regressions (exact-join reindex + Variable×const routing)#804

Merged
FBumann merged 2 commits into
feat/arithmetic-conventionfrom
perf/v1-build-memory-fixes
Jul 1, 2026
Merged

perf: fix v1 build-memory regressions (exact-join reindex + Variable×const routing)#804
FBumann merged 2 commits into
feat/arithmetic-conventionfrom
perf/v1-build-memory-fixes

Conversation

@FBumann

@FBumann FBumann commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

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

  1. Skip the no-op exact-join reindex (_align_constant, v1). A plain coeffs*expr resolved join="exact" and reindexed the whole dataset (a full deepcopy) even though the factor was already broadcast to the expression's coords. Return needs_data_reindex=False when first_mismatched_dim confirms the exact match. → v1/master 1.31× → 1.01×.
  2. Fold Variable × constant into one construction step (both modes). 1a3f2be rerouted Variable.__mul__(DataArray) through to_linexpr() * other, which materialises a unit 1*var expression and then multiplies it — doubling peak on multiply-heavy builds in both semantics. Moved §5/§8/§11 into to_linexpr(other) so the one-step path is semantics-correct without the intermediate. → clears the CodSpeed regressions.

Result — peak vs merge-base master 1dbde37

legacy/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 was git bisect-ed to 1a3f2be; the master baseline was validated against CodSpeed #717's BASE numbers.

FBumann and others added 2 commits July 1, 2026 14:38
_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>
@FBumann FBumann changed the base branch from feat/arithmetic-convention to master July 1, 2026 12:41
@FBumann FBumann changed the base branch from master to feat/arithmetic-convention July 1, 2026 12:46
@FBumann FBumann marked this pull request as ready for review July 1, 2026 12:47
@FBumann FBumann merged commit 825e90a into feat/arithmetic-convention Jul 1, 2026
2 checks passed
@FBumann FBumann deleted the perf/v1-build-memory-fixes branch July 1, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant