Skip to content

Commit

Permalink
move time_imports and trace_* macros to Base but remain owned by Inte…
Browse files Browse the repository at this point in the history
…ractiveUtils (#56276)

This way all packages can be timed including InteractiveUtils and its
deps (Base64, JuliaSyntaxHighlighting, Markdown, StyledStrings).

With this PR
```
% ./julia --start=no -e "@time Base.@time_imports using REPL"
     41.8 ms  StyledStrings
               ┌ 0.1 ms JuliaSyntaxHighlighting.__init__()
     14.2 ms  JuliaSyntaxHighlighting
      1.0 ms  Base64
               ┌ 0.0 ms Markdown.__init__()
      9.6 ms  Markdown
      2.2 ms  InteractiveUtils
      0.3 ms  Unicode
               ┌ 0.0 ms REPL.REPLCompletions.__init__()
               ├ 0.0 ms REPL.__init__()
     95.7 ms  REPL
  0.225907 seconds (290.95 k allocations: 16.761 MiB)
```

Otherwise
```
% ./julia --start=no -e "using InteractiveUtils; @time @time_imports using REPL"
      0.5 ms  Unicode
               ┌ 0.0 ms REPL.REPLCompletions.__init__()
               ├ 0.1 ms REPL.__init__()
    107.5 ms  REPL
  0.127016 seconds (164.18 k allocations: 9.199 MiB)
```

Also the `@trace_compile` and `@trace_dispatch` macros for the same
reason.
  • Loading branch information
IanButterworth authored Oct 22, 2024
1 parent 7d4b2b7 commit ab22f98
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
35 changes: 35 additions & 0 deletions base/timing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,38 @@ macro timed(ex)
)
end
end

# Exported, documented, and tested in InteractiveUtils
# here so it's possible to time/trace all imports, including InteractiveUtils and its deps
macro time_imports(ex)
quote
try
Base.Threads.atomic_add!(Base.TIMING_IMPORTS, 1)
$(esc(ex))
finally
Base.Threads.atomic_sub!(Base.TIMING_IMPORTS, 1)
end
end
end

macro trace_compile(ex)
quote
try
ccall(:jl_force_trace_compile_timing_enable, Cvoid, ())
$(esc(ex))
finally
ccall(:jl_force_trace_compile_timing_disable, Cvoid, ())
end
end
end

macro trace_dispatch(ex)
quote
try
ccall(:jl_force_trace_dispatch_enable, Cvoid, ())
$(esc(ex))
finally
ccall(:jl_force_trace_dispatch_disable, Cvoid, ())
end
end
end
37 changes: 4 additions & 33 deletions stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import Base: typesof, insert!, replace_ref_begin_end!, infer_effects

# defined in Base so it's possible to time all imports, including InteractiveUtils and its deps
# via. `Base.@time_imports` etc.
import Base: @time_imports, @trace_compile, @trace_dispatch

separate_kwargs(args...; kwargs...) = (args, values(kwargs))

"""
Expand Down Expand Up @@ -245,39 +249,6 @@ macro code_lowered(ex0...)
end
end

macro time_imports(ex)
quote
try
Base.Threads.atomic_add!(Base.TIMING_IMPORTS, 1)
$(esc(ex))
finally
Base.Threads.atomic_sub!(Base.TIMING_IMPORTS, 1)
end
end
end

macro trace_compile(ex)
quote
try
ccall(:jl_force_trace_compile_timing_enable, Cvoid, ())
$(esc(ex))
finally
ccall(:jl_force_trace_compile_timing_disable, Cvoid, ())
end
end
end

macro trace_dispatch(ex)
quote
try
ccall(:jl_force_trace_dispatch_enable, Cvoid, ())
$(esc(ex))
finally
ccall(:jl_force_trace_dispatch_disable, Cvoid, ())
end
end
end

"""
@functionloc
Expand Down

0 comments on commit ab22f98

Please sign in to comment.