# Weird interaction between generated function and inlining

**URL:** <https://discourse.julialang.org/t/weird-interaction-between-generated-function-and-inlining/96459>\
**Category:** Internals & Design\
**Created:** [March 22, 2023, 4:24pm UTC](https://discourse.julialang.org/t/weird-interaction-between-generated-function-and-inlining/96459 "2023-03-22T16:24:07Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [March 22, 2023, 4:24pm UTC](https://discourse.julialang.org/t/weird-interaction-between-generated-function-and-inlining/96459/1 "2023-03-22T16:24:07Z")

</div>

I am currently working on a way to memoise functions using fully typed containers (see [this discussion](https://discourse.julialang.org/t/type-stable-generic-memoisation/96237) for the general concept). I have a working [implementation](https://github.com/mhinsch/TypedMemo) (sorry, still very barebone ATM), that seems to largely do what it is supposed to do and has already noticeably improved the runtime of my [simulation](https://github.com/mhinsch/LoneParentsModel.jl).  
However, under specific conditions the call to the memoised function seems to break: The internal cache is generated with the correct types and the function runs and returns a value, but the cache does not get modified. Mystifyingly the problem disappears as soon as I wrap the function call into a closure (i.e. use `(x->myfun(x, v2, v3))(v1)` instead of `myfun(v1, v2, v3)` or mark it as `@noinline`.  
My suspicion is that the cache that is being created in the generating part of the generated function is somehow getting lost and not correctly stored in the global register of the package. I’m not sure, though, if this is a compiler bug or if what I’m trying to do is outside of the specifications for generated functions.  
I will try to find a MWE that reproduces the effect outside of my somewhat baroque simulation code, but maybe someone has some insights on this in the meantime.

---

_[View the full topic](https://discourse.julialang.org/t/weird-interaction-between-generated-function-and-inlining/96459)._
