Skip to content

Warn when a problem is written entry by entry - #46

Merged
dance858 merged 2 commits into
mainfrom
entrywise-performance-warning
Dec 1, 2026
Merged

dance858 merged 2 commits into
mainfrom
entrywise-performance-warning

Conversation

@dance858

@dance858 dance858 commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

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 so sp.compile) now raises a new public sp.PerformanceWarning when more than 500 single entries (x[i] or X[i, j]) are indexed out of arrays.

  • Rule: counts scalar index nodes rather than the share of (1, 1) nodes, so inherently scalar models (e.g. a long scalar recurrence with no indexing) do not warn.
  • Counting: index nodes are tagged with an _Index(Node) subclass and counted in the existing _collect_leaves walk; nodes shared between objective and constraints count once.
  • Timing: issued once per Problem, before the capsules are built, so it shows up before a slow compile.
  • Attribution: points at the first frame outside the package, so sp.Problem, sp.compile and sp.CompiledExpression all report the caller's line.
  • README: new "Write array operations, not loops" section, including how to silence the warning.

test_compile_deep_sum builds 5,000 x[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

dance858 and others added 2 commits December 1, 2026 10:53
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
dance858 merged commit 808e456 into main Dec 1, 2026
22 of 27 checks passed
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
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