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.
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.
|
@Joao-Dionisio Hi, could we follow up on these PRs? I'd love to get them wrapped up while the implementation context is still fresh in my mind. |
|
Hey @Zeroto521 . I'll try to take a look this week, sorry for the delay. |
|
Should |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
As the title. This pr is 1.47x faster than the master branch.