# Save RuntimeGeneratedFunction or Expr to file for later use

**URL:** <https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775>\
**Category:** General Usage\
**Created:** [January 13, 2024, 3:44pm UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775 "2024-01-13T15:44:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejmeitz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejmeitz/32/45283_2.png) [@ejmeitz](https://discourse.julialang.org/u/ejmeitz)\
**Post date:** [January 13, 2024, 3:44pm UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/1 "2024-01-13T15:44:22Z")

</div>

I am using a library to generate a rather expensive function which is returned as a `RuntimeGeneratedFunction` that I am also able to convert into an `Expr`. I would like to avoid having to re-generate this function and was hoping that there was some way to save this function to a file that I can then load and call at a later time? There’s probably a way to generate this function at precompile, but then the precompile time will be large. Any ideas or other approaches would be great!

I tried just saving the function to a file with JLD2, but when you load the file the resulting object is not callable.

---

<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:** [January 15, 2024, 6:57am UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/2 "2024-01-15T06:57:52Z")

</div>

If the generation of the Expr is the expensive part, does it help to just save that Expr to file (using JLD2.jl)? Then in your code, the `@generated` would first just look for the jld2-file and load the expr from there to return or else generate it anew and save it for later.

---

<div class="post-metadata">

**Author:** ![ejmeitz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejmeitz/32/45283_2.png) [@ejmeitz](https://discourse.julialang.org/u/ejmeitz)\
**Post date:** [January 17, 2024, 2:16am UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/3 "2024-01-17T02:16:06Z")

</div>

Yeah I realized the `Expr` can be saved to JLD2. I’m not so familiar with how `@generated` is supposed to work. Are you suggesting something like:

```julia
@generated function(path)
     return load(path, "expr")
end

```

With this approach would the only thing happening at pre-compile time be the loading of the expression from the file? Do you see issues if I pre-compute the files and ship them in a package? I see [some other threads](https://discourse.julialang.org/t/how-to-build-functions-from-symbolic-expressions-with-symbolics-jl/78879/2) with world age issues but I do not completely understand that.

Could do I think as well. Not sure the pros/cons of each

```julia
function (path)
   return @RuntimeGeneratedFunction((load(path, "expr"))
end

```

---

<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:** [January 17, 2024, 6:27am UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/4 "2024-01-17T06:27:53Z")

</div>

> [@ejmeitz](#):
>
> ```julia
> @generated function(path)
> return load(path, "expr")
> end
> 
> ```

This is close to what I am suggesting but does not work in this form. You need to write it like this:

```julia
@generated function foo(arguments, the, function, takes)
    path = "path/to/savefile.jld2"
    isfile(path) && return JLD2.load(path)["expr"] # load return a Dict so need to use the key we used for saving
    # file not present, throw error or compute expr and save
end

```

From your snippet above it looks like there are many of these functions. Is this ghe case? I have some ideas to make life easier, then to copy/paste the code above a couple of times 🙂

I’ll also leave you the [manual section on generated functions](https://docs.julialang.org/en/v1/manual/metaprogramming/#Generated-functions)

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [January 17, 2024, 4:22pm UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/5 "2024-01-17T16:22:52Z")

</div>

> [@ejmeitz](#):
>
> I am using a library

Which library? If it happens to be `Symbolics.jl` you can save it to a file.

---

<div class="post-metadata">

**Author:** ![ejmeitz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejmeitz/32/45283_2.png) [@ejmeitz](https://discourse.julialang.org/u/ejmeitz)\
**Post date:** [January 18, 2024, 2:42am UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/6 "2024-01-18T02:42:49Z")

</div>

> [@abraemer](#):
>
> there are many of these functions

Yes there are multiple functions that I would like to save to a file. I was planning on saving them to the same JLD2 file if that is possible.

> [@Salmon](#):
>
> Which library?

I am using `FastDifferentiation.jl` so its similar to symbolics, but not quite. I did not see [this](https://docs.sciml.ai/Symbolics/stable/manual/io/) page of the `Symbolics.jl` docs until you mentioned this, maybe I’ll try and add something similar to `FastDifferntiation.jl`. It looks like it just converts the a `Symbol` to a string and then writes that to a file? Now that I see that it seems obvious and also kind of avoids having to used `@generated` I believe since I can just add the julia file to the package.

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [January 18, 2024, 5:02am UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/7 "2024-01-18T05:02:21Z")

</div>

essentially yes I think.  
if your expression is something that can be `eval`’d it should in principle work to just save this to a file and include the entire file next time. Note that if your expression is big enough this might still have some compilation overhead. (probably this can also be removed with precompilation)

It might be that my idea wont work though - I havent really needed to do this before

---

<div class="post-metadata">

**Author:** ![ejmeitz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejmeitz/32/45283_2.png) [@ejmeitz](https://discourse.julialang.org/u/ejmeitz)\
**Post date:** [January 18, 2024, 1:15pm UTC](https://discourse.julialang.org/t/save-runtimegeneratedfunction-or-expr-to-file-for-later-use/108775/8 "2024-01-18T13:15:51Z")

</div>

The compilation overhead will be much smaller than actually generating the derivatives will be is the hope. I’ll figure out what works for saving/loading and mark something here as a solution.
