# What is StrideArrays.jl

**URL:** <https://discourse.julialang.org/t/what-is-stridearrays-jl/97146>\
**Category:** Performance\
**Tags:** arrays\
**Created:** [April 6, 2023, 1:39am UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146 "2023-04-06T01:39:50Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 16, 2023, 5:28am UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146/21 "2023-04-16T05:28:24Z")

</div>

PRs adding docs or relevant information from here are welcome.

---

<div class="post-metadata">

**Author:** ![martin.d.maas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin.d.maas/32/50964_2.png) [@martin.d.maas](https://discourse.julialang.org/u/martin.d.maas)\
**Post date:** [April 19, 2023, 3:13am UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146/22 "2023-04-19T03:13:36Z")

</div>

Hi @Elrod, amazing package as usual!

I’m just wondering, what do you mean by @gc\_preserved being required? Is that an alternative to using @inline, or is @inline required anyway?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 19, 2023, 4:08am UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146/23 "2023-04-19T04:08:45Z")

</div>

It is an alternative.

Julia can stack allocate `mutable struct`s such as `MArray` or `StaticStrideArray`s, but (currently) only if they aren’t returned or passed to a non-inlined function.  
So either you can inline everything, or you can use `@gc_preserve` to circumvent that.

`@gc_preserve` uses `GC.@preserve` to protect the memory, and calls `PtrArray` on the arrays, so that you have a non-owning view (regular `view`s in Julia are owning).

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [April 19, 2023, 6:39pm UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146/24 "2023-04-19T18:39:14Z")

</div>

So `@gc_preserve` appears to be a decisive.

From my tests, `MArray` with `@inline`, and even with cutting and pasting the code to be inlined does create allocations (I need an allocation-free solve of a small linear system). So my experience with `MArray` in this respect is shaky.

OTOH, with `@gc_preserve` this problem goes away, and it seems that for my use case, the choice between `MArray` and `StridedArray` ends up as a matter of taste.

However `@gc_preserve` is not exported. Would you mind a PR to StrideArraysCore to elevate `@gc_preserve` to stable API status ?

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [April 19, 2023, 8:30pm UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146/25 "2023-04-19T20:30:18Z")

</div>

As for `MMatrix`: see [inconsistent MArray allocation behavior with @inbounds, loop order · Issue #874 · JuliaArrays/StaticArrays.jl · GitHub](https://github.com/JuliaArrays/StaticArrays.jl/issues/874)

In addition to `@inline` one needs to have `@inbounds` at loops with nonstandard access (e.g. reverse order) to allow for allocation free behaviour.

---

<div class="post-metadata">

**Author:** ![martin.d.maas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin.d.maas/32/50964_2.png) [@martin.d.maas](https://discourse.julialang.org/u/martin.d.maas)\
**Post date:** [April 20, 2023, 4:32am UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146/26 "2023-04-20T04:32:49Z")

</div>

@j-fu and @Elrod, do you guys have a minimal working example of how to use @gc\_preserve instead of @inline?

So far, I’m able to run some simple tests with @inline, but can’t port that to my real code.

Thanks!

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [April 20, 2023, 12:01pm UTC](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146/27 "2023-04-20T12:01:03Z")

</div>

I have a working example [here](https://j-fu.github.io/VoronoiFVM.jl/stable/examples/Example510_Mixture/). Being non-allocating is absolutely mandatory, as this function may be called 100\_000s of times, sometimes with eltype Float64, sometimes with eltype ForwardiDiff.Dual{…}.

[Previous page](https://discourse.julialang.org/t/what-is-stridearrays-jl/97146.md?page=1)
