# Pre-allocating differently typed arrays for multiple dispatch

**URL:** <https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041>\
**Category:** New to Julia\
**Created:** [November 27, 2018, 12:40am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041 "2018-11-27T00:40:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Post date:** [November 27, 2018, 12:40am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041/1 "2018-11-27T00:40:16Z")

</div>

I have a function that allocates a large matrix `H` each time.

```julia
function f(x::Vector{T}) where T <: Real
    H = Matrix{Complex{T}}(undef, 2, 3)
    # do stuff...
    H
end

```

I want pre-allocate `H` to speed up my code, but sometimes `T` is `Float64`, sometimes it’s a dual number or maybe something else.

I’m guessing this is a fairly common problem, but I didn’t know how to search for it. Is there a “best practice” way of pre-allocating arrays for each type and dispatching for the appropriate one?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 27, 2018, 12:54am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041/2 "2018-11-27T00:54:55Z")

</div>

Typically you know `T` outside the big loop (or something) and then you can just allocate it there and pass it into `f`.

---

<div class="post-metadata">

**Author:** ![JaredCrean2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaredcrean2/32/3574_2.png) [@JaredCrean2](https://discourse.julialang.org/u/JaredCrean2)\
**Post date:** [November 27, 2018, 12:55am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041/3 "2018-11-27T00:55:34Z")

</div>

The only way I know of is to make a type with the same static parameters, eg.

```julia
struct fData{T}
  H::Matrix{Complex{T}}

  function fData{T}() where {T}
    H = Matrix{Complex{T}}(undef, 2, 3)
    return new(H)
  end
end

function f(data::fData{T}, x::Vector{T}) where T <: Real
  H = data.h
  # do stuff
end

# in the caller, T must be known from somewhere
data = fData{T}()
x = rand(n) # however x gets created
for i=1:1000
  f(x, data)
end

```

This approach effectively pushes the problem out one level, because `data` is allocated outside the for loop. Its gets more difficult the more levels you have though.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 27, 2018, 2:19am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041/4 "2018-11-27T02:19:34Z")

</div>

We had some discussion about this issue over at [Conveniently avoiding allocating new MechanismState objects · Issue #363 · JuliaRobotics/RigidBodyDynamics.jl · GitHub](https://github.com/JuliaRobotics/RigidBodyDynamics.jl/issues/363) . Perhaps that kind of approach would work for you?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [November 27, 2018, 6:36am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041/5 "2018-11-27T06:36:46Z")

</div>

Yeah, that approach has been working quite well. I’ve since abstracted out the key parts into an `AbstractTypeDict`:

> <https://github.com/JuliaRobotics/RigidBodyDynamics.jl/blob/94b14f7dd22941918921180d9b20605a868f0638/src/caches.jl#L1-L64>

You could just copy the `AbstractTypeDict` type, make a concrete subtype and implement:

- a constructor
- a `valuetype` method
- a `makevalue` method

(Just pattern match `StateCache` or one of the other subtypes in that file).

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [November 27, 2018, 9:40am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041/6 "2018-11-27T09:40:07Z")

</div>

```
function f(x::Vector{T}) where T <: Real
    H = similar(x)
    f!(H, x)
    H
end

function f!(out::Vector{T}, in::Vector{T}) where T <: Real
    ... # your code here
end

```

Edit: sorry, didn’t notice `H` has a different type.  
You can use `eltype(x)` in caller script or function - and replace `similar(x)` with the type you need.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 27, 2018, 9:45am UTC](https://discourse.julialang.org/t/pre-allocating-differently-typed-arrays-for-multiple-dispatch/18041/7 "2018-11-27T09:45:28Z")

</div>

I see this pattern a lot, but it implicitly relies on `T` being closed under `f`. This is a reasonable assumption, but can quickly break down when relying eg on multiple AD packages at the same time.

The fundamental problem is that in general, it is very hard to preallocate for an operation

```julia
d = f(a, b, c)

```

as

```julia
f!(d, a, b, c)

```

when `a`, `b` and `c` can be sufficiently generic, because predicting the type of `d` can be tricky.

My current approach to this problem is to

1. initially avoid it, not think about preallocation,
2. the first line of optimizations is to use immutables, eg `StaticArrays`, and hope for the best,
3. if I decide I really need it, use some kind of a heuristic to determine the element type.

For the last one, something like

```julia
T = typeof(g(one(eltype(a)), one(eltybe(b)), one(eltype(c)))
d = Matrix{T}(undef, I_know, the_dimensions)

```

where `g` somehow reflects the elementary operations for what is going on in `f`. But this can be brittle as the `<: Real` interface is not formally specified and packages can deviate from expectations.
