# Why/where does this allocate?

**URL:** https://discourse.julialang.org/t/why-where-does-this-allocate/122607
**Category:** Performance
**Created:** [November 13, 2024, 8:17pm UTC](https://discourse.julialang.org/t/why-where-does-this-allocate/122607 "2024-11-13T20:17:28Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [November 14, 2024, 2:37pm UTC](https://discourse.julialang.org/t/why-where-does-this-allocate/122607/10 "2024-11-14T14:37:29Z")

</div>

EDIT: I get my 0 allocations for typlues (not helping with the main issue of the thread) with `@generated` in front of the function here getting me an NTuple! My first use of that obscure feature. But it still doesn’t help me enough, ok often, not here when needing a pointer to it for ccall

> [@Performant way to create ntuple?](https://discourse.julialang.org/t/performant-way-to-create-ntuple/5783/2):
>
> This is [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276). You can work around it [using a let block](https://github.com/JuliaLang/julia/issues/15276#issuecomment-297596373) using BenchmarkTools @noinline g(x) = x[1] function f1(M, ::Type{Val{N}}) where N s = 0 for j = 1:M let j = j x = ntuple(i -\> i + j, Val{N}) s += g(x) end end return s end

Are (tuples and) NTuple inherently slow, and allocate more?

```julia
julia> mytuple_200() = ([UInt8(0) for i = 1:200]...,)

julia> @time mytuple_200() # not always 5 allocations for first...?!
  0.000057 seconds (5 allocations: 2.109 KiB)

```

next time around:

```julia
julia> @btime mytuple_200()
  8.320 μs (3 allocations: 464 bytes)

julia> @btime my_smallertuple_()
  229.241 ns (3 allocations: 80 bytes)

```

Always 3 allocations min. independent of size.

I was looking into SmallVector at:

> **[GitHub - matthias314/SmallCollections.jl: A Julia package providing variable-length set and...](https://github.com/matthias314/SmallCollections.jl)**
>
> A Julia package providing variable-length set and vector types that don't allocate

and it’s based on NTuple, and my code on simplified ntuple, or Base.\_ntuple it calls.

I realize I can’t get a pointer to it so not a substitute Memory or (any) Vector type, for me. But at least I though I would get fewer allocations that way, then worry about if getting a pointer to it would be possible with a hack.

I was hoping this about global scope but no:

```julia
julia> function my_code()
         mytuple_200()
         mytuple_200()
         nothing
       end
my_code (generic function with 1 method)

julia> @btime my_code()
  16.614 μs (6 allocations: 928 bytes)

vs.

julia> @benchmark Memory{UInt8}(undef, 200)
BenchmarkTools.Trial: 10000 samples with 997 evaluations.
 Range (min … max): 19.397 ns … 28.090 μs ┊ GC (min … max): 0.00% … 99.75%
 Time (median): 29.724 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 73.294 ns ± 537.958 ns ┊ GC (mean ± σ): 27.37% ± 6.50%

```

---

_[View the full topic](https://discourse.julialang.org/t/why-where-does-this-allocate/122607)._
