Skip to content

Commit 1e95edd

Browse files
try lifting tryfinally to macros
1 parent 42c8ac7 commit 1e95edd

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

src/TimerOutput.jl

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,18 @@ Base.@deprecate get_defaultimer get_defaulttimer
172172

173173
# Macro
174174
macro timeit(args...)
175-
return timer_expr(__module__, false, args...)
175+
blocks = timer_expr(__module__, false, args...)
176+
if blocks isa Expr
177+
blocks
178+
else
179+
Expr(:block,
180+
blocks[1], # the timing setup
181+
Expr(:tryfinally,
182+
:($(esc(args[end]))), # the user expr
183+
:($(blocks[2])) # the timing finally
184+
)
185+
)
186+
end
176187
end
177188

178189
macro timeit_debug(args...)
@@ -204,24 +215,32 @@ function is_func_def(f)
204215
end
205216
end
206217

218+
function _esc(ex)
219+
if isa(ex, Expr)
220+
esc(ex)
221+
else
222+
esc(ex[1]), esc(ex[2])
223+
end
224+
end
225+
207226
function timer_expr(m::Module, is_debug::Bool, ex::Expr)
208227
is_func_def(ex) && return timer_expr_func(m, is_debug, :($(TimerOutputs.DEFAULT_TIMER)), ex)
209-
return esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), ex))
228+
return _esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), ex))
210229
end
211230

212231
function timer_expr(m::Module, is_debug::Bool, label_or_to, ex::Expr)
213232
is_func_def(ex) && return timer_expr_func(m, is_debug, label_or_to, ex)
214-
return esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), label_or_to, ex))
233+
return _esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), label_or_to, ex))
215234
end
216235

217236
function timer_expr(m::Module, is_debug::Bool, label::String, ex::Expr)
218237
is_func_def(ex) && return timer_expr_func(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), ex, label)
219-
return esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), label, ex))
238+
return _esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), label, ex))
220239
end
221240

222241
function timer_expr(m::Module, is_debug::Bool, to, label, ex::Expr)
223242
is_func_def(ex) && return timer_expr_func(m, is_debug, to, ex, label)
224-
return esc(_timer_expr(m, is_debug, to, label, ex))
243+
return _esc(_timer_expr(m, is_debug, to, label, ex))
225244
end
226245

227246
function _timer_expr(m::Module, is_debug::Bool, to::Union{Symbol, Expr, TimerOutput}, label, ex::Expr)
@@ -234,27 +253,26 @@ function _timer_expr(m::Module, is_debug::Bool, to::Union{Symbol, Expr, TimerOut
234253
end
235254
$b₀ = $(gc_bytes)()
236255
$t₀ = $(time_ns)()
237-
$(Expr(:tryfinally,
238-
:($val = $ex),
239-
quote
240-
if $enabled
241-
$(do_accumulate!)($accumulated_data, $t₀, $b₀)
242-
$(pop!)($local_to)
243-
end
244-
end))
245-
$val
256+
end
257+
finally_block = quote
258+
if $enabled
259+
$(do_accumulate!)($accumulated_data, $t₀, $b₀)
260+
$(pop!)($local_to)
261+
end
246262
end
247263

248264
if is_debug
249-
return Base.remove_linenums!(quote
265+
return quote
250266
if $m.timeit_debug_enabled()
251267
$timeit_block
252-
else
253-
$ex
254268
end
255-
end)
269+
end, quote
270+
if $m.timeit_debug_enabled()
271+
$finally_block
272+
end
273+
end
256274
else
257-
return Base.remove_linenums!(timeit_block)
275+
return timeit_block, finally_block
258276
end
259277
end
260278

0 commit comments

Comments
 (0)