# Avoiding allocations for function arguments with non-concrete types

**URL:** <https://discourse.julialang.org/t/avoiding-allocations-for-function-arguments-with-non-concrete-types/57263>\
**Category:** Performance\
**Created:** [March 16, 2021, 4:55am UTC](https://discourse.julialang.org/t/avoiding-allocations-for-function-arguments-with-non-concrete-types/57263 "2021-03-16T04:55:58Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 17, 2021, 6:34pm UTC](https://discourse.julialang.org/t/avoiding-allocations-for-function-arguments-with-non-concrete-types/57263/9 "2021-03-17T18:34:46Z")

</div>

Yes, there are quite a few threads here dealing with that type of problem. There is no magic, but one possible solution was given here:

> [@Macro to write function with many conditionals](https://discourse.julialang.org/t/macro-to-write-function-with-many-conditionals/51616):
>
> The minimum working case would be: Let say I have a vector of mixed types: x = [isodd(i) ? rand(Int64) : rand(Float64) for i in 1:1000] I want to sum these numbers, with: function mysum(x) s = 0. for val in x s += val end s end This turns out to the be slow because of the type instability of the numbers and the runtime dispatch associated with it. This version which splits the calculations for each type runs much faster and does not allocate: function mysum\_fast(x) s = 0. …

---

_[View the full topic](https://discourse.julialang.org/t/avoiding-allocations-for-function-arguments-with-non-concrete-types/57263)._
