# Parametric Structs

**URL:** <https://discourse.julialang.org/t/parametric-structs/130960>\
**Category:** New to Julia\
**Tags:** performance, parametric-types\
**Created:** [July 22, 2025, 11:30pm UTC](https://discourse.julialang.org/t/parametric-structs/130960 "2025-07-22T23:30:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lancejnelson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lancejnelson/32/21898_2.png) [@lancejnelson](https://discourse.julialang.org/u/lancejnelson)\
**Post date:** [July 22, 2025, 11:30pm UTC](https://discourse.julialang.org/t/parametric-structs/130960/1 "2025-07-22T23:30:10Z")

</div>

From a performance stand point, which is better:

```julia
struct FlexibleSystem{D, S, TCELL} <: AbstractSystem{D}
    particles::AbstractVector{S}
    cell::TCELL
    data::Dict{Symbol, Any} # Store arbitrary data about the atom.
end

```

or this:

```julia
struct FlexibleSystem{D, S,V<:AbstractVector{S}, TCELL} <: AbstractSystem{D}
    particles::V
    cell::TCELL
    data::Dict{Symbol, Any} # Store arbitrary data about the atom.
end

```

I feel like using an AbstractVector type for a struct field will lead to less efficient code.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [July 22, 2025, 11:36pm UTC](https://discourse.julialang.org/t/parametric-structs/130960/2 "2025-07-22T23:36:46Z")

</div>

I guess that for the second version you mean

```julia
struct FlexibleSystem{D, S, V <: AbstractVector{S}, TCELL} <: AbstractSystem{D}
    particles::V
    cell::TCELL
    data::Dict{Symbol, Any} # Store arbitrary data about the atom.
end

```

That’s usually better because then the compiler knows the exact type of `particles`. See the [manual](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container).

---

<div class="post-metadata">

**Author:** ![lancejnelson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lancejnelson/32/21898_2.png) [@lancejnelson](https://discourse.julialang.org/u/lancejnelson)\
**Post date:** [July 22, 2025, 11:58pm UTC](https://discourse.julialang.org/t/parametric-structs/130960/3 "2025-07-22T23:58:59Z")

</div>

So why would you ever do the first option; why is it even allowed if it’s less performant?

---

<div class="post-metadata">

**Author:** ![lancejnelson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lancejnelson/32/21898_2.png) [@lancejnelson](https://discourse.julialang.org/u/lancejnelson)\
**Post date:** [July 23, 2025, 12:00am UTC](https://discourse.julialang.org/t/parametric-structs/130960/4 "2025-07-23T00:00:01Z")

</div>

And no I don’t mean to use your modification. Can you explain why your modification is the right way to do it?

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [July 23, 2025, 12:04am UTC](https://discourse.julialang.org/t/parametric-structs/130960/5 "2025-07-23T00:04:21Z")

</div>

> [@lancejnelson](#):
>
> why would you ever do the first option

There are cases where you don’t know the exact type ahead of time. (Think of a `mutable struct`.) Also, the first version might be better if you want to put many objects with different subtypes of `AbstractVector{S}` into a `Vector` or some other collection. (I haven’t checked this.)

> Can you explain why your modification is the right way to do it?

As far as I can tell, your second version has an undefined type `V`. And what would be the purpose of having `AbstractVector{S}` in the type parameters?

---

<div class="post-metadata">

**Author:** ![marteaua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marteaua/32/214927_2.png) [@marteaua](https://discourse.julialang.org/u/marteaua)\
**Post date:** [July 23, 2025, 12:08am UTC](https://discourse.julialang.org/t/parametric-structs/130960/6 "2025-07-23T00:08:51Z")

</div>

It is allowed because Julia is / aims to be very flexible.

Here, putting `V` in the type parameters will lead to more compiled code if you use different concrete types `V` for many different structs constructed, for example StaticArrays’ SVector of a lot of different sizes.

On the other hand, only one “version” of the struct will be instantiated and methods taking your struct as argument won’t compile new method for each concrete type of the array inside, although that method will be less performant.

So it’s a tradeoff, and both will make sense is certain situations..

---

<div class="post-metadata">

**Author:** ![lancejnelson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lancejnelson/32/21898_2.png) [@lancejnelson](https://discourse.julialang.org/u/lancejnelson)\
**Post date:** [July 23, 2025, 12:46am UTC](https://discourse.julialang.org/t/parametric-structs/130960/7 "2025-07-23T00:46:29Z")

</div>

I see why your modification is right. I hadn’t noticed that V wasn’t defined in the type signature.

I just always feel lost when it comes to parametric types and it seems like an important issue when it comes to performance.

It seems that not matter how the struct is defined, as long as concrete types are used upon instantiating, the performance should be optimal. The parameters are just for creating families of types. Is that the right way to think? Or can someone show me a parametric type that is inefficient no matter what.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [July 23, 2025, 1:15am UTC](https://discourse.julialang.org/t/parametric-structs/130960/8 "2025-07-23T01:15:44Z")

</div>

Yes, you want to have concrete types for best performance. This applies recursively. For example, the type `Dict{Symbol,Any}` that you use is concrete, but replacing `Any` by a concrete type would be preferable (if possible).

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 23, 2025, 7:44am UTC](https://discourse.julialang.org/t/parametric-structs/130960/9 "2025-07-23T07:44:16Z")

</div>

> [@lancejnelson](#):
>
> And no I don’t mean to use your modification. Can you explain why your modification is the right way to do it?

The code you provided has invalid syntax, @matthias314 presumably extrapolated valid code by reasoning about your actual intention.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 23, 2025, 7:45am UTC](https://discourse.julialang.org/t/parametric-structs/130960/10 "2025-07-23T07:45:02Z")

</div>

> [@matthias314](#):
>
> See the [manual](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container).

I second this. See the Performance Tips for a start.

---

<div class="post-metadata">

**Author:** ![lancejnelson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lancejnelson/32/21898_2.png) [@lancejnelson](https://discourse.julialang.org/u/lancejnelson)\
**Post date:** [July 23, 2025, 2:45pm UTC](https://discourse.julialang.org/t/parametric-structs/130960/11 "2025-07-23T14:45:01Z")

</div>

Here are two examples from AtomsBase:

```julia
struct FastSystem{D, TCELL, L <: Unitful.Length, M <: Unitful.Mass, S} <: AbstractSystem{D}
    cell::TCELL
    position::Vector{SVector{D, L}}
    species::Vector{S}
    mass::Vector{M}
end

```

and

```julia
struct FlexibleSystem{D, S, TCELL} <: AbstractSystem{D}
    particles::AbstractVector{S}
    cell::TCELL
    data::Dict{Symbol, Any} # Store arbitrary data about the atom.
end

```

from the names I gather that one structure is more flexible, and possibly less efficient/performant, than the other. Is this because the type of `particles` is Abstract, and therefore it’s type can be changed?
