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

[Nonlinear] remove recursion when parsing expressions #2020

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions src/Nonlinear/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,38 @@ function parse_expression(
x::Expr,
parent_index::Int,
)
stack = Tuple{Int,Any}[]
push!(stack, (parent_index, x))
while !isempty(stack)
parent, item = pop!(stack)
if item isa Expr
_parse_expression(stack, data, expr, item, parent)
else
parse_expression(data, expr, item, parent)
end
end
return
end

function _parse_expression(stack, data, expr, x, parent_index)
if isexpr(x, :call)
if length(x.args) == 2 && !isexpr(x.args[2], :...)
_parse_univariate_expression(data, expr, x, parent_index)
_parse_univariate_expression(stack, data, expr, x, parent_index)
else
_parse_multivariate_expression(data, expr, x, parent_index)
_parse_multivariate_expression(stack, data, expr, x, parent_index)
end
elseif isexpr(x, :comparison)
_parse_comparison_expression(data, expr, x, parent_index)
_parse_comparison_expression(stack, data, expr, x, parent_index)
elseif isexpr(x, :...)
_parse_splat_expression(data, expr, x, parent_index)
_parse_splat_expression(stack, data, expr, x, parent_index)
elseif isexpr(x, :&&) || isexpr(x, :||)
_parse_logic_expression(data, expr, x, parent_index)
_parse_logic_expression(stack, data, expr, x, parent_index)
else
error("Unsupported expression: $x")
end
return
end

function _parse_splat_expression(data, expr, x, parent_index)
function _parse_splat_expression(stack, data, expr, x, parent_index)
@assert isexpr(x, :...) && length(x.args) == 1
if parent_index == -1
error(
Expand All @@ -74,13 +87,14 @@ function _parse_splat_expression(data, expr, x, parent_index)
"`(x + 1)...`, `[x; y]...` and `g(f(y)...)` are not.",
)
end
for xi in x.args[1]
parse_expression(data, expr, xi, parent_index)
for xi in reverse(x.args[1])
push!(stack, (parent_index, xi))
end
return
end

function _parse_univariate_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
Expand All @@ -91,17 +105,18 @@ function _parse_univariate_expression(
if id === nothing
# It may also be a multivariate operator like * with one argument.
if haskey(data.operators.multivariate_operator_to_id, x.args[1])
_parse_multivariate_expression(data, expr, x, parent_index)
_parse_multivariate_expression(stack, data, expr, x, parent_index)
return
end
error("Unable to parse: $x")
end
push!(expr.nodes, Node(NODE_CALL_UNIVARIATE, id, parent_index))
parse_expression(data, expr, x.args[2], length(expr.nodes))
push!(stack, (length(expr.nodes), x.args[2]))
return
end

function _parse_multivariate_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
Expand All @@ -111,13 +126,12 @@ function _parse_multivariate_expression(
id = get(data.operators.multivariate_operator_to_id, x.args[1], nothing)
if id === nothing
@assert x.args[1] in data.operators.comparison_operators
_parse_inequality_expression(data, expr, x, parent_index)
_parse_inequality_expression(stack, data, expr, x, parent_index)
return
end
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
parent_var = length(expr.nodes)
for i in 2:length(x.args)
parse_expression(data, expr, x.args[i], parent_var)
for i in length(x.args):-1:2
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
Expand All @@ -126,16 +140,16 @@ end
# confused with `_parse_comparison_expression`, which handles things like
# `a <= b <= c`.
function _parse_inequality_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
parent_index::Int,
)
operator_id = data.operators.comparison_operator_to_id[x.args[1]]
push!(expr.nodes, Node(NODE_COMPARISON, operator_id, parent_index))
parent_var = length(expr.nodes)
for i in 2:length(x.args)
parse_expression(data, expr, x.args[i], parent_var)
for i in length(x.args):-1:2
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
Expand All @@ -144,6 +158,7 @@ end
# confused with `_parse_inequality_expression`, which handles things like
# `a <= b`.
function _parse_comparison_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
Expand All @@ -154,14 +169,14 @@ function _parse_comparison_expression(
end
operator_id = data.operators.comparison_operator_to_id[x.args[2]]
push!(expr.nodes, Node(NODE_COMPARISON, operator_id, parent_index))
parent_var = length(expr.nodes)
for i in 1:2:length(x.args)
parse_expression(data, expr, x.args[i], parent_var)
for i in length(x.args):-2:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end

function _parse_logic_expression(
stack::Vector{Tuple{Int,Any}},
data::Model,
expr::Expression,
x::Expr,
Expand All @@ -170,8 +185,8 @@ function _parse_logic_expression(
id = data.operators.logic_operator_to_id[x.head]
push!(expr.nodes, Node(NODE_LOGIC, id, parent_index))
parent_var = length(expr.nodes)
parse_expression(data, expr, x.args[1], parent_var)
parse_expression(data, expr, x.args[2], parent_var)
push!(stack, (parent_var, x.args[2]))
push!(stack, (parent_var, x.args[1]))
return
end

Expand Down
14 changes: 14 additions & 0 deletions test/Nonlinear/Nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,20 @@ function test_eval_atan2()
return
end

function test_deep_recursion()
model = Nonlinear.Model()
x = MOI.VariableIndex(1)
y = Expr(:call, :sin, x)
for _ in 1:20_000
y = Expr(:call, :^, Expr(:call, :sqrt, y), 2)
end
start = time()
@test Nonlinear.parse_expression(model, y) isa Nonlinear.Expression
# A conservative bound to check we're not doing something expensive.
@test time() - start < 1.0
return
end

end

TestNonlinear.runtests()
Expand Down