From 2edbf8cd5ea7f50cd9449119a058e28b566fce0a Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 21 Mar 2025 18:00:25 -0400 Subject: [PATCH 1/2] Profile: remove scope from profile macros --- stdlib/Profile/src/Allocs.jl | 6 +++--- stdlib/Profile/src/Profile.jl | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/stdlib/Profile/src/Allocs.jl b/stdlib/Profile/src/Allocs.jl index 9d0b18cb468ca..93c9d3392626f 100644 --- a/stdlib/Profile/src/Allocs.jl +++ b/stdlib/Profile/src/Allocs.jl @@ -79,11 +79,11 @@ end function _prof_expr(expr, opts) quote $start(; $(esc(opts))) - try + Base.@__tryfinally( $(esc(expr)) - finally + , $stop() - end + ) end end diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 2e4092fb22c24..27f32fc453d55 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -56,12 +56,12 @@ appended to an internal buffer of backtraces. """ macro profile(ex) return quote - try - start_timer() + start_timer() + Base.@__tryfinally( $(esc(ex)) - finally + , stop_timer() - end + ) end end @@ -78,12 +78,12 @@ it can be used to diagnose performance issues such as lock contention, IO bottle """ macro profile_walltime(ex) return quote - try - start_timer(true) + start_timer(true); + Base.@__tryfinally( $(esc(ex)) - finally + , stop_timer() - end + ) end end From 0439bdbf46adbd7e6ee15cb95e734d86057d22cc Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 21 Mar 2025 22:04:02 -0400 Subject: [PATCH 2/2] add tests --- stdlib/Profile/test/runtests.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/stdlib/Profile/test/runtests.jl b/stdlib/Profile/test/runtests.jl index e7877b949a17e..b487d8963f156 100644 --- a/stdlib/Profile/test/runtests.jl +++ b/stdlib/Profile/test/runtests.jl @@ -155,6 +155,20 @@ end @test z == 10 end +@testset "@profile no scope" begin + @profile no_scope_57858_1 = 1 + @test @isdefined no_scope_57858_1 + Profile.clear() + + @profile_walltime no_scope_57858_1 = 1 + @test @isdefined no_scope_57858_1 + Profile.clear() + + Profile.Allocs.@profile no_scope_57858_2 = 1 + @test @isdefined no_scope_57858_2 + Profile.Allocs.clear() +end + @testset "setting sample count and delay in init" begin n_, delay_ = Profile.init() n_original = n_