# Help eliminating allocations

**URL:** <https://discourse.julialang.org/t/help-eliminating-allocations/42019>\
**Category:** General Usage\
**Created:** [June 25, 2020, 1:31am UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019 "2020-06-25T01:31:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 25, 2020, 1:31am UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019/1 "2020-06-25T01:31:13Z")

</div>

Hi!

I need to reproduce a code in a Kernel include file. In a part, it computes the size of an array of chars. Initially, I was doing something like this, which perfectly mimics the behavior in the .h:

```julia
function test(a::AbstractVector)
       m = length(a)
       return sizeof(NTuple{m, Cchar})
end

```

Note: This is just a MWE of what I am seeing.

The problem is that it allocates three times:

```julia
julia> data = [1,2,3,4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> @btime test($data)
  356.243 ns (3 allocations: 192 bytes)
4

```

The allocation is coming from the type `NTuple{m, Cchar}` that depends on `m`. Of course I can avoid it by just replacing with `m*sizeof(Cchar)`. However, this leads to many changes in a lot of functions. Thus, is there a way to create a type so that `sizeof` returns what I want without allocations?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 25, 2020, 5:36am UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019/2 "2020-06-25T05:36:01Z")

</div>

> [@Ronis\_BR](#):
>
> However, this leads to many changes in a lot of functions.

I’m not sure I understand, wouldn’t any other solution also require those changes?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 25, 2020, 7:33am UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019/3 "2020-06-25T07:33:08Z")

</div>

Not sure I understand, but would this work for you?

```julia
struct Foo{T}
    nelems :: Int
end

Foo(nelems, T) = Foo{T}(nelems)
Base.sizeof(x :: Foo{T}) where {T} = x.nelems * sizeof(T)

function test(a::AbstractVector)
    m = length(a)
    return sizeof(Foo(m, Cchar))
end

```

```julia
julia> using BenchmarkTools
julia> data = [1,2,3,4];
julia> @btime test($data)
  1.879 ns (0 allocations: 0 bytes)
4

```

---

<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:** [June 25, 2020, 7:48am UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019/4 "2020-06-25T07:48:36Z")

</div>

> [@Ronis\_BR](#):
>
> However, this leads to many changes in a lot of functions.

I mean, just do the changes then? This feels a bit like saying that the car runs slowly because there’s a puncture on all tires and asking if there is any way to make it faster that doesn’t involve changing the tires.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 25, 2020, 9:19am UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019/5 "2020-06-25T09:19:27Z")

</div>

Creating a type using runtime information is kind of the definition of “not type-stable”.

However, if the C code that you are translating is passing a fixed-length `data` vector then you could translate that into a `Tuple` or an `SVector` or `MVector` (from StaticArrays) instead of a `Vector`. That will make `m` a compile-time constant, and get rid of the allocations.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 25, 2020, 1:38pm UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019/6 "2020-06-25T13:38:31Z")

</div>

> [@Sukera](#):
>
> I’m not sure I understand, wouldn’t any other solution also require those changes?

> [@kristoffer.carlsson](#):
>
> I mean, just do the changes then? This feels a bit like saying that the car runs slowly because there’s a puncture on all tires and asking if there is any way to make it faster that doesn’t involve changing the tires.

I mean, if I change this, then I will loose the 1 to 1 relationship with the kernel functions. Thus, maybe in the future, if the kernel API changes, then it will be a little harder to update this file. Of course this is not the end of the world, I am just curious how can I avoid that problem 🙂

> [@ffevotte](#):
>
> Not sure I understand, but would this work for you?

Nice, the idea to overload `sizeof` just for that case was very good! Thanks!

> [@Per](#):
>
> However, if the C code that you are translating is passing a fixed-length `data` vector then you could translate that into a `Tuple` or an `SVector` or `MVector` (from StaticArrays) instead of a `Vector` . That will make `m` a compile-time constant, and get rid of the allocations.

Hum, I see. Unfortunately I can’t. Because this vector contains the message the user wants to transmit using SPI interface. It will be very bad to restrict to only the type in StaticArrays.jl. What I can do, it to force the user to use `Tuples` instead. I will see, but the proposal of @ffevotte seems very good.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 25, 2020, 1:51pm UTC](https://discourse.julialang.org/t/help-eliminating-allocations/42019/7 "2020-06-25T13:51:22Z")

</div>

> [@Ronis\_BR](#):
>
> the idea to overload `sizeof` just for that case was very good

Note that it’s not the overloading of `sizeof` that matters. It’s the fact that `Foo` doesn’t get `m` as a type parameter. Essentially, this is just a very convoluted way of writing `m*sizeof(Cchar)`.
