# Taking advantage of pkgimages in 'scripts'

**URL:** <https://discourse.julialang.org/t/taking-advantage-of-pkgimages-in-scripts/93535>\
**Category:** Performance\
**Created:** [January 25, 2023, 9:01pm UTC](https://discourse.julialang.org/t/taking-advantage-of-pkgimages-in-scripts/93535 "2023-01-25T21:01:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 25, 2023, 9:01pm UTC](https://discourse.julialang.org/t/taking-advantage-of-pkgimages-in-scripts/93535/1 "2023-01-25T21:01:19Z")

</div>

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&nbsp;#8 program (Benchmarks Game)](https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-julia-8.html) 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?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 25, 2023, 10:27pm UTC](https://discourse.julialang.org/t/taking-advantage-of-pkgimages-in-scripts/93535/2 "2023-01-25T22:27:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 25, 2023, 10:47pm UTC](https://discourse.julialang.org/t/taking-advantage-of-pkgimages-in-scripts/93535/3 "2023-01-25T22:47:23Z")

</div>

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

```julia
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

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

```

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

```julia
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)](https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/nbody.html) julia would actually be the fastest language shown.
