# Jl\_call - function call latency

**URL:** <https://discourse.julialang.org/t/jl-call-function-call-latency/106791>\
**Category:** Performance\
**Tags:** embedding\
**Created:** [November 27, 2023, 10:14am UTC](https://discourse.julialang.org/t/jl-call-function-call-latency/106791 "2023-11-27T10:14:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AntoineK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoinek/32/202471_2.png) [@AntoineK](https://discourse.julialang.org/u/AntoineK)\
**Post date:** [November 27, 2023, 10:14am UTC](https://discourse.julialang.org/t/jl-call-function-call-latency/106791/1 "2023-11-27T10:14:39Z")

</div>

Hello,  
My first benchmarks show that calling the jl\_call function comes with a latency of 100 nanoseconds (measured by calling an empty Julia function) on a Xeon Gold processor.  
Is it expected ?  
In Julia, BenchmarksTool gives a computation time of 50 ns for the julia function I intend to embed so paying an extra 100 ns in the C code is annoying. Note that my loop is a few microsecs so I am looking at any performance improvements.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 27, 2023, 5:12pm UTC](https://discourse.julialang.org/t/jl-call-function-call-latency/106791/2 "2023-11-27T17:12:27Z")

</div>

Have you tried calling and timing it twice to account for compilation latency?

---

<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:** [November 27, 2023, 5:16pm UTC](https://discourse.julialang.org/t/jl-call-function-call-latency/106791/3 "2023-11-27T17:16:53Z")

</div>

> [@AntoineK](#):
>
> My first benchmarks show that calling the jl\_call function comes with a latency

I wouldn’t use `jl_call` in a low-latency situation, since (I think?) that will still do dynamic dispatch. Instead, if possible you should simple ask Julia for a C function pointer with the specific type signature that you want to call. Then the latency will be the same as for any other C function pointer.

See the paragraph on `@cfunction` in the [Julia embedding manual](https://docs.julialang.org/en/v1/manual/embedding/#Calling-Julia-Functions).

I submitted a documentation PR to clarify this: [additional clarification on cfunction embedding by stevengj · Pull Request #52315 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/52315)

---

<div class="post-metadata">

**Author:** ![AntoineK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoinek/32/202471_2.png) [@AntoineK](https://discourse.julialang.org/u/AntoineK)\
**Post date:** [November 28, 2023, 9:43am UTC](https://discourse.julialang.org/t/jl-call-function-call-latency/106791/4 "2023-11-28T09:43:06Z")

</div>

Yes, the benchmark does a warmup call so that is does not include any timing due to JAOT compilation. The 100ns is the mean duration over a million jl\_calls.

---

<div class="post-metadata">

**Author:** ![AntoineK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoinek/32/202471_2.png) [@AntoineK](https://discourse.julialang.org/u/AntoineK)\
**Post date:** [November 28, 2023, 9:44am UTC](https://discourse.julialang.org/t/jl-call-function-call-latency/106791/5 "2023-11-28T09:44:29Z")

</div>

ok I will investigate the `@cfunction` solution. Thank you.

---

<div class="post-metadata">

**Author:** ![AntoineK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoinek/32/202471_2.png) [@AntoineK](https://discourse.julialang.org/u/AntoineK)\
**Post date:** [November 29, 2023, 3:59pm UTC](https://discourse.julialang.org/t/jl-call-function-call-latency/106791/6 "2023-11-29T15:59:06Z")

</div>

![jl_call_vs_cfunction](https://global.discourse-cdn.com/julialang/original/3X/b/1/b171a6dc8d7d4c7502ff799241284f5f04ad75af.png)

By using @cfunction I am getting better performance. Thanks for the tip. Had a bit of hard time finding the right way of passing Arrays without getting heap memory allocation. Solution found by using a combination of jl\_alloc\_array\_[1,2]d to allocate the arrays and a cfunction defined as follow:

```julia
jl_value_t *cfunc = jl_eval_string("@cfunction(gemv!, Cvoid, (Ref{Array{Cdouble,1}}, Ref{Array{Cdouble,2}}, Ref{Array{Cdouble,1}}))");

```

the “jl\_call” curve seems a bit suspicious. Will investigate why its behavior is different.
