# Recommended way to set up global buffers

**URL:** <https://discourse.julialang.org/t/recommended-way-to-set-up-global-buffers/89597>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 1, 2022, 4:43am UTC](https://discourse.julialang.org/t/recommended-way-to-set-up-global-buffers/89597 "2022-11-01T04:43:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [November 1, 2022, 4:43am UTC](https://discourse.julialang.org/t/recommended-way-to-set-up-global-buffers/89597/1 "2022-11-01T04:43:56Z")

</div>

Say my package needs some global buffers to avoid allocations of many small vectors of a fixed type but with variable length (with benchmark results showing that this gives indeed a nice speedup). This could look as follows:

```julia
const BUFFER = Vector{Vector{Int}}()
const BUFFER_LENGTH = 64

function __init__ ()
    Threads.resize_nthreads!(BUFFER, Vector{Int}(undef, BUFFER_LENGTH))
end

function do_some_work(args...)
    if some_length <= BUFFER_LENGTH
        buffer = BUFFER[Threads.threadid()]
    else
        buffer = # allocate
    end
    # use buffer etc.
end

```

This worked with static scheduling in the past. Now, we have two more challenges:

- Dynamic scheduling of threads
- Precompilation with executing work such as SnoopPrecompile.jl does not see buffers set up in ` __init__ `.

What is currently the best way to set up some global buffers for a case like this?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [November 1, 2022, 5:45am UTC](https://discourse.julialang.org/t/recommended-way-to-set-up-global-buffers/89597/2 "2022-11-01T05:45:05Z")

</div>

While I’m not certain about how to deal with precompilation, I think the dynamic scheduling issue might be worked around by using a `Channel` instead of a `Vector` of vectors. The difference between a channel and a vector in this context is that the element is removed from the collection in the former, so two tasks can’t access the same buffer. Once a task completes, it may return its buffer back to the pool.

---

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [November 1, 2022, 8:39am UTC](https://discourse.julialang.org/t/recommended-way-to-set-up-global-buffers/89597/4 "2022-11-01T08:39:54Z")

</div>

Thanks! I may look into that. But first, I will wait to see if there is a nice way solving the precompilation problem, too.
