Skip to content

Commit 9054e5d

Browse files
committed
Test precompilation in unittests
1 parent f1a98ed commit 9054e5d

File tree

4 files changed

+89
-43
lines changed

4 files changed

+89
-43
lines changed

src/DynamicExpressions.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ macro ignore(args...) end
4444
@ignore include("../test/runtests.jl")
4545

4646
include("precompile.jl")
47+
do_precompilation(; force_run=false)
4748

4849
end

src/precompile.jl

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -107,56 +107,93 @@ function test_all_combinations(; binary_operators, unary_operators, turbo, types
107107
end
108108
end
109109
end
110+
return nothing
110111
end
111112

112-
@precompile_setup begin
113-
binary_operators = [[+, -, *, /, ^]]
114-
unary_operators = [[sin, cos, exp, log, sqrt, abs, tanh, cosh, sinh]]
115-
turbo = [true, false]
116-
types = [Float16, Float32, Float64]
117-
@precompile_all_calls begin
118-
test_all_combinations(;
119-
binary_operators=binary_operators,
120-
unary_operators=unary_operators,
121-
turbo=turbo,
122-
types=types,
113+
function test_functions_on_trees(::Type{T}, operators) where {T}
114+
local x, c, tree
115+
for T1 in [Float16, Float32, Float64]
116+
x = Node(T1; feature=1)
117+
c = Node(T1; val=T1(1.0))
118+
tree = Node(
119+
2,
120+
Node(1, Node(1, Node(2, x, c), Node(3, c, Node(1, x)))),
121+
Node(3, Node(1, Node(4, x, x))),
123122
)
124123
end
125-
operators = OperatorEnum(;
126-
binary_operators=binary_operators[1],
127-
unary_operators=unary_operators[1],
128-
define_helper_functions=false,
124+
tree = convert(Node{T}, tree)
125+
for preserve_topology in [true, false]
126+
tree = copy_node(tree; preserve_topology)
127+
set_node!(tree, copy_node(tree; preserve_topology))
128+
end
129+
130+
string_tree(tree, operators)
131+
count_nodes(tree)
132+
count_constants(tree)
133+
count_depth(tree)
134+
index_constants(tree)
135+
has_operators(tree)
136+
has_constants(tree)
137+
get_constants(tree)
138+
set_constants(tree, get_constants(tree))
139+
combine_operators(tree, operators)
140+
simplify_tree(tree, operators)
141+
return nothing
142+
end
143+
144+
"""Run force_run=true; @precompile_setup otherwise."""
145+
macro maybe_precompile_setup(force_run, ex)
146+
precompile_ex = Expr(
147+
:macrocall, Symbol("@precompile_setup"), LineNumberNode(@__LINE__), ex
129148
)
130-
# Want to precompile all above calls.
131-
types = [Float16, Float32, Float64]
132-
for T in types
133-
@precompile_all_calls begin
134-
local x, c
135-
for T1 in [Float16, Float32, Float64]
136-
x = Node(T1; feature=1)
137-
c = Node(T1; val=T1(1.0))
138-
end
139-
tree = Node(
140-
2,
141-
Node(1, Node(1, Node(2, x, c), Node(3, c, Node(1, x)))),
142-
Node(3, Node(1, Node(4, x, x))),
149+
return quote
150+
if $(esc(force_run))
151+
$(esc(ex))
152+
else
153+
$(esc(precompile_ex))
154+
end
155+
end
156+
end
157+
158+
"""Run force_run=true; @precompile_all_calls otherwise."""
159+
macro maybe_precompile_all_calls(force_run, ex)
160+
precompile_ex = Expr(
161+
:macrocall, Symbol("@precompile_all_calls"), LineNumberNode(@__LINE__), ex
162+
)
163+
return quote
164+
if $(esc(force_run))
165+
$(esc(ex))
166+
else
167+
$(esc(precompile_ex))
168+
end
169+
end
170+
end
171+
172+
function do_precompilation(; force_run=false)
173+
@maybe_precompile_setup force_run begin
174+
binary_operators = [[+, -, *, /, ^]]
175+
unary_operators = [[sin, cos, exp, log, sqrt, abs, tanh, cosh, sinh]]
176+
turbo = [true, false]
177+
types = [Float16, Float32, Float64]
178+
@maybe_precompile_all_calls force_run begin
179+
test_all_combinations(;
180+
binary_operators=binary_operators,
181+
unary_operators=unary_operators,
182+
turbo=turbo,
183+
types=types,
143184
)
144-
tree = convert(Node{T}, tree)
145-
for preserve_topology in [true, false]
146-
tree = copy_node(tree; preserve_topology)
147-
set_node!(tree, copy_node(tree; preserve_topology))
185+
end
186+
operators = OperatorEnum(;
187+
binary_operators=binary_operators[1],
188+
unary_operators=unary_operators[1],
189+
define_helper_functions=false,
190+
)
191+
# Want to precompile all above calls.
192+
types = [Float16, Float32, Float64]
193+
for T in types
194+
@maybe_precompile_all_calls force_run begin
195+
test_functions_on_trees(T, operators)
148196
end
149-
string_tree(tree, operators)
150-
count_nodes(tree)
151-
count_constants(tree)
152-
count_depth(tree)
153-
index_constants(tree)
154-
has_operators(tree)
155-
has_constants(tree)
156-
get_constants(tree)
157-
set_constants(tree, get_constants(tree))
158-
combine_operators(tree, operators)
159-
simplify_tree(tree, operators)
160197
end
161198
end
162199
end

test/test_precompilation.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using Test
2+
using DynamicExpressions
3+
4+
DynamicExpressions.do_precompilation(; force_run=true)

test/unittest.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ end
6363
@safetestset "Test operators within module" begin
6464
include("test_custom_operators.jl")
6565
end
66+
67+
@safetestset "Test precompilation" begin
68+
include("test_precompilation.jl")
69+
end

0 commit comments

Comments
 (0)