Skip to content

Visit shared subexpressions once in is_canonical - #3129

Open
blegat wants to merge 1 commit into
masterfrom
bl/is_canonical_aliases
Open

blegat wants to merge 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

`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            |

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

Development

Successfully merging this pull request may close these issues.

1 participant