Przejdź do treści
Biblioteka skilli
Programowanie · Analiza

precompiling-julia-package

/precompiling-julia-package

Mierzy czas ładowania i pierwszego wywołania pakietu Julia i dodaje przykładowe obciążenia PrecompileTools.jl, gdy opóźnienia są problemem.

Licencja
MIT
Ostatnia zmiana repozytorium
26.09.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
1
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
1
Przestarzałość

Brak sygnałów. Repozytorium zmienione 26.09.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Mierzy czas ładowania i pierwszego wywołania pakietu Julia i dodaje przykładowe obciążenia PrecompileTools.jl, gdy opóźnienia są problemem.

Oryginalny opis (EN)

Use when measured Julia package load or first-call latency is a problem, or when the user mentions precompilation, time-to-first-execution, time-to-first-load, slow package load, slow first call, startup latency, or PrecompileTools.jl. Measure on Julia 1.13 before adding a PrecompileTools.jl workload.

Jak działa

  1. Step 1: Measure before changing anything
  2. Step 2: Add a PrecompileTools workload
  3. Step 3: Fix invalidations when TTFL does not improve
  4. Step 4: Verify the improvement
  5. Optional: find exact methods with SnoopCompile.jl
  6. Pitfalls
Sekcje z nagłówków SKILL.md, w oryginale · 199 linii, 1 153 słów
Podgląd SKILL.md
---
name: precompiling-julia-package
description: Use when measured Julia package load or first-call latency is a problem, or when the user mentions precompilation, time-to-first-execution, time-to-first-load, slow package load, slow first call, startup latency, or PrecompileTools.jl. Measure on Julia 1.13 before adding a PrecompileTools.jl workload.
---

# Reducing Julia TTFX and TTFL with PrecompileTools.jl

Julia compiles methods on demand, so the first time a package is loaded
(**TTFL**, time to first load) or a function is called (**TTFX**, time to first
execution) can dominate interactive latency. `PrecompileTools.jl` lets a package
declare a small, representative workload that Julia runs during precompilation
and caches, so that work is paid once when the package is installed or updated
instead of every time a fresh process starts.

