Created
July 30, 2017 21:10
-
-
Save randy3k/81313cb89fd7e6c568698d0cab20143f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro compileinfo(file_name, command) | |
quote | |
open($(esc(file_name)), "w") do io | |
ccall(:jl_dump_compiles, Void, (Ptr{Void},), io.handle) | |
try | |
$command | |
finally | |
close(io) | |
ccall(:jl_dump_compiles, Void, (Ptr{Void},), C_NULL) | |
end | |
end | |
end | |
end | |
function getcompileinfo(file_name) | |
raw_data = open(file_name) do f readstring(f) end; | |
data = vcat(map(split(raw_data, r"\n(?=[0-9]+)")) do x reshape(split(x, r"\t"),(1,2)) end...); | |
data[:, 2] = map(view(data, :, 2)) do x replace(x, r"^\"(.*)\"$", s"\1") end; # remove quotes | |
pc = Dict{Module, Vector{Tuple}}() | |
for k in 1:size(data, 1) | |
m, t, f, a = parse_call(data[k, :]) | |
if isa(m, Module) | |
if m ∉ keys(pc) | |
pc[m] = Tuple[] | |
end | |
push!(pc[m], (t, f, a)) | |
end | |
end | |
pc | |
end | |
function parse_call(c) | |
ast, _ = parse(c[2], 1, greedy=true, raise=false) | |
if ast.head == :call | |
f, args = ast.args[1], ast.args[2:end] | |
if isa(f, Expr) && f.head == :. | |
try | |
truef = eval(Main, f) | |
m = eval(Main, Symbol(replace(string(truef), r"^([^.]*).*$", s"\1"))) | |
if isa(m, Module) | |
return m, parse(Int, c[1]), truef, eval(Main, Expr(:tuple, args[2:end]...)) | |
end | |
catch | |
println("Error in parsing entry: $f") | |
end | |
end | |
end | |
return nothing, nothing, nothing, nothing | |
end | |
function writefile(prefix::AbstractString, pc::Dict) | |
if !isdir(prefix) | |
mkpath(prefix) | |
end | |
for (k, v) in pc | |
open(joinpath(prefix, "precompile_$(lowercase(string(k))).jl"), "w") do io | |
println(io, "function _precompile_()") | |
println(io, " ccall(:jl_generating_output, Cint, ()) == 1 || return nothing") | |
for f in v | |
println(io, " precompile($(f[2]), $(f[3]))") | |
end | |
println(io, "end") | |
end | |
end | |
end | |
file_name = tempname() | |
using BandedMatrices | |
@compileinfo file_name begin | |
include(Pkg.dir("BandedMatrices", "test","runtests.jl")) | |
end | |
pc = getcompileinfo(file_name) | |
writefile("BandedMatrices", pc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment