Skip to content

Commit

Permalink
move macro to InteractiveUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Jul 19, 2021
1 parent 5ddf6de commit b6f3ec0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,6 @@ export
@timev,
@elapsed,
@allocated,
@time_imports,

# tasks
@sync,
Expand Down
42 changes: 0 additions & 42 deletions base/timing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,45 +371,3 @@ macro timed(ex)
(value=val, time=elapsedtime/1e9, bytes=diff.allocd, gctime=diff.total_time/1e9, gcstats=diff)
end
end

"""
@time_imports
A macro to execute an expression and produce a report of any time spent importing packages and their
dependencies.
If a package's dependencies have already been imported either globally or by another dependency they will
not appear under that package and the package will accurately report a faster load time than if it were to
be loaded in isolation.
```julia-repl
julia> @time_imports using CSV
3.5 ms ┌ IteratorInterfaceExtensions
27.4 ms ┌ TableTraits
614.0 ms ┌ SentinelArrays
138.6 ms ┌ Parsers
2.7 ms ┌ DataValueInterfaces
3.4 ms ┌ DataAPI
59.0 ms ┌ WeakRefStrings
35.4 ms ┌ Tables
49.5 ms ┌ PooledArrays
972.1 ms CSV
```
!!! note
During the load process a package sequentially imports where necessary all of its dependencies, not just
its direct dependencies. That is also true for the dependencies themselves so nested importing will likely
occur, but not always. Therefore the nesting shown in this output report is not equivalent to the dependency
tree, but does indicate where import time has accumulated.
"""
macro time_imports(ex)
quote
try
LOAD_TIMING[Threads.threadid()] += 1
$(esc(ex))
finally
LOAD_TIMING[Threads.threadid()] -= 1
end
end
end
1 change: 1 addition & 0 deletions stdlib/InteractiveUtils/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ InteractiveUtils.code_llvm
InteractiveUtils.@code_llvm
InteractiveUtils.code_native
InteractiveUtils.@code_native
InteractiveUtils.@time_imports
InteractiveUtils.clipboard
```
2 changes: 1 addition & 1 deletion stdlib/InteractiveUtils/src/InteractiveUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Base.Experimental.@optlevel 1

export apropos, edit, less, code_warntype, code_llvm, code_native, methodswith, varinfo,
versioninfo, subtypes, supertypes, @which, @edit, @less, @functionloc, @code_warntype,
@code_typed, @code_lowered, @code_llvm, @code_native, clipboard
@code_typed, @code_lowered, @code_llvm, @code_native, @time_imports, clipboard

import Base.Docs.apropos

Expand Down
44 changes: 44 additions & 0 deletions stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ macro code_lowered(ex0...)
end
end

macro time_imports(ex)
quote
try
Base.LOAD_TIMING[Threads.threadid()] += 1
$(esc(ex))
finally
Base.LOAD_TIMING[Threads.threadid()] -= 1
end
end
end

"""
@functionloc
Expand Down Expand Up @@ -332,3 +343,36 @@ Set the optional keyword argument `debuginfo` by putting it before the function
`debuginfo` may be one of `:source` (default) or `:none`, to specify the verbosity of code comments.
"""
:@code_native

"""
@time_imports
A macro to execute an expression and produce a report of any time spent importing packages and their
dependencies.
If a package's dependencies have already been imported either globally or by another dependency they will
not appear under that package and the package will accurately report a faster load time than if it were to
be loaded in isolation.
```julia-repl
julia> @time_imports using CSV
3.5 ms ┌ IteratorInterfaceExtensions
27.4 ms ┌ TableTraits
614.0 ms ┌ SentinelArrays
138.6 ms ┌ Parsers
2.7 ms ┌ DataValueInterfaces
3.4 ms ┌ DataAPI
59.0 ms ┌ WeakRefStrings
35.4 ms ┌ Tables
49.5 ms ┌ PooledArrays
972.1 ms CSV
```
!!! note
During the load process a package sequentially imports where necessary all of its dependencies, not just
its direct dependencies. That is also true for the dependencies themselves so nested importing will likely
occur, but not always. Therefore the nesting shown in this output report is not equivalent to the dependency
tree, but does indicate where import time has accumulated.
"""
:@time_imports

0 comments on commit b6f3ec0

Please sign in to comment.