# Problem with broadcasting, flatten, and type-stability

**URL:** <https://discourse.julialang.org/t/problem-with-broadcasting-flatten-and-type-stability/25539>\
**Category:** General Usage\
**Created:** [June 21, 2019, 11:30pm UTC](https://discourse.julialang.org/t/problem-with-broadcasting-flatten-and-type-stability/25539 "2019-06-21T23:30:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [June 21, 2019, 11:30pm UTC](https://discourse.julialang.org/t/problem-with-broadcasting-flatten-and-type-stability/25539/1 "2019-06-21T23:30:27Z")

</div>

I’m writing a custom `AbstractArray` implementation which supports broadcasting. This implementation makes use of `Broadcast.flatten`, but I’m finding that this introduces type-instabilities once expressions go beyond a few operations.

Here’s a MWE. Rather than give you my whole custom `AbstractArray` type, you can see it by just overloading normal Array broadcasting to insert a call to `flatten`:

```julia
Broadcast.preprocess(dest::Array, bc::Broadcast.Broadcasted{Nothing}) = Broadcast.flatten(bc)

```

Then here’s an expression which is just complicated enough to trigger it:

```julia
x = rand(2,2)
foo(x) = @. (x + 2*x + 2*x)*x
@code_warntype foo(x)

```

Scrolling through the long output, you’ll see the red. Although the return type is inferred, the instabilities within result in e.g. a 1000x performance hit on a 128x128 array (checked on Julia 1.1 and release-1.2/8a84ba5018)

Am I doing anything stupid here, and if not does anyone have strategies to remedy this or make things easier on inference? Should I file an Issue? Thanks.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [June 22, 2019, 2:32am UTC](https://discourse.julialang.org/t/problem-with-broadcasting-flatten-and-type-stability/25539/2 "2019-06-22T02:32:37Z")

</div>

> [@marius311](#):
>
> Should I file an Issue?

Oh yea, I thought this sounded familiar, I realize now I already filed it a year ago to much interesting discussion 🙂 [inference failure for broadcast implementations that use `flatten` (eg SparseArrays) · Issue #27988 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/27988)

Unfortunately still no solution. The underlying thing being the fairly weird fact that the following does not infer, but it does if you start a fresh session and uncomment the commented line:

```julia
using Test
using Base.Broadcast: cat_nested, Broadcasted
#@inferred cat_nested(1, Broadcasted(*, (2, 3)), 4, 5, 6, 7)
@inferred cat_nested(Broadcasted(*, (1, Broadcasted(*, (2, 3)), 4, 5, 6, 7)))

```
