# Allocation using broadcasting with custom type

**URL:** <https://discourse.julialang.org/t/allocation-using-broadcasting-with-custom-type/4592>\
**Category:** General Usage\
**Created:** [July 2, 2017, 12:05am UTC](https://discourse.julialang.org/t/allocation-using-broadcasting-with-custom-type/4592 "2017-07-02T00:05:24Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [July 2, 2017, 8:23pm UTC](https://discourse.julialang.org/t/allocation-using-broadcasting-with-custom-type/4592/2 "2017-07-02T20:23:12Z")

</div>

Looks like there are two issues: slatting, and specialization on the function (by default specialization only happens if function is called). This gets rid of allocations:

```julia
function Base.Broadcast.broadcast!{F}(f::F, dest::Foo, src1, src2)
    broadcast!(f, unsafe_get(dest), unsafe_get(src1), unsafe_get(src2))
    return dest
end

```

I’m not sure whether there’s a way of avoiding allocations and still use splatting. However, note that the allocation is only 16 bytes per iteration, so if the array is large this may not matter in practice (no copy of the array is made).

BTW, [this thread](https://discourse.julialang.org/t/broadcast-for-custom-type/3497/3) might be useful.

---

_[View the full topic](https://discourse.julialang.org/t/allocation-using-broadcasting-with-custom-type/4592)._
