Skip to content

Speed up Term.__init__, add shortcut path for Term() and Term(x) - #1283

Open
Zeroto521 wants to merge 5 commits into
scipopt:masterfrom
Zeroto521:expr/term-perf
Open

Zeroto521 wants to merge 5 commits into
scipopt:masterfrom
Zeroto521:expr/term-perf

Conversation

@Zeroto521

@Zeroto521 Zeroto521 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Term is created for every variable that enters an expression (x + y, 2 * x, quicksum(...), ...) and it is the key of the dict behind Expr.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) and Term(Variable) (n=1), inspared by #1273. Terms with two or more variables construct through the unchanged code path, User can't call Term(x, y) directly in real code. Term(x, y) will be created from Term.__mul__.

case base(ns) head(ns) speedup user can call or not
Term() 315.7 64.6 4.89x YES (via CONST)
Term(v) 564.1 107.3 5.26x YES (via Variable)
Term(v, w) 724.6 610.9 1.19x NO (Fake Case)
Term(w, v) 658.3 691.7 0.95x NO (Fake Case)
Term(v, w, x) 772.2 805.0 0.96x NO (Fake Case)
Term(v, w, x, y) 951.9 905.4 1.05x NO (Fake Case)
Term(v) == Term(v) 115.3 36.7 3.14x YES
Term(v, w, x) == Term(v, w, x) 236.6 48.6 4.87x YES
dict[Term(v)] 111.2 44.3 2.51x YES
Term(v) * Term(w) 1799.5 385.9 4.66x YES
Term(v, w) * Term(x, y) 637.2 237.7 2.68x YES
(v + w) * (x + y) 3072.3 1757.2 1.75x YES
Term._evaluate (1 var) 58.9 54.8 1.07x YES
Term._evaluate (3 vars) 76.7 77.7 0.99x YES

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.
Comment thread src/pyscipopt/scip.pxi
return self

def ptr(self):
cpdef size_t ptr(self) noexcept:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
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.
Comment thread src/pyscipopt/expr.pxi
self.vartuple = vartuple
self.hashval = <Py_ssize_t>hash((var.ptr(),))

else:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant