Skip to content

Commit 3c8cfe9

Browse files
committed
Add option to make write more git diff-friendly
`SnoopCompile.write(filename, data; suppress_time=true)` now omits the timing information. Closes #314.
1 parent 98d42a8 commit 3c8cfe9

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SnoopCompile"
22
uuid = "aa65fe97-06da-5843-b5b1-d5d13cad87d2"
33
author = ["Tim Holy <tim.holy@gmail.com>", "Shuhei Kadowaki <aviatesk@gmail.com>"]
4-
version = "3.0.3"
4+
version = "3.1.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/utils.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let known_type_cache = IdDict{Tuple{Module,Tuple{Vararg{Symbol}},Symbol},Bool}()
6161
end
6262
end
6363

64-
function add_repr!(list, modgens::Dict{Module, Vector{Method}}, mi::MethodInstance, topmod::Module=mi.def.module; check_eval::Bool, time=nothing, kwargs...)
64+
function add_repr!(list, modgens::Dict{Module, Vector{Method}}, mi::MethodInstance, topmod::Module=mi.def.module; check_eval::Bool, time=nothing, suppress_time::Bool=false, kwargs...)
6565
# Create the string representation of the signature
6666
# Use special care with keyword functions, anonymous functions
6767
tt = Base.unwrap_unionall(mi.specTypes)
@@ -83,9 +83,9 @@ function add_repr!(list, modgens::Dict{Module, Vector{Method}}, mi::MethodInstan
8383
# Keyword function
8484
fname = mkw.captures[1] === nothing ? mkw.captures[2] : mkw.captures[1]
8585
fkw = "Core.kwftype(typeof($fname))"
86-
return add_if_evals!(list, topmod, fkw, paramrepr, tt; check_eval=check_eval, time=time)
86+
return add_if_evals!(list, topmod, fkw, paramrepr, tt; check_eval, time, suppress_time)
8787
elseif mkwbody !== nothing
88-
ret = handle_kwbody(topmod, m, paramrepr, tt; check_eval = check_eval, kwargs...)
88+
ret = handle_kwbody(topmod, m, paramrepr, tt; check_eval, kwargs...)
8989
if ret !== nothing
9090
push!(list, append_time(ret, time))
9191
return true
@@ -107,7 +107,7 @@ function add_repr!(list, modgens::Dict{Module, Vector{Method}}, mi::MethodInstan
107107
mkwc = match(kwbodyrex, cname)
108108
if mkwc === nothing
109109
getgen = "typeof(which($(caller.name),$csigstr).generator.gen)"
110-
return add_if_evals!(list, topmod, getgen, paramrepr, tt; check_eval=check_eval, time=time)
110+
return add_if_evals!(list, topmod, getgen, paramrepr, tt; check_eval, time, suppress_time)
111111
else
112112
getgen = "which(Core.kwfunc($(mkwc.captures[1])),$csigstr).generator.gen"
113113
ret = handle_kwbody(topmod, caller, cparamrepr, tt; check_eval = check_eval, kwargs...) #, getgen)
@@ -123,9 +123,9 @@ function add_repr!(list, modgens::Dict{Module, Vector{Method}}, mi::MethodInstan
123123
# Anonymous function, wrap in an `isdefined`
124124
prefix = "isdefined($mmod, Symbol(\"$mname\")) && "
125125
fstr = "getfield($mmod, Symbol(\"$mname\"))" # this is universal, var is Julia 1.3+
126-
return add_if_evals!(list, topmod, fstr, paramrepr, tt; prefix=prefix, check_eval = check_eval, time=time)
126+
return add_if_evals!(list, topmod, fstr, paramrepr, tt; prefix, check_eval, time, suppress_time)
127127
end
128-
return add_if_evals!(list, topmod, reprcontext(topmod, p), paramrepr, tt, check_eval = check_eval, time=time)
128+
return add_if_evals!(list, topmod, reprcontext(topmod, p), paramrepr, tt; check_eval, time, suppress_time)
129129
end
130130

131131
function handle_kwbody(topmod::Module, m::Method, paramrepr, tt, fstr="fbody"; check_eval = true)
@@ -195,11 +195,15 @@ Adds the precompilation statements only if they can be evaled. It uses [`can_eva
195195
196196
In some cases, you may want to bypass this function by passing `check_eval=true` to increase the snooping performance.
197197
"""
198-
function add_if_evals!(pclist, mod::Module, fstr, params, tt; prefix = "", check_eval::Bool=true, time=nothing)
198+
function add_if_evals!(pclist, mod::Module, fstr, params, tt; prefix = "", check_eval::Bool=true, time=nothing, suppress_time::Bool=false)
199199
ttstr = tupletypestring(fstr, params)
200200
can, exc = can_eval(mod, ttstr, check_eval)
201201
if can
202-
push!(pclist, append_time(prefix*wrap_precompile(ttstr), time))
202+
str = prefix*wrap_precompile(ttstr)
203+
if !suppress_time
204+
str = append_time(str, time)
205+
end
206+
push!(pclist, str)
203207
return true
204208
else
205209
@debug "Module $mod: skipping $tt due to eval failure" exception=exc _module=mod _file="precompile_$mod.jl"

src/write.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ function write(filename::AbstractString, pc::Vector; kwargs...)
3232
end
3333
end
3434

35-
"""
36-
write(prefix::AbstractString, pc::Dict; always::Bool = false)
37-
38-
Write each modules' precompiles to a separate file. If `always` is
39-
true, the generated function will always run the precompile statements
40-
when called, otherwise the statements will only be called during
41-
package precompilation.
42-
"""
4335
function write(prefix::AbstractString, pc::Dict; always::Bool = false)
4436
if !isdir(prefix)
4537
mkpath(prefix)
@@ -59,3 +51,18 @@ function write(prefix::AbstractString, pc::Dict; always::Bool = false)
5951
end
6052
end
6153
end
54+
55+
@doc """
56+
write(prefix::AbstractString, pc; always::Bool=false, suppress_time::Bool=false)
57+
58+
Write each modules' precompiles to a separate file. If `always` is true, the
59+
generated function will always run the precompile statements when called,
60+
otherwise the statements will only be called during package precompilation.
61+
62+
When writing results from parceling `@snoop_inference`, by default SnoopCompile
63+
appends the time taken to precompile each statement to the generated file. If
64+
`suppress_time` is true, this information will be omitted.
65+
66+
!!! compat "SnoopCompile 3.1"
67+
The `suppress_time` keyword argument was added in SnoopCompile 3.1.
68+
""" write

test/snoop_inference.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,12 @@ include("testmodules/SnoopBench.jl")
748748
str = String(take!(io))
749749
@test occursin(r"typeof\(mappushes\),Any,Vector\{A\}", str)
750750
@test occursin(r"typeof\(mappushes!\),typeof\(identity\),Vector\{Any\},Vector\{A\}", str)
751+
@test occursin(r"# time: \d", str)
752+
SnoopCompile.write(io, tmis; tmin=0.0, suppress_time=true)
753+
str = String(take!(io))
754+
@test occursin(r"typeof\(mappushes\),Any,Vector\{A\}", str)
755+
@test occursin(r"typeof\(mappushes!\),typeof\(identity\),Vector\{Any\},Vector\{A\}", str)
756+
@test !occursin(r"# time: \d", str)
751757

752758
list = Any[1, 1.0, Float16(1.0), a]
753759
tinf = @snoop_inference SnoopBench.mappushes(isequal(Int8(1)), list)

0 commit comments

Comments
 (0)