Skip to content

REPL: delay compilation in macros like @time to avoid the need for @eval#58015

Closed
IanButterworth wants to merge 2 commits intoJuliaLang:masterfrom
IanButterworth:ib/time_delay_compile
Closed

REPL: delay compilation in macros like @time to avoid the need for @eval#58015
IanButterworth wants to merge 2 commits intoJuliaLang:masterfrom
IanButterworth:ib/time_delay_compile

Conversation

@IanButterworth
Copy link
Copy Markdown
Member

@IanButterworth IanButterworth commented Apr 4, 2025

Avoids the need to use @eval with @time etc. in the REPL

With this

julia> @trace_compile rand(2,2) * rand(2,2)
#=   13.6 ms =# precompile(Tuple{Core.GeneratedFunctionStub, UInt64, Method, Any, Vararg{Any}}) # recompile
#=   56.2 ms =# precompile(Tuple{typeof(Base.rand), Int64, Int64})
#=    2.9 ms =# precompile(Tuple{typeof(Base.:(*)), Array{Float64, 2}, Array{Float64, 2}})
2×2 Matrix{Float64}:
 0.740604  0.959697
 0.938691  0.937117

julia> @trace_compile rand(2,2) * rand(2,2)
2×2 Matrix{Float64}:
 0.1135    0.162588
 0.463798  0.891144
julia> @time rand(2,2) * rand(2,2)
  0.090083 seconds (174.88 k allocations: 7.729 MiB, 99.95% compilation time)
2×2 Matrix{Float64}:
 0.402414  0.39624
 0.287094  0.210326

julia> @time rand(2,2) * rand(2,2)
  0.000014 seconds (13 allocations: 528 bytes)
2×2 Matrix{Float64}:
 0.1135    0.162588
 0.463798  0.891144

Without (master)

julia> @trace_compile rand(2,2) * rand(2,2)
2×2 Matrix{Float64}:
 0.686422  0.833379
 1.08226   1.23549

julia> @time rand(2,2) * rand(2,2)
  0.000005 seconds (6 allocations: 336 bytes)
2×2 Matrix{Float64}:
 0.183913  0.224988
 0.362542  0.366543

However it currently breaks for silenced entries.. the expr check needs work..

julia> @trace_compile rand(2,2) * rand(2,2);

julia>

@IanButterworth IanButterworth added REPL Julia's REPL (Read Eval Print Loop) tooling labels Apr 4, 2025
Comment thread stdlib/REPL/src/REPL.jl Outdated
Comment thread stdlib/REPL/src/REPL.jl Outdated
@JeffBezanson
Copy link
Copy Markdown
Member

I'm not sure I like this. But if we want to do it, wouldn't it make more sense to put the @latestworld in the macro implementation instead of special-casing in the REPL?

@timholy
Copy link
Copy Markdown
Member

timholy commented Apr 5, 2025

The concern was about someone putting a @time in a method body when @latestworld needs to be top-level. But I agree this is risky.

@IanButterworth

This comment was marked as resolved.

@IanButterworth

This comment was marked as resolved.

@IanButterworth
Copy link
Copy Markdown
Member Author

This is working now with the new @latestworld_if_toplevel annotation.

I can fix docstrings and add tests if we're ok with this approach. Perhaps discuss it in triage today?

@IanButterworth IanButterworth added the triage This should be discussed on a triage call label Apr 10, 2025
@IanButterworth
Copy link
Copy Markdown
Member Author

Triage were positive about this but did suggest @Keno review usage of this new annotation.

@IanButterworth IanButterworth requested a review from Keno April 10, 2025 17:26
@IanButterworth IanButterworth removed the triage This should be discussed on a triage call label Apr 10, 2025
@IanButterworth IanButterworth force-pushed the ib/time_delay_compile branch from 5f9a93a to 2679a1d Compare April 16, 2025 00:33
@IanButterworth IanButterworth marked this pull request as ready for review April 16, 2025 00:52
Copy link
Copy Markdown
Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel fairly certain this is a major breaking change (it overrides the force compile that is part of the current spec) and should not be merged, though I feel somewhat disinterested in trying to give a real review and explanation, which I know is unfair

@IanButterworth
Copy link
Copy Markdown
Member Author

Ok. I'm not much up for a vague challenge to get this in, so I'll close. Thanks for being upfront.

@IanButterworth IanButterworth deleted the ib/time_delay_compile branch April 16, 2025 02:56
@timholy
Copy link
Copy Markdown
Member

timholy commented Apr 16, 2025

Yes, it would be nice to be able to say both: don't compile before execution reaches "here," but you must compile before execution reaches "there."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

REPL Julia's REPL (Read Eval Print Loop) tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants