Skip to content

Commit 720f439

Browse files
lift tryfinally to macros
1 parent 42c8ac7 commit 720f439

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

src/TimerOutput.jl

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,37 @@ 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...)
179190
if !isdefined(__module__, :timeit_debug_enabled)
180191
Core.eval(__module__, :(timeit_debug_enabled() = false))
181192
end
182193

183-
return timer_expr(__module__, true, args...)
194+
blocks = timer_expr(__module__, true, args...)
195+
if blocks isa Expr
196+
blocks
197+
else
198+
Expr(:block,
199+
blocks[1], # the timing setup
200+
Expr(:tryfinally,
201+
:($(esc(args[end]))), # the user expr
202+
:($(blocks[2])) # the timing finally
203+
)
204+
)
205+
end
184206
end
185207

186208
function enable_debug_timings(m::Module)
@@ -204,24 +226,32 @@ function is_func_def(f)
204226
end
205227
end
206228

229+
function _esc(ex)
230+
if isa(ex, Expr)
231+
esc(ex)
232+
else
233+
esc(ex[1]), esc(ex[2])
234+
end
235+
end
236+
207237
function timer_expr(m::Module, is_debug::Bool, ex::Expr)
208238
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))
239+
return _esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), ex))
210240
end
211241

212242
function timer_expr(m::Module, is_debug::Bool, label_or_to, ex::Expr)
213243
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))
244+
return _esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), label_or_to, ex))
215245
end
216246

217247
function timer_expr(m::Module, is_debug::Bool, label::String, ex::Expr)
218248
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))
249+
return _esc(_timer_expr(m, is_debug, :($(TimerOutputs).DEFAULT_TIMER), label, ex))
220250
end
221251

222252
function timer_expr(m::Module, is_debug::Bool, to, label, ex::Expr)
223253
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))
254+
return _esc(_timer_expr(m, is_debug, to, label, ex))
225255
end
226256

227257
function _timer_expr(m::Module, is_debug::Bool, to::Union{Symbol, Expr, TimerOutput}, label, ex::Expr)
@@ -234,27 +264,26 @@ function _timer_expr(m::Module, is_debug::Bool, to::Union{Symbol, Expr, TimerOut
234264
end
235265
$b₀ = $(gc_bytes)()
236266
$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
267+
end
268+
finally_block = quote
269+
if $enabled
270+
$(do_accumulate!)($accumulated_data, $t₀, $b₀)
271+
$(pop!)($local_to)
272+
end
246273
end
247274

248275
if is_debug
249-
return Base.remove_linenums!(quote
276+
return quote
250277
if $m.timeit_debug_enabled()
251278
$timeit_block
252-
else
253-
$ex
254279
end
255-
end)
280+
end, quote
281+
if $m.timeit_debug_enabled()
282+
$finally_block
283+
end
284+
end
256285
else
257-
return Base.remove_linenums!(timeit_block)
286+
return timeit_block, finally_block
258287
end
259288
end
260289

0 commit comments

Comments
 (0)