Open
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.
Contributor
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.
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.
As the title. This pr is 1.47x faster than the master branch.