Skip to content

Commit

Permalink
quality improvement for recompile_invalidations (#36)
Browse files Browse the repository at this point in the history
* quality improvement for recompile_invalidations

The previous implementation was unnecessarily forcing compilation of the wrapper code for each use of the macro, and was assuming that hygiene rules do not apply to a gensym in a toplevel block and assuming that the internal tryfinally syntax is legal to generate directly. This change tries to avoid all of those assumptions, and should therefore also give better backtraces too.

* Update invalidations.jl
  • Loading branch information
vtjnash authored Mar 9, 2024
1 parent 184a413 commit 76037ed
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/invalidations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ Recompile any invalidations that occur within the given expression. This is gene
by users in creating "Startup" packages to ensure that the code compiled by package authors is not invalidated.
"""
macro recompile_invalidations(expr)
list = gensym(:list)
Expr(:toplevel,
:($list = ccall(:jl_debug_method_invalidation, Any, (Cint,), 1)),
Expr(:tryfinally,
esc(expr),
:(ccall(:jl_debug_method_invalidation, Any, (Cint,), 0))
),
:(if ccall(:jl_generating_output, Cint, ()) == 1
foreach($PrecompileTools.precompile_mi, $PrecompileTools.invalidation_leaves($list))
end)
)
# use QuoteNode instead of esc(Expr(:quote)) so that $ is not permitted as usual (instead of having this macro work like `@eval`)
return :(recompile_invalidations($__module__, $(QuoteNode(expr))))
end

function recompile_invalidations(__module__::Module, @nospecialize expr)
list = ccall(:jl_debug_method_invalidation, Any, (Cint,), 1)
try
Core.eval(__module__, expr)
finally
ccall(:jl_debug_method_invalidation, Any, (Cint,), 0)
end
if ccall(:jl_generating_output, Cint, ()) == 1
foreach(precompile_mi, invalidation_leaves(list))
end
nothing
end

function invalidation_leaves(invlist)
Expand Down

0 comments on commit 76037ed

Please sign in to comment.