Skip to content

Commit 584bf3d

Browse files
Merge pull request #57 from SciML/as/faster-symbolic-eval
refactor: improve `symbolic_evaluate` performance
2 parents d4b9089 + 554a212 commit 584bf3d

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/trait.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,11 @@ The returned value should either be a value or an expression involving symbolic
7373
not present as keys in `syms`.
7474
7575
This is already implemented for
76-
`symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict{Symbol})`.
76+
`symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict)`.
7777
"""
78-
function symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict{Symbol})
79-
while (new_expr = MacroTools.postwalk(expr) do sym
80-
return get(syms, sym, sym)
81-
end) != expr
82-
expr = new_expr
78+
function symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict)
79+
while (newexpr = _symbolic_evaluate_helper(expr, syms)) != expr
80+
expr = newexpr
8381
end
8482
return try
8583
eval(expr)
@@ -88,6 +86,20 @@ function symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict{Symbol})
8886
end
8987
end
9088

89+
function _symbolic_evaluate_helper(expr, syms::Dict)
90+
if (res = get(syms, expr, nothing)) !== nothing
91+
return res
92+
end
93+
expr isa Expr || return expr
94+
95+
newexpr = Expr(expr.head)
96+
sizehint!(newexpr.args, length(expr.args))
97+
for arg in expr.args
98+
push!(newexpr.args, _symbolic_evaluate_helper(arg, syms))
99+
end
100+
newexpr
101+
end
102+
91103
############ IsTimeseriesTrait
92104

93105
abstract type IsTimeseriesTrait end

0 commit comments

Comments
 (0)