Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize IF THEN ELSE for cases where condition expression resolves to a constant #102

Closed
chrispcampbell opened this issue Aug 4, 2021 · 0 comments · Fixed by #103 or #190
Closed
Assignees
Milestone

Comments

@chrispcampbell
Copy link
Contributor

In the En-ROADS and C-ROADS models, we have a number of equations that are gated by a kind of "feature flag" for things that aren't used in the app but are made available for internal testing and calibration by modelers. The construct usually has the following form:

  a = IF THEN ELSE(Choose scenario > 0, Something complex, Something simple) ~~|

Currently there is no way for SDE to optimize these out when the conditional resolves to a constant value, so we end up generating code (and including data) that doesn't actually get used in the actual app.

I found a way to optimize this by adding a simple ExprReader class that determines whether an expression tree resolves to a single constant numeric value, and then for the case where the condition is constant, EquationReader only needs to visit one branch (the other one can be culled by the unused reference elimination code), and EquationGen can generate the single branch instead of the full IF THEN ELSE.

This change decreases the generated WASM binary for En-ROADS by ~25%, and makes the model run ~10% faster.

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