# Allocations when using FunctionWrapper and StaticArrays

**URL:** <https://discourse.julialang.org/t/allocations-when-using-functionwrapper-and-staticarrays/29078>\
**Category:** New to Julia\
**Tags:** memory-allocation\
**Created:** [September 23, 2019, 3:24pm UTC](https://discourse.julialang.org/t/allocations-when-using-functionwrapper-and-staticarrays/29078 "2019-09-23T15:24:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bender9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bender9000/32/9464_2.png) [@bender9000](https://discourse.julialang.org/u/bender9000)\
**Post date:** [September 23, 2019, 3:24pm UTC](https://discourse.julialang.org/t/allocations-when-using-functionwrapper-and-staticarrays/29078/1 "2019-09-23T15:24:38Z")

</div>

I’ve simplified my unnecessarily complicated post to show a simple case where FunctionWrappers allocate whereas using a closure without function argument types doesn’t allocate. I’m not sure why this happens. Any help on this topic is appreciated.

```julia
# %%
using StaticArrays
using BenchmarkTools
import FunctionWrappers
# %%

struct DummyModel{I}
    invoker::FunctionWrappers.FunctionWrapper{Float32,Tuple{MVector{I,Float32}}}
    modulecode
end

function compilemodel(y::Vector{Float32})
    I = length(y)
    
    runbody = quote
        function run(x::MVector{$I,Float32})
            m = SVector{$I,Float32}($y)
            first(x-m)
        end
    end
    
    runinvokerbody = quote
        runinvoker() = run
    end

    modulecode = Expr(:block, :(using StaticArrays), runbody, runinvokerbody)
    moduleexpr = Expr(:module, true, gensym(:CompiledModel), modulecode)
    eventmodule = eval(moduleexpr)

    DummyModel{I}(Base.invokelatest(eventmodule.runinvoker), modulecode)
end

# %%
MT = MVector{3,Float32}

modelvec = Vector{Float32}([1,2,3])
model = compilemodel(modelvec)

x = MT([1,2,3])

@btime $(model).invoker($x)

# %%

struct DummyModel1
    invoker::Function
end

function compilemodel1(y::Vector{Float32})    
    I = length(y)
    func = function (x::MVector{I,Float32}) where {I}
        m = SVector{I,Float32}(y)
        first(x-m)
    end
   DummyModel1(func) 
end

model1 = compilemodel1(modelvec)

@btime $(model1.invoker)($x)

```

**Original Post:**

I am trying to unroll some matrix operations using StaticArrays based on some model parameters without allocations. Following is a simplified example of how I was going about it in Julia 0.6. However, in Julia 1.1 the same code allocates.

What changes in Julia 1.x are causing this behavior and what is the recommended way to resolve this issue.

```julia
using StaticArrays
using BenchmarkTools
import FunctionWrappers

struct DummyModel{I}
    invoker::FunctionWrappers.FunctionWrapper{Float32,Tuple{MVector{I,Float32}}}
    modulecode
end

function compilemodel(y::Vector{Float32})
    I = length(y)
    
    runbody = quote
        function run(x::MVector{$I,Float32})
            m = SVector{$I,Float32}($y)
            first(x-m)
        end
    end
    
    runinvokerbody = quote
        runinvoker() = run
    end

    modulecode = Expr(:block, :(using StaticArrays), runbody, runinvokerbody)
    moduleexpr = Expr(:module, true, gensym(:CompiledModel), modulecode)
    eventmodule = eval(moduleexpr)

    DummyModel{I}(Base.invokelatest(eventmodule.runinvoker), modulecode)
end

function measure_allocations(model, input)
    @allocated model.invoker(input)
end

# %%

MT = MVector{3,Float32}

model = compilemodel(Vector{Float32}([1,2,3]))
x = MT([1,2,3])

# measure_allocations(model, x)
@btime $(model).invoker($x) # 20.849 ns (1 allocation: 16 bytes)

```
