Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Dec 31, 2022
1 parent 86b53f8 commit 3082d33
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 59 deletions.
2 changes: 2 additions & 0 deletions src/SnoopCompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ function __init__()
if isdefined(SnoopCompile, :runtime_inferencetime)
@require PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" include("visualizations.jl")
end

@require PettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" include("report_invalidations.jl")
end

end # module
67 changes: 8 additions & 59 deletions src/invalidations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,15 @@ else
end

"""
tabulated_invalidations(;
report_invalidations(;
job_name::String,
invalidations,
n_rows::Int = 10,
process_filename::Function = x -> x,
)
A NamedTuple, `(; table_data, header)`, which
contains a condensed form of invalidations in the form
of a matrix and a header, which can be easily tabulated.
Print a tabular summary of invalidations.
An `@info` message also prints some information, for example
if the table has been truncated (upon request).
Expand All @@ -277,66 +276,16 @@ invalidations = SnoopCompileCore.@snoopr begin
end;
import SnoopCompile
(; table_data, header) = SnoopCompile.tabulated_invalidations(;
using SnoopCompile
report_invalidations(;
job_name = "MyWork",
invalidations,
)
import PrettyTables
PrettyTables.pretty_table(
table_data;
header,
formatters = PrettyTables.ft_printf("%s", 2:2),
header_crayon = PrettyTables.crayon"yellow bold",
subheader_crayon = PrettyTables.crayon"green bold",
crop = :none,
alignment = [:l, :c, :c, :c],
)
```
Using `report_invalidations` requires that you first load the `PrettyTables.jl` package.
"""
function tabulated_invalidations(;
job_name::String,
invalidations,
n_rows::Int = 10,
process_filename::Function = x -> x,
)
trees = reverse(invalidation_trees(invalidations))
n_total_invalidations = length(uinvalidated(invalidations))
# TODO: Merge `@info` statement with one below
@info "Number of invalidations for $job_name: $n_total_invalidations"
invs_per_method = map(trees) do methinvs
countchildren(methinvs)
end
n_invs_total = length(invs_per_method)
truncated_invs = n_rows < n_invs_total
sum_invs = sum(invs_per_method)
invs_per_method = invs_per_method[1:min(n_rows, n_invs_total)]
trees = trees[1:min(n_rows, n_invs_total)]
trunc_msg = truncated_invs ? " (showing $n_rows) " : ""
@info "$(job_name): $n_invs_total total method invalidations$trunc_msg"
n_invalidations_percent = map(invs_per_method) do inv
inv_perc = inv / sum_invs
Int(round(inv_perc*100, digits = 0))
end
meth_name = map(trees) do inv
"$(inv.method.name)"
end
fileinfo = map(trees) do inv
"$(process_filename(string(inv.method.file))):$(inv.method.line)"
end
header = (
["<file name>:<line number>", "Method Name", "Invalidations", "Invalidations %"],
["", "", "Number", "(xᵢ/∑x)"],
)
table_data = hcat(
fileinfo,
meth_name,
invs_per_method,
n_invalidations_percent,
)
return (; table_data, header)
end
function report_invalidations end

"""
trees = invalidation_trees(list)
Expand Down
57 changes: 57 additions & 0 deletions src/report_invalidations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This file is loaded conditionally via @require if PrettyTables is loaded

import .PrettyTables

export report_invalidations

function report_invalidations(;
job_name::String,
invalidations,
n_rows::Int = 10,
process_filename::Function = x -> x,
)
trees = reverse(invalidation_trees(invalidations))
n_total_invalidations = length(uinvalidated(invalidations))
# TODO: Merge `@info` statement with one below
@info "Number of invalidations for $job_name: $n_total_invalidations"
invs_per_method = map(trees) do methinvs
countchildren(methinvs)
end
n_invs_total = length(invs_per_method)
truncated_invs = n_rows < n_invs_total
sum_invs = sum(invs_per_method)
invs_per_method = invs_per_method[1:min(n_rows, n_invs_total)]
trees = trees[1:min(n_rows, n_invs_total)]
trunc_msg = truncated_invs ? " (showing $n_rows) " : ""
@info "$(job_name): $n_invs_total total method invalidations$trunc_msg"
n_invalidations_percent = map(invs_per_method) do inv
inv_perc = inv / sum_invs
Int(round(inv_perc*100, digits = 0))
end
meth_name = map(trees) do inv
"$(inv.method.name)"
end
fileinfo = map(trees) do inv
"$(process_filename(string(inv.method.file))):$(inv.method.line)"
end
header = (
["<file name>:<line number>", "Method Name", "Invalidations", "Invalidations %"],
["", "", "Number", "(xᵢ/∑x)"],
)
table_data = hcat(
fileinfo,
meth_name,
invs_per_method,
n_invalidations_percent,
)

PrettyTables.pretty_table(
table_data;
header,
formatters = PrettyTables.ft_printf("%s", 2:2),
header_crayon = PrettyTables.crayon"yellow bold",
subheader_crayon = PrettyTables.crayon"green bold",
crop = :none,
alignment = [:l, :c, :c, :c],
)
end

0 comments on commit 3082d33

Please sign in to comment.