Repository navigation
Warn when a problem is written entry by entry - #46
Merged
Merged
Conversation
Problem (and so compile) now raises sp.PerformanceWarning when more than 1,000 single entries (x[i] or X[i, j]) are indexed out of arrays, the signature of a model built in a Python loop over entries. Index nodes are tagged with an _Index subclass and counted in the existing _collect_leaves walk. The warning is issued before the build and attributed to the first frame outside the package, so it points at the user's call. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
dance858
added a commit
that referenced
this pull request
Dec 1, 2026
main added the PerformanceWarning for problems written entry by entry (#46). _collect_leaves now returns both the entry count for the warning and the build time for stats. The stats test that builds a loop uses 400 entries, below the warning's threshold. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012aJMgsjHjvvLLFNbMGuvAL
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
A model written with a Python loop over entries (
for i in range(n): e += x[i] ** 2) creates a few graph nodes per entry, so it is slower to compile and differentiate than the array form (sp.sum(x ** 2)), and users get no hint of this.sp.Problem(and sosp.compile) now raises a new publicsp.PerformanceWarningwhen more than 500 single entries (x[i]orX[i, j]) are indexed out of arrays.(1, 1)nodes, so inherently scalar models (e.g. a long scalar recurrence with no indexing) do not warn._Index(Node)subclass and counted in the existing_collect_leaveswalk; nodes shared between objective and constraints count once.Problem, before the capsules are built, so it shows up before a slow compile.sp.Problem,sp.compileandsp.CompiledExpressionall report the caller's line.test_compile_deep_sumbuilds 5,000x[i % 3]picks on purpose and is marked to ignore the warning.Testing
python -m pytest tests/— 121 passed. New tests cover: warns for a 2,000-term loop, no warning for the vectorized form / 300 terms / a scalar recurrence, fires once, attributed to the caller's line for all three entry points, and can be silenced with a warnings filter.🤖 Generated with Claude Code