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

refactor: improve symbolic_evaluate performance #57

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
24 changes: 18 additions & 6 deletions src/trait.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@
not present as keys in `syms`.

This is already implemented for
`symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict{Symbol})`.
`symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict)`.
"""
function symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict{Symbol})
while (new_expr = MacroTools.postwalk(expr) do sym
return get(syms, sym, sym)
end) != expr
expr = new_expr
function symbolic_evaluate(expr::Union{Symbol, Expr}, syms::Dict)
while (newexpr = _symbolic_evaluate_helper(expr, syms)) != expr
expr = newexpr

Check warning on line 80 in src/trait.jl

View check run for this annotation

Codecov / codecov/patch

src/trait.jl#L78-L80

Added lines #L78 - L80 were not covered by tests
end
return try
eval(expr)
Expand All @@ -88,6 +86,20 @@
end
end

function _symbolic_evaluate_helper(expr, syms::Dict)
if (res = get(syms, expr, nothing)) !== nothing
return res

Check warning on line 91 in src/trait.jl

View check run for this annotation

Codecov / codecov/patch

src/trait.jl#L89-L91

Added lines #L89 - L91 were not covered by tests
end
expr isa Expr || return expr

Check warning on line 93 in src/trait.jl

View check run for this annotation

Codecov / codecov/patch

src/trait.jl#L93

Added line #L93 was not covered by tests

newexpr = Expr(expr.head)
sizehint!(newexpr.args, length(expr.args))
for arg in expr.args
push!(newexpr.args, _symbolic_evaluate_helper(arg, syms))
end
newexpr

Check warning on line 100 in src/trait.jl

View check run for this annotation

Codecov / codecov/patch

src/trait.jl#L95-L100

Added lines #L95 - L100 were not covered by tests
end

############ IsTimeseriesTrait

abstract type IsTimeseriesTrait end
Expand Down
Loading