Skip to content

Commit da85988

Browse files
IanButterworthKristofferC
authored andcommitted
Profile: remove scope from profile macros (#57858)
Fixes this, which breaks expectations from the way `@time` doesn't introduce a new scope. ``` julia> using Profile julia> @Profile x = 1 1 julia> x ERROR: UndefVarError: `x` not defined in `Main` Suggestion: check for spelling errors or missing imports. ``` (cherry picked from commit d6af199)
1 parent bf9f1e7 commit da85988

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

stdlib/Profile/src/Allocs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ end
7979
function _prof_expr(expr, opts)
8080
quote
8181
$start(; $(esc(opts)))
82-
try
82+
Base.@__tryfinally(
8383
$(esc(expr))
84-
finally
84+
,
8585
$stop()
86-
end
86+
)
8787
end
8888
end
8989

stdlib/Profile/src/Profile.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ appended to an internal buffer of backtraces.
5656
"""
5757
macro profile(ex)
5858
return quote
59-
try
60-
start_timer()
59+
start_timer()
60+
Base.@__tryfinally(
6161
$(esc(ex))
62-
finally
62+
,
6363
stop_timer()
64-
end
64+
)
6565
end
6666
end
6767

@@ -78,12 +78,12 @@ it can be used to diagnose performance issues such as lock contention, IO bottle
7878
"""
7979
macro profile_walltime(ex)
8080
return quote
81-
try
82-
start_timer(true)
81+
start_timer(true);
82+
Base.@__tryfinally(
8383
$(esc(ex))
84-
finally
84+
,
8585
stop_timer()
86-
end
86+
)
8787
end
8888
end
8989

stdlib/Profile/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ end
155155
@test z == 10
156156
end
157157

158+
@testset "@profile no scope" begin
159+
@profile no_scope_57858_1 = 1
160+
@test @isdefined no_scope_57858_1
161+
Profile.clear()
162+
163+
@profile_walltime no_scope_57858_1 = 1
164+
@test @isdefined no_scope_57858_1
165+
Profile.clear()
166+
167+
Profile.Allocs.@profile no_scope_57858_2 = 1
168+
@test @isdefined no_scope_57858_2
169+
Profile.Allocs.clear()
170+
end
171+
158172
@testset "setting sample count and delay in init" begin
159173
n_, delay_ = Profile.init()
160174
n_original = n_

0 commit comments

Comments
 (0)