Skip to content

Commit 336c71b

Browse files
IanButterworthKristofferC
authored andcommitted
Remove try from at-time and close compile timer during throw (#39133)
* remove try from at-time and close compile timer during throw * add scope tests for at-time and aat-timev (cherry picked from commit 03957db)
1 parent 239ab11 commit 336c71b

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

base/timing.jl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,9 @@ macro time(ex)
203203
local stats = gc_num()
204204
local compile_elapsedtime = cumulative_compile_time_ns_before()
205205
local elapsedtime = time_ns()
206-
local val = try
207-
$(esc(ex))
208-
finally
209-
elapsedtime = time_ns() - elapsedtime
210-
compile_elapsedtime = cumulative_compile_time_ns_after() - compile_elapsedtime
211-
end
206+
local val = $(esc(ex))
207+
elapsedtime = time_ns() - elapsedtime
208+
compile_elapsedtime = cumulative_compile_time_ns_after() - compile_elapsedtime
212209
local diff = GC_Diff(gc_num(), stats)
213210
time_print(elapsedtime, diff.allocd, diff.total_time, gc_alloc_count(diff), compile_elapsedtime, true)
214211
val
@@ -252,12 +249,9 @@ macro timev(ex)
252249
local stats = gc_num()
253250
local compile_elapsedtime = cumulative_compile_time_ns_before()
254251
local elapsedtime = time_ns()
255-
local val = try
256-
$(esc(ex))
257-
finally
258-
elapsedtime = time_ns() - elapsedtime
259-
compile_elapsedtime = cumulative_compile_time_ns_after() - compile_elapsedtime
260-
end
252+
local val = $(esc(ex))
253+
elapsedtime = time_ns() - elapsedtime
254+
compile_elapsedtime = cumulative_compile_time_ns_after() - compile_elapsedtime
261255
local diff = GC_Diff(gc_num(), stats)
262256
timev_print(elapsedtime, diff, compile_elapsedtime)
263257
val

src/task.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ static void JL_NORETURN throw_internal(jl_value_t *exception JL_MAYBE_UNROOTED)
587587
{
588588
jl_ptls_t ptls = jl_get_ptls_states();
589589
ptls->io_wait = 0;
590+
// @time needs its compile timer disabled on error,
591+
// and cannot use a try-finally as it would break scope for assignments
592+
jl_measure_compile_time[ptls->tid] = 0;
590593
if (ptls->safe_restore)
591594
jl_longjmp(*ptls->safe_restore, 1);
592595
// During startup

test/core.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5080,6 +5080,19 @@ end
50805080
@test f17255(10000)[1]
50815081
GC.enable(true)
50825082

5083+
# PR #39133, ensure that @time evaluates in the same scope
5084+
function time_macro_scope()
5085+
@time time_macro_local_var = 1
5086+
time_macro_local_var
5087+
end
5088+
@test time_macro_scope() == 1
5089+
5090+
function timev_macro_scope()
5091+
@timev timev_macro_local_var = 1
5092+
timev_macro_local_var
5093+
end
5094+
@time timev_macro_scope() == 1
5095+
50835096
# issue #18710
50845097
bad_tvars() where {T} = 1
50855098
@test isa(which(bad_tvars, ()), Method)

0 commit comments

Comments
 (0)