Skip to content

Remove the constraint cache that measures as doing nothing - #137

Merged
bernalde merged 1 commit into
mainfrom
refactor/issue-136-remove-memoize-by-x
Aug 5, 2026
Merged

bernalde merged 1 commit into
mainfrom
refactor/issue-136-remove-memoize-by-x

Conversation

@bernalde

@bernalde bernalde commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Closes #136.

What changed

Removed memoize_by_x from lyopronto/functions.py and its six call sites in opt_Tsh, opt_Pch, and opt_Pch_Tsh. The stacked constraint functions are now passed to SLSQP directly.

The constraint stacking and the jac= arguments are untouched — those carry the entire 3.2–3.4× speedup measured when they landed.

Why

The cache has nothing to hit. Once the residuals are stacked into one vector-valued constraint, SLSQP performs a single finite-difference sweep per point and every evaluation inside that sweep is at a different x.

Its docstring claimed it "avoids solving the full constraint system once per component", which is what the stacking does. That made the helper read as load-bearing when it was not — the reason to remove it rather than leave it in place.

It also never evicted: every point SLSQP visited was retained, keyed by the 7-vector's bytes, for the duration of each sp.minimize call.

Verification

Trajectories are bitwise identical to main on all three optimizers, using the standard fixtures from tests/conftest.py — same shape, np.array_equal true across all seven columns (time, Tsub, Tbot, Tsh, Pch, flux, percent dried).

No runtime regression. A first 3-repeat pass showed opt_Tsh at +6.6%, which looked like a regression. It was noise. Re-measured with 7 interleaved repetitions in separate subprocesses, alternating between two scratch trees so neither variant benefits from warm state:

median min max stdev
with cache (main) 3.627 s 3.569 s 4.145 s 0.198 s
without cache (this PR) 3.494 s 3.419 s 4.253 s 0.323 s

The between-variant difference is 3.7% of median and favors removal; the run-to-run spread within a single variant is 15.9% of median. The difference is below the noise floor, so the honest claim is "no measurable effect either way", not "removal is faster". Output checksums equal across every repetition.

Acceptance criteria

  • memoize_by_x is gone and git grep memoize returns nothing.
  • The three optimizers still stack constraints and pass exact objective gradients.
  • Trajectories bitwise identical to main on the three conftest.py cases.
  • No runtime regression (difference within measurement noise, see above).
  • Full non-Pyomo lane green.

Tests run

  • ruff check lyopronto tests examples main.py — clean.
  • pytest tests/ -n auto -m "not pyomo" — see the check run on this head.
  • Trajectory and A/B timing comparison described above.

mypy lyopronto was not run to completion: this environment's numpy stubs need a newer python_version than pyproject.toml sets. Pre-existing and advisory; CI's Static analysis check covers it.

Follow-up

Review of this PR surfaced that opt_Pch_Tsh — one of the three modules changed here — has no reference-trajectory test, so its share of the bitwise-identity claim rests on the manual comparison alone. Tracked in #138 and deliberately not folded into this PR, which is a four-file deletion.

Related

memoize_by_x was deliberately excluded from LyoHUB#42, which upstreams the constraint stacking. Removing it here also shrinks the fork's delta against upstream by one function.

memoize_by_x wrapped both stacked constraint functions in all three
optimizers, but the cache has nothing to hit: once the residuals are stacked
into one vector-valued constraint, SLSQP performs a single finite-difference
sweep per point and every evaluation inside that sweep is at a different x.

Its docstring claimed it avoided solving the full constraint system once per
component, which is what the stacking does. That made the helper read as
load-bearing when it was not, which is the reason to remove it rather than
leave it in place. It also never evicted: every point SLSQP visited was
retained for the duration of each sp.minimize call.

The stacking and the jac= arguments are untouched -- those carry the entire
3.2-3.4x speedup.

Trajectories are bitwise identical to main on all three optimizers using the
tests/conftest.py fixtures: same shape, np.array_equal across all seven
columns. Timing is unchanged within measurement noise -- 7 interleaved
repetitions give medians of 3.627 s with the cache and 3.494 s without, while
the run-to-run spread within a single variant is 15.9% of the median.

Closes #136.
@bernalde
bernalde marked this pull request as ready for review August 5, 2026 20:37

@bernalde bernalde left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintainer review of 21b5ddc. This account authored the PR, so GitHub only accepts a COMMENT event; see the merge-readiness note at the end for what that does and does not mean.

A pure deletion with a behavior-preservation claim, so the review is mostly about whether the claim holds and whether anything still depends on what was removed.

What I verified

Nothing depends on the removed helper. git grep memoize at head returns nothing across source, tests, examples, benchmarks, and docs. docs/reference.md renders the API through mkdocstrings (::: lyopronto, show_submodules: true), so the entry disappears automatically rather than leaving a dangling reference.

