# Expensive Code Generation for Functions for Reuse

**URL:** <https://discourse.julialang.org/t/expensive-code-generation-for-functions-for-reuse/117899>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [August 6, 2024, 4:03pm UTC](https://discourse.julialang.org/t/expensive-code-generation-for-functions-for-reuse/117899 "2024-08-06T16:03:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_L](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ian_l/32/49509_2.png) [@Ian\_L](https://discourse.julialang.org/u/Ian_L)\
**Post date:** [August 6, 2024, 4:03pm UTC](https://discourse.julialang.org/t/expensive-code-generation-for-functions-for-reuse/117899/1 "2024-08-06T16:03:27Z")

</div>

Hello. I’m looking for some guidance on a design decision to make. I have an algorithm which makes use of 10 to 20 specialized function that are called repeatedly, however, none of the functions are known until the necessary info is supplied at runtime. One strategy is to generate the code and cache the functions for later which is what the existing codebase does. Generating the AST for the function requires running expensive steps (i.e. graph algorithms), so I am unsure if there is even an alternative route besides eval-ing and storing the functions for reuse. Moreover I don’t think I can take a memoization route since the procedures are inherently stochastic. I have a couple of questions

- In similar scenarios where the functions ought to be stored somehow, what do people usually do? Do you do as I explained above, or something better? Are there example packages that run into similar design choices?
- Would generated functions work here? I also do not wish to pollute the namespace, so it seems I need anonymous functions. Is `eval` the go-to here? `eval`ing these functions sounds like a type unstable mess 😅.
- How could I make sensible stack traces for the user if the code is generated?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 6, 2024, 4:29pm UTC](https://discourse.julialang.org/t/expensive-code-generation-for-functions-for-reuse/117899/2 "2024-08-06T16:29:41Z")

</div>

Can you give an example of what the functions you need actually look like? Are you sure there’s no way a normal `function` can’t handle it? Seeing examples will probably help.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 6, 2024, 4:53pm UTC](https://discourse.julialang.org/t/expensive-code-generation-for-functions-for-reuse/117899/3 "2024-08-06T16:53:33Z")

</div>

> [@Ian\_L](#):
>
> I have an algorithm which makes use of 10 to 20 specialized function that are called repeatedly, however, none of the functions are known until the necessary info is supplied at runtime.

Pass the functions as arguments to your algorithm?

If you’re passing 10–20 functions to your algorithm all at once, maybe those should come from a type rather than a single argument? e.g. if you have an argument `x::T`, then you can functions `foo(x, …)` that the caller can overload for a type `T` to do different things.

It’s hard to say more without knowing more specifics.

---

<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:** [August 6, 2024, 10:37pm UTC](https://discourse.julialang.org/t/expensive-code-generation-for-functions-for-reuse/117899/4 "2024-08-06T22:37:08Z")

</div>

> [@Ian\_L](#):
>
> Would generated functions work here? I also do not wish to pollute the namespace, so it seems I need anonymous functions. Is `eval` the go-to here? `eval`ing these functions sounds like a type unstable mess 😅.

It’s impossible to give advice specific to your application, given that you didn’t provide code, but the general rule of “avoid metaprogramming” is always valid. In Julia it’s possible to move values into the type domain to achieve compilation at run time, so metaprogramming is only rarely necessary or desirable.
