Taking advantage of pkgimages in 'scripts'

There’s a lot of benchmark scripts out there right now where Julia is at a big disadvantage due to compiler overhead and such. Has anyone thought out an easy way that we can start taking advantage of pkgimages to reduce this compilation overhead in various benchmarks?

Having to bundle all of the user’s code into a dev’d package seems a little heavy. E.g. in the example of n-body Julia #8 program (Benchmarks Game) could we make this significantly faster by bundling the code into a pkgimage, and if so what would be the most elegant way to do that?

4 Likes

I asked this question two months ago in the #ttfx channel on Slack and it went unanswered :joy:

1 Like

It’d be nice if we had a way to build “scriptimages” which were just a wrapper around a pkgimage that could be invoked by julia from the command line in a similar way to a script.


For what it’s worth, I just tried taking the above linked script and shoved it in a little dev’d package, removed the interactive part and added

function main(N = parse(Int, ARGS[1]); bodies=bodies)
    @printf("%.9f\n", energy(bodies))
    nbody!(bodies, N)
    @printf("%.9f\n", energy(bodies))
end

using SnoopPrecompile

# This will let SnoopPrecompile AOT compile all the machine code we need to just call `main()` from a script.
@precompile_setup begin
    _bodies = copy(bodies)
    @precompile_all_calls begin
        push!(_bodies, init_sun(_bodies))
        main(1000; bodies=_bodies)
    end
end

The timings I got on my machine using the original script was

real    0m2.453s
user    0m2.343s
sys     0m0.698s

and now using the precompiled pkgimage to invoke main() I get

real    0m1.982s
user    0m2.069s
sys     0m0.697s

So almost a 20% speedup of the wall time. If they got a 20% wall time speedup in the benchmarks posted: n-body (Benchmarks Game) julia would actually be the fastest language shown.