SymPy engine: the finiteness check in the constructor fully cancels large rational functions on every intermediate result

Open
#569 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
performance

Research direction

Start by reading SympyPGF.__init__ and expressions._is_finite_function, especially the call to value_at_one._cancel described in the issue. Profile the rw_counter_23 Ψ loop on its sub-invariant before and after the change, then run make golden. Done means the constructor hot path avoids full cancellation and the golden results are unchanged.

Written by the indexing model from the issue text.

Description

area: engines bug priority: high

Found while profiling the engine check of rule PR 3b (#552); exists on main.

SympyPGF.__init__ calls expressions._is_finite_function, which calls value_at_one._cancel: a full cancellation in a FracField (gcd with heugcd) of the whole closed form. This happens for every intermediate result. For the sub-invariant of rw_counter_23 (numerator of 1268 terms, about 80 kB, denominator 1 − 421/500·C²) that is 41 constructor calls and 220 s of 296 s spent in the loop step Ψ. The arithmetic itself (substitution, addition) is cheap.

The cost is not specific to the sandwich. Every analysis on SymPy with large closed forms pays it.

Proposal

  • Decide finiteness without cancelling. A closed form whose denominator after as_numer_denom is a non-constant product form is not finite unless it cancels, so test that cheaply first.
  • Or compute the flag lazily, only when it is needed.

Done when

  • No full cancellation in the constructor on the hot path. The flag is computed only when needed, or by a cheap sufficient test.
  • The profile of rw_counter_23 (Ψ on the sub-invariant, SymPy) is measured before and after.
  • make golden changes no result.
Dominant language
Python
Stars
6
Forks
2
Avg merge
1h 52m
Merged PRs (30d)
187

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from LKlinke/Prodigy

All issues in LKlinke/Prodigy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.