Skip to content

Commit 243bded

Browse files
Add a docs section about loading/precomp/ttfx time tuning (#55569)
1 parent 2ee6551 commit 243bded

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

doc/src/manual/performance-tips.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,125 @@ Prominent examples include [MKL.jl](https://github.com/JuliaLinearAlgebra/MKL.jl
13941394
These are external packages, so we will not discuss them in detail here.
13951395
Please refer to their respective documentations (especially because they have different behaviors than OpenBLAS with respect to multithreading).
13961396

1397+
## Execution latency, package loading and package precompiling time
1398+
1399+
### Reducing time to first plot etc.
1400+
1401+
The first time a julia method is called it (and any methods it calls, or ones that can be statically determined) will be
1402+
compiled. The [`@time`](@ref) macro family illustrates this.
1403+
1404+
```
1405+
julia> foo() = rand(2,2) * rand(2,2)
1406+
foo (generic function with 1 method)
1407+
1408+
julia> @time @eval foo();
1409+
0.252395 seconds (1.12 M allocations: 56.178 MiB, 2.93% gc time, 98.12% compilation time)
1410+
1411+
julia> @time @eval foo();
1412+
0.000156 seconds (63 allocations: 2.453 KiB)
1413+
```
1414+
1415+
Note that `@time @eval` is better for measuring compilation time because without [`@eval`](@ref), some compilation may
1416+
already be done before timing starts.
1417+
1418+
When developing a package, you may be able to improve the experience of your users with *precompilation*
1419+
so that when they use the package, the code they use is already compiled. To precompile package code effectively, it's
1420+
recommended to use [`PrecompileTools.jl`](https://julialang.github.io/PrecompileTools.jl/stable/) to run a
1421+
"precompile workload" during precompilation time that is representative of typical package usage, which will cache the
1422+
native compiled code into the package `pkgimage` cache, greatly reducing "time to first execution" (often referred to as
1423+
TTFX) for such usage.
1424+
1425+
Note that [`PrecompileTools.jl`](https://julialang.github.io/PrecompileTools.jl/stable/) workloads can be
1426+
disabled and sometimes configured via Preferences if you do not want to spend the extra time precompiling, which
1427+
may be the case during development of a package.
1428+
1429+
### Reducing package loading time
1430+
1431+
Keeping the time taken to load the package down is usually helpful.
1432+
General good practice for package developers includes:
1433+
1434+
1. Reduce your dependencies to those you really need. Consider using [package extensions](@ref) to support interoperability with other packages without bloating your essential dependencies.
1435+
3. Avoid use of [`__init__()`](@ref) functions unless there is no alternative, especially those which might trigger a lot
1436+
of compilation, or just take a long time to execute.
1437+
4. Where possible, fix [invalidations](https://julialang.org/blog/2020/08/invalidations/) among your dependencies and from your package code.
1438+
1439+
The tool [`@time_imports`](@ref) can be useful in the REPL to review the above factors.
1440+
1441+
```julia-repl
1442+
julia> @time @time_imports using Plots
1443+
0.5 ms Printf
1444+
16.4 ms Dates
1445+
0.7 ms Statistics
1446+
┌ 23.8 ms SuiteSparse_jll.__init__() 86.11% compilation time (100% recompilation)
1447+
90.1 ms SuiteSparse_jll 91.57% compilation time (82% recompilation)
1448+
0.9 ms Serialization
1449+
┌ 39.8 ms SparseArrays.CHOLMOD.__init__() 99.47% compilation time (100% recompilation)
1450+
166.9 ms SparseArrays 23.74% compilation time (100% recompilation)
1451+
0.4 ms Statistics → SparseArraysExt
1452+
0.5 ms TOML
1453+
8.0 ms Preferences
1454+
0.3 ms PrecompileTools
1455+
0.2 ms Reexport
1456+
... many deps omitted for example ...
1457+
1.4 ms Tar
1458+
┌ 73.8 ms p7zip_jll.__init__() 99.93% compilation time (100% recompilation)
1459+
79.4 ms p7zip_jll 92.91% compilation time (100% recompilation)
1460+
┌ 27.7 ms GR.GRPreferences.__init__() 99.77% compilation time (100% recompilation)
1461+
43.0 ms GR 64.26% compilation time (100% recompilation)
1462+
┌ 2.1 ms Plots.__init__() 91.80% compilation time (100% recompilation)
1463+
300.9 ms Plots 0.65% compilation time (100% recompilation)
1464+
1.795602 seconds (3.33 M allocations: 190.153 MiB, 7.91% gc time, 39.45% compilation time: 97% of which was recompilation)
1465+
1466+
```
1467+
1468+
Notice that in this example there are multiple packages loaded, some with `__init__()` functions, some of which cause
1469+
compilation of which some is recompilation. Recompilation is caused by earlier packages invalidating methods, then in
1470+
these cases when the following packages run their `__init__()` function some hit recompilation before the code can be run.
1471+
1472+
Further, note the `Statistics` extension `SparseArraysExt` has been activated because `SparseArrays` is in the dependency
1473+
tree. i.e. see `0.4 ms Statistics → SparseArraysExt`.
1474+
1475+
This report gives a good opportunity to review whether the cost of dependency load time is worth the functionality it brings.
1476+
Also the `Pkg` utility `why` can be used to report why a an indirect dependency exists.
1477+
1478+
```
1479+
(CustomPackage) pkg> why FFMPEG_jll
1480+
Plots → FFMPEG → FFMPEG_jll
1481+
Plots → GR → GR_jll → FFMPEG_jll
1482+
```
1483+
1484+
or to see the indirect dependencies that a package brings in, you can `pkg> rm` the package, see the deps that are removed
1485+
from the manifest, then revert the change with `pkg> undo`.
1486+
1487+
If loading time is dominated by slow `__init__()` methods having compilation, one verbose way to identify what is being
1488+
compiled is to use the julia args `--trace-compile=stderr --trace-compile-timing` which will report a [`precompile`](@ref)
1489+
statement each time a method is compiled, along with how long compilation took. For instance, the full setup would be:
1490+
1491+
```
1492+
$ julia --startup-file=no --trace-compile=stderr --trace-compile-timing
1493+
julia> @time @time_imports using CustomPackage
1494+
...
1495+
```
1496+
1497+
Note the `--startup-file=no` which helps isolate the test from packages you may have in your `startup.jl`.
1498+
1499+
More analysis of the reasons for recompilation can be achieved with the
1500+
[`SnoopCompile`](https://github.com/timholy/SnoopCompile.jl) package.
1501+
1502+
### Reducing precompilation time
1503+
1504+
If package precompilation is taking a long time, one option is to set the following internal and then precompile.
1505+
```
1506+
julia> Base.PRECOMPILE_TRACE_COMPILE[] = "stderr"
1507+
1508+
pkg> precompile
1509+
```
1510+
1511+
This has the effect of setting `--trace-compile=stderr --trace-compile-timing` in the precompilation processes themselves,
1512+
so will show which methods are precompiled and how long they took to precompile.
1513+
1514+
There are also profiling options such as [using the external profiler Tracy to profile the precompilation process](@ref Profiling-package-precompilation-with-Tracy).
1515+
13971516

13981517
## Miscellaneous
13991518

0 commit comments

Comments
 (0)