# Why does this type instability lead to allocations?

**URL:** <https://discourse.julialang.org/t/why-does-this-type-instability-lead-to-allocations/92821>\
**Category:** Performance\
**Created:** [January 11, 2023, 7:39pm UTC](https://discourse.julialang.org/t/why-does-this-type-instability-lead-to-allocations/92821 "2023-01-11T19:39:54Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [January 13, 2023, 6:48pm UTC](https://discourse.julialang.org/t/why-does-this-type-instability-lead-to-allocations/92821/4 "2023-01-13T18:48:15Z")

</div>

Many thanks for your input, @cjdoris.

> [@cjdoris](#):
>
> My guess is that, even though the return type of `mysum(v)` is inferred (as `Float64`), it is still boxing the return value, and therefore still allocating. In principle, Julia could indeed compile versions of these methods to return `Float64` natively, but I guess that in the face of dynamic dispatch, Julia currently always boxes the result. Presumably this is an optimization that could be implemented.

Interesting. I felt confident that specifying the output of `mysumv()` would remove this boxing. If this is the source of the allocations, then as you say, it’s likely something that can be fixed. Can you not tell where boxing is occurring in the output of `@code_warntype`? I assumed it was on red lines of code, but the only red line in the for loop is at %7, which doesn’t relate to the output of `mysumv()`.

> [@cjdoris](#):
>
> To summarise, it appears to take ~10ns to box the intermediate results, ~60ns to do dynamic dispatch and ~20ns to do the actual work. Hence the allocations coming from boxing have a fairly negligible impact on run-time.

If this is the case then I feel that the time taken for dynamic dispatch is something that can be significantly improved also, based on [my previous tests](https://discourse.julialang.org/t/experiments-in-speeding-up-runtime-dispatch-for-integer-value-specialization/90355).

---

_[View the full topic](https://discourse.julialang.org/t/why-does-this-type-instability-lead-to-allocations/92821)._