Removing it is still free. memoize_by_x appears in no tagged release — I checked every tag in the repository, and none contains it. There is no compatibility owed, which is exactly the window in which a superseded helper should go.

The optimization that actually works is intact. All three modules still build eq_sys/ineq_sys and still pass jac=; only the cache wrapper is gone.

The behavior claim holds. Trajectories are bitwise identical to main on all three optimizers using the tests/conftest.py fixtures — np.array_equal true across all seven columns.

The timing claim is correctly stated. The PR body reports the difference as within measurement noise rather than quoting the favorable number, which matches what the interleaved 7-repetition A/B shows: 3.7% between variants against 15.9% run-to-run spread within a single variant. Reporting that as "no measurable effect" is the right call.

Findings

Nonblocking: the bitwise-identity guarantee has no standing guard

The claim that trajectories are unchanged rests on a one-off local measurement. Nothing in the suite would catch a future edit to these same three files that shifted them.

The reference-trajectory tests are deliberately loose — test_optimizer_matches_reference_trajectory allows 5% on percent dried, 1% on final time, and 0.5 °C on maximum product temperature. Those tolerances are appropriate for what they were written to do (guard against gross physical regressions), but they leave a wide band in which a numerical change passes unnoticed.

More specifically: of the three modules this PR changes, opt_Pch_Tsh has no reference-data test at all. test_opt_Tsh.py and test_opt_Pch.py both load test_data/reference_opt_*.csv; there is no reference_opt_Pch_Tsh.csv and no equivalent test.

This gap is pre-existing and not introduced here, so it does not block a deletion that is otherwise well evidenced. But it is worth naming on this PR specifically, because this PR's entire justification is "no behavioral change" — and a reviewer should know that assurance came from a manual comparison rather than from CI.

Suggested resolution: a follow-up issue to add a reference-trajectory test for opt_Pch_Tsh, generated from main before any further optimizer work. I would not expand this PR to do it — generating and committing reference data is its own change with its own review surface, and folding it in here would bury a clean four-file deletion.

Tests run

At head 21b5ddc, repo's active conda environment:

  • pytest tests/ -n auto -m "not pyomo" — 375 passed
  • ruff check lyopronto tests examples main.py — clean
  • Trajectory comparison against main on the three conftest.py cases — bitwise identical
  • Interleaved A/B timing, 7 repetitions per variant in separate subprocesses from two scratch trees — medians 3.627 s with the cache, 3.494 s without; checksums equal on every repetition

mypy lyopronto could not run: this environment's numpy stubs need a newer python_version than pyproject.toml sets. Pre-existing and advisory; CI's Static analysis check passed it.

GitHub checks: the four post-ready_for_review runs are all green — Full Validation, PR Tests, Pyomo Tests, Notebook Tests. These supersede the pre-transition runs on the same SHA, which this repository's workflows re-trigger because they subscribe to ready_for_review.

Merge readiness

No blocking findings. The one nonblocking item is a pre-existing coverage gap whose recommended resolution is a follow-up issue, not a change here.

main requires the four status contexts above and sets no required_pull_request_reviews, so no approving review is enforced; required_conversation_resolution is off, so the inline thread this review creates will not block the merge. This account authored the PR, so GitHub restricts it to COMMENT — a mechanical limit, not an unmet gate.

Comment thread lyopronto/opt_Pch_Tsh.py Resolved
@bernalde

bernalde commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

Addressed the maintainer review

No code change. The single finding was Nonblocking and prescribed its own resolution as a follow-up rather than an edit here, so the head is unchanged at 21b5ddc.

Finding and resolution

Nonblocking: the bitwise-identity guarantee has no standing guard. Recorded as #138.

Verified at this head before filing: test_opt_Tsh.py and test_opt_Pch.py each load a committed test_data/reference_opt_*.csv, there is no reference_opt_Pch_Tsh.csv, and tests/test_opt_Pch_Tsh.py asserts physical reasonableness, bounds, and relative properties without pinning a trajectory. The finding's premise holds.

Deferred rather than folded in because generating and committing reference data is its own change with its own review surface, and it would bury a four-file deletion. #138 also captures that the existing tolerances (5% percent-dried, 1% final time, 0.5 °C max temperature) guard gross physical regressions rather than numerical drift, so closing the gap and revisiting the tolerances belong together.

Cross-links

Nothing else outstanding

No Blocking findings were raised and none were declined. Current-head CI is the four post-ready_for_review runs, all green: Full Validation, PR Tests, Pyomo Tests, Notebook Tests.

@bernalde
bernalde merged commit dfdb53d into main Aug 5, 2026
17 checks passed
@bernalde
bernalde deleted the refactor/issue-136-remove-memoize-by-x branch August 5, 2026 21:35
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.

Remove memoize_by_x: the constraint cache measures as contributing nothing

1 participant