Skip to content

Visit shared subexpressions once in is_canonical - #3129

Merged
blegat merged 1 commit into
masterfrom
bl/is_canonical_aliases
Nov 17, 2026
Merged

blegat merged 1 commit into
masterfrom
bl/is_canonical_aliases

Conversation

@blegat

@blegat blegat commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

is_canonical(::ScalarNonlinearFunction) is called for every nonlinear constraint added to a Utilities.Model. It was exponential in the depth of the expression graph when subexpressions are shared.

Benchmark of is_canonical on f_{k+1} = sin(f_k) * f_k + f_k with f_0 = sin(x), whose tree has O(3^n) nodes but whose DAG has O(n) nodes:

n master (μs) master (bytes) this PR (μs) this PR (bytes)
1 0.0597 80 0.271 352
4 1.59 176 1.0 944
8 131 464 2.09 1376
12 10800 464 3.81 1840

@blegat
blegat force-pushed the bl/is_canonical_aliases branch from 5da6eb4 to c7c4d8d Compare November 6, 2026 15:27
`is_canonical(::ScalarNonlinearFunction)` is called for every nonlinear
constraint added to a `Utilities.Model`. It was exponential in the depth
of the expression graph when subexpressions are shared, as in a
Runge-Kutta discretization.

Benchmark of `is_canonical` on `f_{k+1} = sin(f_k) * f_k + f_k` with
`f_0 = sin(x)`, whose tree has O(3^n) nodes but whose DAG has O(n) nodes:

| n  | master (μs) | master (bytes) | this PR (μs) | this PR (bytes) |
|----|-------------|----------------|--------------|-----------------|
| 1  | 0.0597      | 80             | 0.271        | 352             |
| 4  | 1.59        | 176            | 1.0          | 944             |
| 8  | 131         | 464            | 2.09         | 1376            |
| 12 | 10800       | 464            | 3.81         | 1840            |
@blegat
blegat force-pushed the bl/is_canonical_aliases branch from c7c4d8d to 66fbd57 Compare November 17, 2026 08:38
@blegat
blegat merged commit dd8fca4 into master Nov 17, 2026
31 checks passed
@blegat
blegat deleted the bl/is_canonical_aliases branch November 17, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant