Skip to content

Instantly share code, notes, and snippets.

@KristofferC
Last active July 12, 2018 13:45
Show Gist options
  • Save KristofferC/a8a70f4bddafa068d19fd21d40c9015c to your computer and use it in GitHub Desktop.
Save KristofferC/a8a70f4bddafa068d19fd21d40c9015c to your computer and use it in GitHub Desktop.
import LibGit2
options_path = "src/options.h"
if !isfile(options_path)
error("current working dir must be the julia directory")
end
repo = LibGit2.GitRepo(".")
LibGit2.transact(repo) do repo
println("Welcome to the custom precompile statement builder")
r = Base.prompt("Do you want to run the one-recompilation version that gives slightly worse result (otherwise two recompilations are needed) [y/n]"; default = "y")
slow_version = (r == "n" || r == "N")
if slow_version
println("Recompiling julia to emit what functions it compile")
options = read(options_path, String)
write(options_path, replace(options, "//#define TRACE_COMPILE" => "#define TRACE_COMPILE"))
run(`make -j4`)
end
println("*************************************************************************************")
println("We will record all functions used by julia when it starts up")
println("Feel free to play around a bit in the REPL as well, exit the started julia when done")
println("*************************************************************************************")
sleep(1)
if slow_version
run(pipeline(`./julia`; stderr="__precompile.txt"))
else
run(pipeline(`./julia -e 'io = open("__precompile.txt", "w"); ccall(:jl_dump_compiles, Cvoid, (Ptr{Cvoid},), io.handle)' -i`))
end
precompiles = readlines("base/precompile.jl")
new_precompiles = String[]
for p in readlines("__precompile.txt")
if slow_version
if startswith(p, "precompile")
push!(new_precompiles, p)
end
else
s = split(p, '\t')[2]
push!(new_precompiles, string("precompile(", s[2:end-1], ")"))
end
end
rm("__precompile.txt")
# Two last lines in precompile.jl are end of modules.
# Insert before those
i = length(precompiles) - 3
precompiles_after = [precompiles[1:i]; new_precompiles; precompiles[i+1:end]]
write("base/precompile.jl", join(precompiles_after, '\n'))
if slow_version
write(options_path, replace(options, "#define TRACE_COMPILE" => "//#define TRACE_COMPILE"))
end
println("Precompile statements written, recompiling julia with the new statements!")
sleep(1)
run(`make -j4`)
println("Done!")
end
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment