Skip to content

Commit 95a3b41

Browse files
ChrisRackauckastimholyranocha
authored
Add a copy-paste invalidations analysis script (#326)
This is oriented at giving people a "recipe" for tracking down invalidations. Co-authored-by: Tim Holy <tim.holy@gmail.com> Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
1 parent ee19331 commit 95a3b41

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

docs/src/tutorial.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ Certain concepts and types will appear repeatedly, so it's worth
44
spending a little time to familiarize yourself at the outset.
55
You can find a more expansive version of this page in [this blog post](https://julialang.org/blog/2021/01/precompile_tutorial/).
66

7+
## Cut to the Chase: A copy-paste analysis of invalidations
8+
9+
The following is a quick "grab and go" script for analyzing invalidations.
10+
Insert package loads (`using` or `import` statements) and/or method definitions into the `@snoopr` block,
11+
and put the workload you want to be fast in the `@snoopi_deep` block.
12+
The resulting plot shows the distribution of the invalidations sorted by the number of children affected.
13+
Generally, invalidations with many children matter more than those
14+
with few children, and thus this shows how many "bad actors" need to be investigated. `show(trees[end])` show the method which leads to the most
15+
invalidations, with `show(trees[end-1])` being the second most, and so forth.
16+
While the plot shows total invalidations (`trees`), only the ones in `staletrees` affect the workload in `@snoopi_deep`.
17+
18+
```julia
19+
using SnoopCompileCore
20+
invalidations = @snoopr using PkgA, PkgB;
21+
tinf = @snoopi_deep begin
22+
some_workload()
23+
end
24+
using SnoopCompile
25+
trees = invalidation_trees(invalidations)
26+
staletrees = precompile_blockers(trees, tinf)
27+
28+
@show length(SnoopCompile.uinvalidated(invalidations)) # show total invalidations
29+
30+
show(trees[end]) # show the most invalidating method
31+
32+
# Count number of children (number of invalidations per invalidated method)
33+
n_invalidations = map(SnoopCompile.countchildren, trees)
34+
35+
import Plots
36+
Plots.plot(
37+
1:length(trees),
38+
n_invalidations;
39+
markershape=:circle,
40+
xlabel="i-th method invalidation",
41+
label="Number of children per method invalidations"
42+
)
43+
```
44+
745
## `MethodInstance`s, type-inference, and backedges
846

947
Our first goal is to understand how code connects together.

0 commit comments

Comments
 (0)