Skip to content

Commit

Permalink
Remove duplicates from parcel (fixes #70) (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy authored Feb 25, 2020
1 parent f5f065e commit a1738ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/parcel_snoopi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function handle_kwbody(topmod::Module, m::Method, paramrepr, tt, fstr="fbody")
end

function parcel(tinf::AbstractVector{Tuple{Float64,Core.MethodInstance}}; subst=Vector{Pair{String, String}}(), blacklist=String[])
pc = Dict{Symbol, Vector{String}}() # output
pc = Dict{Symbol, Set{String}}() # output
modgens = Dict{Module, Vector{Method}}() # methods with generators in a module
mods = OrderedSet{Module}() # module of each parameter for a given method
for (t, mi) in reverse(tinf)
Expand All @@ -216,7 +216,7 @@ function parcel(tinf::AbstractVector{Tuple{Float64,Core.MethodInstance}}; subst=
# If we haven't yet started the list for this module, initialize
topmodname = nameof(topmod)
if !haskey(pc, topmodname)
pc[topmodname] = String[]
pc[topmodname] = Set{String}()
# For testing our precompile directives, we might need to have lookup available
if VERSION >= v"1.4.0-DEV.215" && topmod !== Core && !isdefined(topmod, :__bodyfunction__)
Core.eval(topmod, lookup_kwbody_ex)
Expand Down Expand Up @@ -298,7 +298,7 @@ function parcel(tinf::AbstractVector{Tuple{Float64,Core.MethodInstance}}; subst=
add_if_evals!(pc[topmodname], topmod, reprcontext(topmod, p), paramrepr, tt)
end
end
return pc
return Dict(mod=>collect(lines) for (mod, lines) in pc)
end

"""
Expand All @@ -312,7 +312,7 @@ pcI = ["good","bad","hi","bye","no"]
SnoopCompile.blacklist_remover!(pcI, blacklist)
```
"""
function blacklist_remover!(pcI, blacklist)
function blacklist_remover!(pcI::AbstractVector, blacklist)
idx = Int[]
for (iLine, line) in enumerate(pcI)
if any(occursin.(blacklist, line))
Expand All @@ -322,3 +322,14 @@ function blacklist_remover!(pcI, blacklist)
deleteat!(pcI, idx)
return pcI
end

function blacklist_remover!(pcI::AbstractSet, blacklist)
# We can't just use `setdiff!` because this is a substring search
todelete = Set{eltype(pcI)}()
for line in pcI
if any(occursin.(blacklist, line))
push!(todelete, line)
end
end
return setdiff!(pcI, todelete)
end
14 changes: 14 additions & 0 deletions test/snoopi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,17 @@ end
dct = Docs.meta(SnoopCompile)
@test haskey(dct, Docs.Binding(SnoopCompile, Symbol("@snoopi")))
end

@testset "Duplicates (#70)" begin
tinf = @snoopi begin
function eval_local_function(i)
@eval generated() = $i
return Base.invokelatest(generated)
end
eval_local_function(1)
eval_local_function(2)
eval_local_function(3)
end
pc = SnoopCompile.parcel(tinf)
@test count(isequal("precompile(Tuple{typeof(generated)})"), pc[:Main]) == 1
end

0 comments on commit a1738ae

Please sign in to comment.