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.

(cherry picked from commit ab22f98)
  • Loading branch information
IanButterworth committed Oct 24, 2024
1 parent c00ba05 commit 7b3408e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
13 changes: 13 additions & 0 deletions base/timing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,16 @@ macro timed(ex)
)
end
end

# Exported, documented, and tested in InteractiveUtils
# here so it's possible to time 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
15 changes: 4 additions & 11 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

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

"""
Expand Down Expand Up @@ -240,17 +244,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

"""
@functionloc
Expand Down

0 comments on commit 7b3408e

Please sign in to comment.