Treat latency thresholds as prompts to measure, not pass/fail rules. Julia 1.13
reduces package precompile time substantially compared with Julia 1.12, so
measure on the Julia version and hardware that matter to users. See the [Julia
1.13 release highlights](https://julialang.org/blog/2026/09/julia-1.13-highlights/).

Useful initial signals are:

- **TTFL ≥ 1 s** — `using MyPkg` is slow.
- **TTFX ≥ 3 s** — the first call to a function is slow.

A repeated first call in a latency-sensitive REPL or service can justify work
below those thresholds. A one-shot batch script that runs for hours may not
benefit from precompiling its own code.

## Step 1: Measure before changing anything

Never add a workload based on a guess. Record the current numbers first so you
can prove the change helped. Always use a clean, up-to-date precompile cache and
skip the user's startup file:

```sh
julia --project -e 'using Pkg; Pkg.precompile()'
```

### Measuring TTFL

Run the load several times in fresh processes and take the minimum — the first
run may just be cold disk cache:

```sh
for i in 1 2 3; do
  julia --startup-file=no --project -e 't=@elapsed using MyPkg; println(round(t, digits=3), " s")'
done
```

### Measuring TTFX

Time the **first** call of each representative function, in a fresh process.
Do not call the function before the timed call, or you will measure the already
compiled second call:

```sh
julia --startup-file=no --project -e '
using MyPkg
t = @elapsed MyPkg.foo(1, 2)
println("TTFX foo: ", round(t, digits=3), " s")
t = @elapsed MyPkg.bar([1.0, 2.0])
println("TTFX bar: ", round(t, digits=3), " s")
'
```

Each call in the same process can share compilation with earlier calls, so when
isolating one function, measure it alone in its own process.

To see *what* is being compiled, add `--trace-compile=stderr`; each line is a
method compiled at runtime and is a candidate for the workload:

```sh
julia --startup-file=no --project --trace-compile=stderr -e 'using MyPkg'
```

## Step 2: Add a PrecompileTools workload

`PrecompileTools.jl` must be a normal dependency of the package (add it to
`[deps]`). Put the workload in the package module, after the code it exercises:

```julia
module MyPkg

using PrecompileTools: @setup_workload, @compile_workload

# ... your package code ...

@setup_workload begin
    # Setup runs ONLY during precompilation, never at load time.
    # Build minimal but representative inputs here.
    x = [1.0, 2.0, 3.0]
    y = 2.0

    @compile_workload begin
        # Exercise the hot paths you want cached.
        result = compute(x, y)
        summarize(result)
    end
end

end # module
```

The workload is package code that runs automatically during precompilation
(`Pkg.precompile()`, or when the package is installed or updated), so
precompiling a package executes that code. Only precompile code you trust.

Key properties that make this work:

- `@setup_workload` wraps the whole block so it runs during precompilation and
  is skipped at runtime. It also catches errors, so a broken workload cannot
  make the package unloadable — but a silently failing workload also does
  nothing, so check that precompilation actually exercises your code.
- `@compile_workload` marks the methods hit inside it for caching (bytecode
  from Julia 1.9+, and native code via pkgimages on 1.9+).
- The cached work is keyed to the Julia version, dependency versions, and the
  package's own source, so it is recomputed automatically when any of those
  change. After editing the workload, re-measure.

### Choosing a good workload

The goal is to cover the methods a user will realistically hit, at the lowest
precompilation cost:

- Use the **real input types** callers use, not `Any` or toy types. Dispatch on
  concrete types is what gets compiled, so `foo(1, 2)` does nothing for a later
  `foo(1.0, 2.0)`.
- Cover the entry points and the functions they transitively call. Exercising a
  top-level API usually compiles most of its callees for free.
- Keep it small. Every line here slows `Pkg.precompile()` at install/update time
  and enlarges the package image. Precompile the top TTFX offenders, not
  everything.
- Avoid side effects: no writing files, starting servers, or mutating global
  state. The block may run in unusual environments (CI, read-only caches).
- If a function is expensive to actually run, call it on tiny inputs — you are
  paying for compilation, not for the numeric result.

## Step 3: Fix invalidations when TTFL does not improve

Sometimes load time is dominated not by missing precompiled code but by
**invalidations**: loading your package makes previously compiled methods from
other packages stale, so they must be recompiled. If TTFL stays high after
adding a workload, add `@recompile_invalidations` around your package code:

```julia
using PrecompileTools
PrecompileTools.@recompile_invalidations begin
    include("api.jl")
    include("backend.jl")
end
```

This detects methods invalidated by loading the package and re-precompiles them
during the package's own precompilation. It is most useful for packages that
extend or interact heavily with other packages' methods. Place it so it wraps
the code responsible for the invalidations.

## Step 4: Verify the improvement

Re-run the Step 1 measurements with a fresh cache:

```sh
julia --project -e 'using Pkg; Pkg.precompile()'
```

Report the before/after numbers and whether they meet the package's real latency
goal; do not treat the initial signals above as universal limits. If nothing improved, check that the
workload actually ran (a swallowed error in `@setup_workload` will leave you
with a no-op) and that the types you exercised match the types callers use.

## Optional: find exact methods with SnoopCompile.jl

When you cannot tell which methods are worth precompiling, `SnoopCompile.jl`
records the full inference and can emit code you can fold into a workload:

```julia
using SnoopCompile
tinf = @snoopi_deep begin
    MyPkg.foo(1, 2)
    MyPkg.bar([1.0, 2.0])
end
```

Use the results to discover hot methods, then write a hand-tuned
`@setup_workload` — a curated workload is usually smaller and faster to
precompile than the raw output. `@snoopr` similarly reports runtime
invalidations. Reach for these only when measurement plus `--trace-compile`
is not enough to locate the cost.

## Pitfalls

- **Over-precompiling.** A workload that pulls in a large dependency graph can
  make installation slow and fail CI precompile timeouts. Precompile the paths
  that actually show up in TTFL/TTFX measurements.
- **Stale numbers.** Cached precompilation is invalidated by any source edit,
  dependency change, or Julia upgrade. Re-measure after each change; do not
  trust a number from before an edit.
- **Testing only one type.** A workload on `Float64` inputs does not help a
  user whose first call uses `Int` or `Matrix{Float64}`.
- **Mistaking compilation for algorithm cost.** If a function is slow on the
  *second* call too, the problem is the algorithm, not TTFX. Precompilation
  will not fix it.

Wersje

W bazie nie ma innych skilli o nazwie precompiling-julia-package.

Migawka bazy z 13.11.2026 · wersja paczki 78c16256d700 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 95testing-julia-package
    Konfiguruje i uruchamia testy pakietów Julia, w tym zależności testowe w workspace, Pkg.test() i równoległe pliki testów.
  • 100developing-julia-package
    Wspiera pracę nad kodem istniejącego pakietu Julia: układ modułów i include, multiple dispatch, adnotacje i stabilność typów, formatowanie oraz docstringi.
  • 85julia-evaluation
    Uruchamia i testuje kod Julia w trwałej sesji REPL oraz przeładowuje zmiany bez restartu.
  • 100julia-pro
    Pisze i optymalizuje programy w Julii 1.10+, z wielokrotnym dyspozytorowaniem, zarządzaniem pakietami i kodem do obliczeń numerycznych.
  • 100replicant
    Uruchamia fragmenty kodu Julia w trwałej sesji REPLicant przez julia +rpc, bez kosztu startu Julii przy każdym wywołaniu.
  • 98julia-package-dev
    Pomaga wydać i utrzymać pakiet Julia: podbicie wersji, rejestrację w General, zakresy compat, dokumentację i CI.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.