# Allocations even when using StaticArrays.jl

**URL:** <https://discourse.julialang.org/t/allocations-even-when-using-staticarrays-jl/122775>\
**Category:** Performance\
**Tags:** staticarrays\
**Created:** [November 18, 2024, 2:00pm UTC](https://discourse.julialang.org/t/allocations-even-when-using-staticarrays-jl/122775 "2024-11-18T14:00:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnmyslinski](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@johnmyslinski](https://discourse.julialang.org/u/johnmyslinski)\
**Post date:** [November 18, 2024, 2:00pm UTC](https://discourse.julialang.org/t/allocations-even-when-using-staticarrays-jl/122775/1 "2024-11-18T14:00:50Z")

</div>

Hi All,

In this below MRE I am still seeing some allocations when using @btime. Based on my understanding, I would expect to see 0 allocations.

- Am I misunderstanding something?
- Are the 8 allocations I see a byproduct of my contrived testing set up?

Thanks!

```julia
using StaticArrays, BenchmarkTools

const Pnt3 = SVector{3, Float64}
const Pnt4 = SVector{4, Float64}

const Mat4 = SMatrix{4, 4, Float64}

struct Transformation
    m::Mat4
    inv_m::Mat4
end

function Translate(v::Pnt3)::Transformation
    m = Mat4(
        1, 0, 0, v[1],
        0, 1, 0, v[2],
        0, 0, 1, v[3],
        0, 0, 0, 1,
    )
    m_inv = Mat4(
        1, 0, 0, -v[1],
        0, 1, 0, -v[2],
        0, 0, 1, -v[3],
        0, 0, 0, 1,
    )
    return Transformation(m, m_inv)
end

function (t::Transformation)(p::Pnt3)::Pnt3
    tmp = Pnt4(p...,1.0)
    ph = Mat4(tmp..., tmp..., tmp..., tmp...)
    pt = t.m * ph
    pr = Pnt3(pt[SA[1,2,3]])
    if pt[4] == 1.0
        return pr
    end
    return pr ./ pt[4]
end

const p = Pnt3(1,2,3)
const tr = Translate(Pnt3(2,2,2))

function benchmark(tr::Transformation, p::Pnt3)::Pnt3
    @btime ($tr)($p)
end

benchmark(tr, p)
benchmark(tr, p)

```

Output

```julia
404.950 ns (8 allocations: 464 bytes)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 18, 2024, 2:01pm UTC](https://discourse.julialang.org/t/allocations-even-when-using-staticarrays-jl/122775/2 "2024-11-18T14:01:52Z")

</div>

> [@johnmyslinski](#):
>
> `const Mat4 = SMatrix{4, 4, Float64}`

Not a concrete type. [Should be](https://juliaarrays.github.io/StaticArrays.jl/stable/api/#SMatrix) `SMatrix{4, 4, Float64, 16}`

---

<div class="post-metadata">

**Author:** ![johnmyslinski](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@johnmyslinski](https://discourse.julialang.org/u/johnmyslinski)\
**Post date:** [November 18, 2024, 2:09pm UTC](https://discourse.julialang.org/t/allocations-even-when-using-staticarrays-jl/122775/3 "2024-11-18T14:09:43Z")

</div>

Ughhhhh.

I have been looking at this for way too long.

Appreciate the help!!!
