# How to do custom allocators

**URL:** <https://discourse.julialang.org/t/how-to-do-custom-allocators/69112>\
**Category:** Performance\
**Tags:** memory-allocation, struct\
**Created:** [October 2, 2021, 1:02pm UTC](https://discourse.julialang.org/t/how-to-do-custom-allocators/69112 "2021-10-02T13:02:29Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [October 16, 2021, 10:34pm UTC](https://discourse.julialang.org/t/how-to-do-custom-allocators/69112/21 "2021-10-16T22:34:36Z")

</div>

I’m not sure. There is some intrusive linked list even in [Julia core](https://github.com/JuliaLang/julia/blob/bb5b98e72a151c41471d8cc14cacb495d647fb7f/base/linked_list.jl). It seems to be useful to implement a queue.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [October 17, 2021, 10:13am UTC](https://discourse.julialang.org/t/how-to-do-custom-allocators/69112/22 "2021-10-17T10:13:22Z")

</div>

I’m just wondering what are the modification patterns where a custom allocator in addition to the built-in one offers an advantage. Of course, using lists as stacks or queues is one such case but the solution for that case is basically known.

Maybe one case is when it’s known that the structure isn’t going to contain more than N items, and a pool of O(N) pre-allocated objects offers some advantages over the Julia allocator.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [October 17, 2021, 11:50am UTC](https://discourse.julialang.org/t/how-to-do-custom-allocators/69112/23 "2021-10-17T11:50:20Z")

</div>

Hi Vasily,

let me try to motivate this with your simple List definition

```julia
mutable struct Cons{T}
    car::T
    cdr::Union{Nothing, Cons{T}}
end
const List{T} = Union{Nothing, Cons{T}}

```

`T = Tuple{Int, StaticArrays.SVector{100, Float64}}` and rather artificial tests similar to

```julia
l = nil()
for j in 1:n
    l = cons((j, p) , l)
end
s = 0
while l != nothing
    s += l.car[1]
    l = l.cdr
end

```

you can either use the Julia allocator resulting in

```julia
 List without allocator 442.167 ms (1000000 allocations: 854.49 MiB)

```

or a fixed size allocator (if you know the maximal number of elements)

```julia
  with fixed allocator 83.345 ms (0 allocations: 0 bytes)

```

or a resizable allocator

```julia
  with resizable allocator 75.376 ms (0 allocations: 0 bytes)

```

or an allocator keeping an internal free list (if you want to remove and readd elements to the list)

```julia
  with fixed free list allocator 98.112 ms (0 allocations: 0 bytes)
  with resizable free list allocator 88.990 ms (0 allocations: 0 bytes)

```

or an corresponding SOA allocator

```julia
  with fixed SOA allocator 56.665 ms (0 allocations: 0 bytes)
  with resizable SOA allocator 56.830 ms (0 allocations: 0 bytes)
  with fixed free list SOA allocator 68.195 ms (0 allocations: 0 bytes)
  with resizable free list SOA allocator 73.258 ms (0 allocations: 0 bytes)

```

For me these are only abstractions of common preallocation patterns. I see a tradeoff of programming convenience vs. a bit of performance gain, because you have to rework your data structures.

[Previous page](https://discourse.julialang.org/t/how-to-do-custom-allocators/69112.md?page=1)
