Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time macros: use tryfinally macro approach #39138

Closed

Conversation

IanButterworth
Copy link
Sponsor Member

@KristofferC's suggestion in #39133 of the @tryfinally macro approach is a neat way to maintain scope while ensuring the finalizer is run

I verified that the finalizer is run in each of these:

julia> @time y = 1
  0.000005 seconds
1

julia> y
1

julia> @time error()
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:42
 [2] top-level scope
   @ ~/Documents/GitHub/julia/base/timing.jl:213 [inlined]
 [3] top-level scope
   @ ./REPL[31]:0

julia> x = rand(3,3);

julia> Threads.@threads for i in 1:20 @time x * x; end
  0.565114 seconds (2.36 M allocations: 126.880 MiB, 4.93% gc time, 99.95% compilation time)
  0.566743 seconds (2.36 M allocations: 126.893 MiB, 4.92% gc time, 0.00% compilation time)
  0.000002 seconds (1 allocation: 160 bytes)
  0.565122 seconds (2.36 M allocations: 126.882 MiB, 4.93% gc time, 0.00% compilation time)
  0.568685 seconds (2.36 M allocations: 126.907 MiB, 4.90% gc time, 0.00% compilation time)
  0.566732 seconds (2.36 M allocations: 126.890 MiB, 4.92% gc time, 0.00% compilation time)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000001 seconds (1 allocation: 160 bytes)
  0.565109 seconds (2.36 M allocations: 126.879 MiB, 4.93% gc time, 0.01% compilation time)
  0.000001 seconds (1 allocation: 160 bytes)
  0.000000 seconds (1 allocation: 160 bytes)
  0.000001 seconds (2 allocations: 192 bytes)
  0.000001 seconds (1 allocation: 160 bytes)

This new @tryfinally macro could be pretty useful! Perhaps in a future PR @trycatch could be added and both exported

julia> Base.@tryfinally(xx = 1, println("finalizer"))
finalizer
1

julia> xx
1

julia> Base.@tryfinally(error(), println("finalizer"))
finalizer
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:42
 [2] top-level scope
   @ REPL[37]:1

@IanButterworth
Copy link
Sponsor Member Author

If this is preferable, it should probably go into the next 1.6 beta etc. @JeffBezanson ?

@JeffBezanson
Copy link
Sponsor Member

I don't think it's worth switching to this. tryfinally is an intermediate form that we should not use in source ASTs.

@IanButterworth
Copy link
Sponsor Member Author

Ok!
Just to tie the two together this intended to use the same mechanism #39217

@NHDaly
Copy link
Member

NHDaly commented Aug 2, 2021

Interesting.

This looks great. Makes sense that we want to wait for #39217.

In the meantime, just to be sure everyone knows, we realized that the current @time macro implementation turns off compilation measurement whenever anyone throws an exception, even one that's handled:
https://github.com/JuliaLang/julia/pull/41733/files#r681256138

julia> @time begin
           # The following exception disables compilation time measurement
           try throw("ha HAH!") catch end
           @eval module M ; f(x) = 2*x ; end
           @eval M.f(2)
       end
  0.006116 seconds (2.17 k allocations: 156.874 KiB)
4

So this fix would definitely be nice. 😊

Does it still make sense to wait for #39217? How's the progress there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants