Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more precompile methods #130

Open
3 tasks
henry2004y opened this issue Jan 22, 2023 · 3 comments
Open
3 tasks

Add more precompile methods #130

henry2004y opened this issue Jan 22, 2023 · 3 comments

Comments

@henry2004y
Copy link
Owner

henry2004y commented Jan 22, 2023

Now we only have load in precompilation. We need more to take advantage of Julia 1.9.

  • readvariable
  • fillmesh
  • getxxx

Maybe first take a look at how other packages handle this.

@henry2004y
Copy link
Owner Author

henry2004y commented Jan 23, 2023

I still see the same issue as in #90: making readvariable into precompilation triggers extra 2 allocations. Why?

It is probably related to type instability! By adding readvariable(meta, "CellID") into precompilation I lost 8% of speed:

# Current no precompilation:
julia> @benchmark readvariable(meta, "CellID")
BenchmarkTools.Trial: 10000 samples with 9 evaluations.
 Range (min  max):  2.723 μs    7.401 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     2.853 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   2.938 μs ± 284.272 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

   ▃█▄                                                         
  ▂███▅▃▃▆▅▅▆▅▄▃▃▃▃▃▃▂▁▁▁▂▂▁▁▁▁▂▁▁▁▂▄▃▂▁▁▁▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  2.72 μs         Histogram: frequency by time         3.6 μs <

 Memory estimate: 320 bytes, allocs estimate: 9.

# Precompile `readvariable`:
BenchmarkTools.Trial: 10000 samples with 8 evaluations.
 Range (min  max):  2.932 μs    8.017 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     3.159 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   3.254 μs ± 381.799 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

    █▃  ▃                                                      
  ▇███▇▆█▆▅▅▄▄▃▅▅▃▄▄▃▃▃▃▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▁▂▁▂▂▂▂▂▂▂▂▂▂▁▁▂▁▂▂▂ ▃
  2.93 μs         Histogram: frequency by time         5.3 μs <

 Memory estimate: 464 bytes, allocs estimate: 11.
file = "test/init.vlsv"
meta = load(file)
# no precompilation
julia> @time Vlasiator.fillmesh(meta, ["proton/vg_v", "CellID"], skipghosttype=true, maxamronly=true);
  0.328785 seconds (405.75 k allocations: 27.188 MiB)

julia> @time Vlasiator.fillmesh(meta, ["proton/vg_v", "CellID"], skipghosttype=true, maxamronly=true);
  0.000142 seconds (69 allocations: 2.859 KiB)
# with precompilation
julia> @time Vlasiator.fillmesh(meta, ["proton/vg_v", "CellID"], skipghosttype=true, maxamronly=true);
  0.000564 seconds (607 allocations: 35.359 KiB)

julia> @time Vlasiator.fillmesh(meta, ["proton/vg_v", "CellID"], skipghosttype=true, maxamronly=true);
  0.000104 seconds (73 allocations: 3.141 KiB)

Related to #127

@henry2004y
Copy link
Owner Author

Checking invalidations

using SnoopCompileCore
invalidations = @snoopr using Vlasiator;
tinf = @snoopi_deep begin
   file = "bulk.0000006.vlsv"
   meta = load(file)
   v = readvariable(meta, "proton/vg_rho")
end
using SnoopCompile

trees = invalidation_trees(invalidations)
staletrees = precompile_blockers(trees, tinf)

@show length(uinvalidated(invalidations))  # show total invalidations

show(trees[end])  # show the most invalidating method

# Count number of children (number of invalidations per invalidated method)
n_invalidations = map(SnoopCompile.countchildren, trees)

#=
# (optional) plot the number of children per method invalidations
import Plots
Plots.plot(
    1:length(trees),
    n_invalidations;
    markershape=:circle,
    xlabel="i-th method invalidation",
    label="Number of children per method invalidations"
)
=#
# (optional) report invalidations summary
using PrettyTables  # needed for `report_invalidations` to be defined
SnoopCompile.report_invalidations(;
     invalidations,
     process_filename = x -> last(split(x, ".julia/packages/")),
     n_rows = 0,  # no-limit (show all invalidations)
  )

[ Info: 160 methods invalidated for 18 functions

file name:line number Function Name Invalidations Invalidations %
StaticArrays/QPt9n/src/SizedArray.jl:88 convert 36 23
StaticArrays/QPt9n/src/indexing.jl:370 unsafe_view 36 23
EzXML/ZNwhK/src/node.jl:40 convert 15 10
EzXML/ZNwhK/src/streamreader.jl:54 convert 10 6
FillArrays/xqile/src/FillArrays.jl:688 print_matrix_row 9 6
LaTeXStrings/pJ7vn/src/LaTeXStrings.jl:109 lastindex 8 5
LaTeXStrings/pJ7vn/src/LaTeXStrings.jl:112 nextind 7 5
FillArrays/xqile/src/trues.jl:26 to_indices 6 4
EzXML/ZNwhK/src/node.jl:32 convert 5 3
EzXML/ZNwhK/src/node.jl:1363 IteratorSize 5 3
StaticArrays/QPt9n/src/broadcast.jl:27 _axes 5 3
LaTeXStrings/pJ7vn/src/LaTeXStrings.jl:125 sizeof 4 3
LaTeXStrings/pJ7vn/src/LaTeXStrings.jl:117 getindex 3 2
StaticArrays/QPt9n/src/broadcast.jl:30 instantiate 2 1
LaTeXStrings/pJ7vn/src/LaTeXStrings.jl:108 firstindex 1 1
FillArrays/xqile/src/FillArrays.jl:555 unique 1 1
EzXML/ZNwhK/src/document.jl:200 write 1 1
StaticArrays/QPt9n/src/SOneTo.jl:57 getproperty 1 1

None of these comes within the package. What shall I do?

@henry2004y
Copy link
Owner Author

JuliaLang/julia#49567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant