Repository navigation
Conversation
Avoid sorted, the key lambda, and the generator used to build the hash for the common single-variable case by storing the fresh argument tuple directly and hashing the SCIP variable pointer. Handle the constant term explicitly as well.
Expose Variable.ptr() and Variable.getIndex() as cpdef so Cython callers avoid Python dispatch, and reuse them in Term instead of reading scip_var directly. In the single-variable Term path, fetch the variable with PyTuple_GET_ITEM (the *args tuple is exact, so no bounds check is needed) and keep the Variable type check through the <object> cast, so non-Variable arguments still raise TypeError instead of being read as SCIP_VAR*.
Term.vartuple only ever holds Variables (guaranteed by __init__ and __mul__), so the unchecked PyTuple_GET_ITEM is safe in _evaluate and matches what __eq__ and __mul__ already do. Take the lengths from len(self.vartuple) instead of len(self), which is a PyObject_Length call dispatching through Term.__len__, and use Py_ssize_t for the lengths and loop counters: list/tuple sizes are Py_ssize_t, and storing them in an int would truncate silently.
Zeroto521
commented
Oct 29, 2026
| return self | ||
|
|
||
| def ptr(self): | ||
| cpdef size_t ptr(self) noexcept: |
Contributor
Author
There was a problem hiding this comment.
Variable.ptr() / Variable.getIndex() both are now cpdef (size_t / int, noexcept), so Cython code calls them directly instead of going through the Python method table.
Joao-Dionisio
self-requested a review
October 29, 2026 15:28
hash(tuple(v.ptr() for v in res.vartuple)) goes through a generator and calls the Python-level Variable.ptr() once per variable. Collect the pointers in the merge loop instead and hash the resulting tuple, which keeps the hash identical to the one Term.__init__ builds for the same variables.
Zeroto521
force-pushed
the
expr/term-perf
branch
from
October 29, 2026 15:37
1d87ba2 to
fe8433d
Compare
Zeroto521
commented
Oct 29, 2026
| self.vartuple = vartuple | ||
| self.hashval = <Py_ssize_t>hash((var.ptr(),)) | ||
|
|
||
| else: |
Contributor
Author
There was a problem hiding this comment.
Should Term forbid n > 1? Term(x, y) (n>1) cannot be initialized directly, it’s always comes from Term.mul
This branch has not been deployed
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.
Termis created for every variable that enters an expression (x + y,2 * x,quicksum(...), ...) and it is the key of thedictbehindExpr.terms, so its constructor, its comparison and the*used to multiply monomials are all on the hot path of modelling code.This PR adds a shortcut path for
Const=Term()(n=0) andTerm(Variable)(n=1), inspared by #1273. Terms with two or more variables construct through the unchanged code path, User can't callTerm(x, y)directly in real code.Term(x, y)will be created fromTerm.__mul__.Term()CONST)Term(v)Variable)Term(v, w)Term(w, v)Term(v, w, x)Term(v, w, x, y)Term(v) == Term(v)Term(v, w, x) == Term(v, w, x)dict[Term(v)]Term(v) * Term(w)Term(v, w) * Term(x, y)(v + w) * (x + y)Term._evaluate(1 var)Term._evaluate(3 vars)