# Function pointers & signatures

**URL:** https://discourse.julialang.org/t/function-pointers-signatures/106114
**Category:** Internals & Design
**Created:** [November 12, 2023, 3:18pm UTC](https://discourse.julialang.org/t/function-pointers-signatures/106114 "2023-11-12T15:18:56Z")
**Posts on this page:** 1
**Showing post:** 3

<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: [November 12, 2023, 3:51pm UTC](https://discourse.julialang.org/t/function-pointers-signatures/106114/3 "2023-11-12T15:51:26Z")

</div>

> [@f.ij](#):
>
> as far as my understanding goes, I could make s.algorithm a function pointer with a fixed signature, and just swap them out during runtime, without any performance hit.

More accurately, providing a fixed signature potentially removes the need for runtime dispatch of calling the varying functions. However, that doesn’t mean it’s as performant as compiling for specific functions. The lack of inlining has an inherent runtime cost for the call, and it also prevents various optimizations at compile-time.

> [@f.ij](#):
>
> Right now in julia, since functions are of a singleton type, my code is type instable and thus slow. The compiler doesn’t know anything (?) about the type of function that could be found in the field `algorithm` so I get generic code which is slow.

Sort of. If the runtime dispatch dominates the runtime, then you’re seeing a large hit. If the various compiled functions take much longer, then the runtime dispatch is negligible. Julia compiles per call signature, so [“function barriers”](https://docs.julialang.org/en/v1/manual/performance-tips/#kernel-functions) and type assertions (not necessarily fixed ones, can be generic and known at compile-time) can minimize the type-instability and runtime dispatches, and isolate them from the efficient compiled code.

Still, there are many cases where it’s worth removing runtime dispatch by communicating more information. FunctionWrappers.jl lets you specify a call signature and contain various functions’ compiled code. Despite describing some important limitations, the documentation is still scant so I’m linking my [recent comment](https://discourse.julialang.org/t/function-inside-struct-allocates-when-referenced/105785/22) on it, the rest of the thread is worth reading too.

---

_[View the full topic](https://discourse.julialang.org/t/function-pointers-signatures/106114)._
