# Reuse Compiled Code Between REPLs?

**URL:** https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175
**Category:** New to Julia
**Tags:** repl, precompilation
**Created:** [July 18, 2024, 7:44am UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175 "2024-07-18T07:44:42Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)
#### Post date: [July 18, 2024, 7:44am UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/1 "2024-07-18T07:44:42Z")

</div>

Hi all,

A newbie question: Can the first-time compilation time be avoided when running the same function in different REPL sessions? Specifically, if I execute a function in the terminal REPL and then want to run the same function in the VSCode REPL, can I somehow reuse the compiled code from the terminal REPL to avoid recompilation in the VSCode REPL?

Thanks!

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [July 18, 2024, 8:20am UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/2 "2024-07-18T08:20:48Z")

</div>

Well not easily. The only option to reuse compiled code is to create a sysimg e.g. with PackageCompiler.jl.

Note that _precompiled_ code is shared automatically between julia sessions (as long as you don’t mess with JULIA\_DEPOT).

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 18, 2024, 9:58am UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/3 "2024-07-18T09:58:11Z")

</div>

Some nuance here. First we have to clarify what “running the same function” means. Let’s say you ran `foo(1, "bar")`, so you expect to run `foo(1, "bar")` again in another REPL session and do the exact same thing. Thing is, `foo(1, "bar")` isn’t even guaranteed to do the same thing within the same session. The symbol `foo`’s behavior, even existence, depends on its parent module/namespace and the (package) environment. If either are different, then the answer is reasonably no. For a simple example, `MyModule.foo` can behave differently from `YourModule.foo`, even if the method’s expression is identical because of something as small as a global constant variable like `pi` having a different value.

For that reason, almost every way of caching compiled code for the runtime involves a package, which has a module and an environment. If an importable unit in an active Julia process isn’t exactly what you’re going for, PackageCompiler.jl and JuliaScript.jl repurpose the package system. As an exception, StaticCompiler.jl can compile an executable from Julia (subset) function that doesn’t rely on the runtime; it’s not explicitly tied to a module or environment but the code behavior still implicitly does.

---

<div class="post-metadata">

### Author: ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)
#### Post date: [July 18, 2024, 11:38am UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/4 "2024-07-18T11:38:37Z")

</div>

Maybe what you are after is RemoteREPL.jl ? This way you can access the full julia kernel of a different process.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [July 18, 2024, 12:07pm UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/5 "2024-07-18T12:07:30Z")

</div>

Relevant discussion on Github from a few hours ago:

> <https://github.com/JuliaLang/julia/issues/55161>
>
> There should be a command line parameter called \`--append-precompiles\` or \`save-…compile\` or something like this which enables to save all precompiles which happened during runtime (either in parallel at runtime or when the program finishes). The precompiles should probably be saved in the module image where the precompile was triggered from (the caller of the function).
> 
> This way, the precompilation image file contains both the information from the precompile pass and from everything which came up during runtime (possibly the result of multiple runs if new calls come up in different runs). If the module is changed, all precompiles would get invalidated as usual. Afterwards, the number of precompiles grows first with the first precompile run and then possibly with each run (although most modules will not grow anymore at all after precompilation or only for the first run).
> 
> I am not sure how the REPL comes into place for this features. Until there are good ideas it might be best to not have this feature (saving new precompiles) in an interactive session.
> 
> With this mechanism in place I could even imagine that the regular precompile run might not even be intended for a lot of use cases. Just run the code and whenever some method is called the first time, precompile it, save the precompilation and go on. That means after a code change the functions are precompiled iff they are actually used, i.e. no currently unused function is precompiled and no function is precompiled more than one (even when accounting for multiple program runs).

> [@filchristou](#):
>
> Maybe what you are after is [RemoteREPL.jl](https://juliahub.com/ui/Packages/General/RemoteREPL) ?

There’s also DaemonMode.jl (no idea if it’s still functional, no updates for a few years).

---

<div class="post-metadata">

### Author: ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)
#### Post date: [July 18, 2024, 7:10pm UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/6 "2024-07-18T19:10:11Z")

</div>

Thanks! It sounds like what I had in mind; from the docs of PackageCompiler.jl:

> You can save loaded packages and compiled functions into a file (called a [sysimage](https://julialang.github.io/PackageCompiler.jl/stable/sysimages.html#sysimages)) that you pass to `julia` upon startup. Typically the goal is to reduce latency on your machine

I will give it a try and come back. By the way, why did you mention that my original question was not easily achievable? Have you already tried the package?

---

<div class="post-metadata">

### Author: ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)
#### Post date: [July 18, 2024, 7:16pm UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/7 "2024-07-18T19:16:09Z")

</div>

Thanks, but I think I am not getting completely your point; I might need extra knowledge on this.

> Thing is, `foo(1, "bar")` isn’t even guaranteed to do the same thing within the same session.

With the above, do you mean different behavior **and** outcome after applying the same `foo(1, "bar")` function in different REPL sessions? Or just different behavior? I guess if the function behaves differently but achieves the same, does not matter that much for my purposes.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [July 18, 2024, 7:31pm UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/8 "2024-07-18T19:31:54Z")

</div>

> [@Gast](#):
>
> By the way, why did you mention that my original question was not easily achievable?

I guess by “not easily” I mean “not out of the box”. Using PackageCompiler.jl requires some amount of effort.  
Tbh I never used it myself but I think it shouldn’t be too hard to create a custom sysimg - though the compilation takes a bit of time I’ve heard. And you’ll need to recreate the sysimg everytime you want to have different functions.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [July 18, 2024, 7:43pm UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/9 "2024-07-18T19:43:39Z")

</div>

If the same things are needed repeatedly, you can create custom [Startup packages](https://julialang.github.io/PrecompileTools.jl/stable/#Tutorial:-local-%22Startup%22-packages).

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 18, 2024, 10:03pm UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/10 "2024-07-18T22:03:33Z")

</div>

> [@Gast](#):
>
> do you mean different behavior **and** outcome

I meant either, but in this case you should care about different behavior alone because that rules out reusing compiled code.

---

<div class="post-metadata">

### Author: ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)
#### Post date: [July 19, 2024, 6:56am UTC](https://discourse.julialang.org/t/reuse-compiled-code-between-repls/117175/11 "2024-07-19T06:56:31Z")

</div>

Thanks, I will try this! I didn’t know I could create custom Startup packages with PrecompileTools.jl.
