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
:
Broadcast.preprocess(dest::Array, bc::Broadcast.Broadcasted{Nothing}) = Broadcast.flatten(bc)
Then here’s an expression which is just complicated enough to trigger it:
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.