# Implementation for AbstractVector with limited storage

**URL:** <https://discourse.julialang.org/t/implementation-for-abstractvector-with-limited-storage/130064>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 20, 2025, 2:28pm UTC](https://discourse.julialang.org/t/implementation-for-abstractvector-with-limited-storage/130064 "2025-06-20T14:28:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 20, 2025, 2:28pm UTC](https://discourse.julialang.org/t/implementation-for-abstractvector-with-limited-storage/130064/1 "2025-06-20T14:28:41Z")

</div>

I am solving a problem where for each set of parameters, I can have 1–4 outcomes. How many depends on the parameters.

Currently I am using something not unlike

```julia
solutions = Vector{T}() # T is calculated before, it is an immutable composite type
for schema in (schema1, schema2, schema3, schema4)
    sol = try_solution(schema, parameters)
    if sol !== nothing
       push!(solutions, sol)
    end
end
pick_best(solutions)

```

I benchmarked my code and the allocation is costly (I am doing this multiple times).

An obvious solution would be implementing a `<:AbstractVector{T}` with limited size, eg

```julia
mutable struct LimitedVector{N,T}
    count::Int
    contents::MVector{N,T}
    LimitedVector{N,T}() = new(0, MVector{N,T}())
end

```

with the obvious `push!`, `size`, `getindex`.

Two questions:

1. is there a package which implements something like this already? then I would not make yet another package 😉
2. is there a better approach?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 20, 2025, 2:34pm UTC](https://discourse.julialang.org/t/implementation-for-abstractvector-with-limited-storage/130064/2 "2025-06-20T14:34:24Z")

</div>

What about `sizehint!`ing the Vector? It’s already doing effectively what your `LimitedVector` does (albeit with the ability to surpass `N`). Or just allocating an `undef` array of length `N` and then `empty!`ing it.

There’d still be opportunities for more micro-optimizations, but that’d be a very simple 80% sort of solution.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 20, 2025, 5:42pm UTC](https://discourse.julialang.org/t/implementation-for-abstractvector-with-limited-storage/130064/3 "2025-06-20T17:42:30Z")

</div>

A `SmallVector` from SmallCollections.jl :

```julia
solutions = SmallVector{4,T}() # T is calculated before, it is an immutable composite type
for schema in (schema1, schema2, schema3, schema4)
    sol = try_solution(schema, parameters)
    if sol !== nothing
       solutions = push(solutions, sol)
    end
end
pick_best(solutions)

```
