Skip to content

Commit 64c797c

Browse files
committed
refactor: reduce code complexity of eval options
1 parent 5a30d47 commit 64c797c

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

src/Evaluate.jl

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,53 @@ EvalOptions contain flags for the different modes to evaluate an expression.
3636
# Fields
3737
3838
- `turbo::Val`: If `Val{true}`, use LoopVectorization.jl for faster
39-
evaluation.
39+
evaluation.
4040
- `bumper::Val`: If `Val{true}, use Bumper.jl for faster evaluation.
4141
- `early_exit::Val`: If `Val{true}`, any element of any step becoming
42-
`NaN` or `Inf` will terminate the computation and the whole buffer will be
43-
returned with `NaN`s. This makes sure that expressions with singularities
44-
don't wast compute cycles. Setting `Val{false}` will continue the computation
45-
as usual and thus result in `NaN`s only in the elements that actually have
46-
`NaN`s.
47-
48-
# Constructors
49-
50-
EvalOptions(; turbo=Val(false), bumper=Val(false), early_exit=Val(true))
51-
52-
Construct EvalOptions with defaults. Can also be called with boolean values
53-
instead of `Val`s for convenience, although this should be avoided as it
54-
introduces a type instability.
42+
`NaN` or `Inf` will terminate the computation and the whole buffer will be
43+
returned with `NaN`s. This makes sure that expressions with singularities
44+
don't wast compute cycles. Setting `Val{false}` will continue the computation
45+
as usual and thus result in `NaN`s only in the elements that actually have
46+
`NaN`s.
5547
"""
5648
struct EvalOptions{T,B,E}
5749
turbo::Val{T}
5850
bumper::Val{B}
5951
early_exit::Val{E}
6052
end
61-
function EvalOptions(; turbo=Val(false), bumper=Val(false), early_exit=Val(true))
62-
v_turbo = isa(turbo, Val) ? turbo : (turbo ? Val(true) : Val(false))
63-
v_bumper = isa(bumper, Val) ? bumper : (bumper ? Val(true) : Val(false))
64-
v_early_exit = isa(early_exit, Val) ? early_exit : (early_exit ? Val(true) : Val(false))
65-
return EvalOptions(v_turbo, v_bumper, v_early_exit)
53+
54+
@inline _to_bool_val(x::Bool) = x ? Val(true) : Val(false)
55+
@inline _to_bool_val(x::Val{T}) where {T} = Val(T::Bool)
56+
57+
function EvalOptions(;
58+
turbo::Union{Bool,Val}=Val(false),
59+
bumper::Union{Bool,Val}=Val(false),
60+
early_exit::Union{Bool,Val}=Val(true),
61+
)
62+
return EvalOptions(_to_bool_val(turbo), _to_bool_val(bumper), _to_bool_val(early_exit))
63+
end
64+
65+
function _process_deprecated_kws(eval_options, deprecated_kws)
66+
turbo = get(deprecated_kws, :turbo, nothing)
67+
bumper = get(deprecated_kws, :bumper, nothing)
68+
if any(Base.Fix2(, (:turbo, :bumper)), keys(deprecated_kws))
69+
throw(ArgumentError("Invalid keyword argument(s): $(keys(deprecated_kws))"))
70+
end
71+
if !isempty(deprecated_kws)
72+
@assert eval_options === nothing "Cannot use both `eval_options` and deprecated flags `turbo` and `bumper`."
73+
Base.depwarn(
74+
"The `turbo` and `bumper` keyword arguments are deprecated. Please use `eval_options` instead.",
75+
:eval_tree_array,
76+
)
77+
end
78+
if eval_options !== nothing
79+
return eval_options
80+
else
81+
return EvalOptions(;
82+
turbo=turbo === nothing ? Val(false) : turbo,
83+
bumper=bumper === nothing ? Val(false) : bumper,
84+
)
85+
end
6686
end
6787

6888
"""
@@ -71,8 +91,6 @@ end
7191
cX::AbstractMatrix{T},
7292
operators::OperatorEnum;
7393
eval_options::Union{EvalOptions,Nothing}=nothing,
74-
turbo::Union{Bool,Val,Nothing}=nothing,
75-
bumper::Union{Bool,Val,Nothing}=nothing,
7694
) where {T}
7795
7896
Evaluate a binary tree (equation) over a given input data matrix. The
@@ -84,9 +102,7 @@ and triplets of operations for lower memory usage.
84102
- `cX::AbstractMatrix{T}`: The input data to evaluate the tree on.
85103
- `operators::OperatorEnum`: The operators used in the tree.
86104
- `eval_options::Union{EvalOptions,Nothing}`: See EvalOptions for documenation
87-
on the different evaluation modes.
88-
- `turbo::Union{Bool,Val,Nothing}`: Deprecated. Part of EvalOptions now.
89-
- `bumper::Union{Bool,Val,Nothing}`: Deprecated. Part of EvalOptions now.
105+
on the different evaluation modes.
90106
91107
92108
# Returns
@@ -116,28 +132,9 @@ function eval_tree_array(
116132
cX::AbstractMatrix{T},
117133
operators::OperatorEnum;
118134
eval_options::Union{EvalOptions,Nothing}=nothing,
119-
turbo::Union{Bool,Val,Nothing}=nothing,
120-
bumper::Union{Bool,Val,Nothing}=nothing,
135+
_deprecated_kws...,
121136
) where {T}
122-
@assert(
123-
eval_options === nothing || (turbo === nothing && bumper === nothing),
124-
"Cannot use both `eval_options` and deprecated flags `turbo` and `bumper`."
125-
)
126-
#! format: off
127-
_eval_options =
128-
if eval_options !== nothing
129-
eval_options
130-
else
131-
(turbo !== nothing || bumper !== nothing) && Base.depwarn(
132-
"The `turbo` and `bumper` keyword arguments are deprecated. Please use `eval_options` instead.",
133-
:eval_tree_array,
134-
)
135-
EvalOptions(;
136-
turbo = turbo === nothing ? Val(false) : turbo,
137-
bumper = bumper === nothing ? Val(false) : bumper,
138-
)
139-
end
140-
#! format: on
137+
_eval_options = _process_deprecated_kws(eval_options, _deprecated_kws)
141138
if _eval_options.turbo isa Val{true} || _eval_options.bumper isa Val{true}
142139
@assert T in (Float32, Float64)
143140
end

0 commit comments

Comments
 (0)