-
Notifications
You must be signed in to change notification settings - Fork 277
Speed up constant * Expr
#1185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Speed up constant * Expr
#1185
Conversation
Replace local prod_v with coef and refactor the numeric-scaling branch to iterate with PyDict_Next instead of a Python dict comprehension. Reuse coef for the product of term coefficients in the Expr * Expr branch and update res assignments accordingly. This simplifies the code and avoids creating intermediate Python dicts during scaling, improving clarity and likely performance.
Add a line to CHANGELOG.md noting a performance improvement: "Speed up `constant * Expr` via C-level API". This documents an optimization that accelerates multiplication of constants by Expr using the C-level API for the upcoming 6.0.0 release.
Extend tests/test_expr.py (test_mul) to cover multiplication of Expr by a scalar and mark Expr*Expr cases. Adds an assertion verifying (x + y) * 2.0 produces the expected string representation and a comment indicating Expr * Expr tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR optimizes the multiplication of a constant with an Expr object by replacing Python-level dictionary comprehension with C-level PyDict_Next iteration, achieving a 1.47x speedup (from 6.58s to 4.44s for the benchmark).
Changes:
- Replaced dictionary comprehension with C-level API (
PyDict_Next) forconstant * Exprmultiplication - Renamed variable
prod_vtocoeffor consistency across multiplication operations - Added test coverage for
Expr * numbermultiplication
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/pyscipopt/expr.pxi | Optimized __mul__ method to use C-level PyDict_Next API for scalar multiplication; renamed prod_v to coef for consistency |
| tests/test_expr.py | Added test case to verify Expr * number multiplication produces correct results |
| CHANGELOG.md | Documented the performance improvement for constant * Expr operations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add an assertion in tests/test_expr.py to verify left-side scalar multiplication works: assert str(2.0 * (x + y)) == "Expr({Term(x): 2.0, Term(y): 2.0})". This ensures that multiplying an Expr by a scalar on the left yields the same result and string representation as Expr * scalar.
As the title. This pr is 1.47x faster than the master branch.