# StaticRanges

**URL:** <https://discourse.julialang.org/t/staticranges/20919>\
**Category:** Performance\
**Tags:** package, performance\
**Created:** [February 18, 2019, 12:43am UTC](https://discourse.julialang.org/t/staticranges/20919 "2019-02-18T00:43:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [February 18, 2019, 12:43am UTC](https://discourse.julialang.org/t/staticranges/20919/1 "2019-02-18T00:43:28Z")

</div>

I’m working on what may turn into a package.

[https://github.com/Tokazama/StaticRanges.jl](https://github.com/Tokazama/StaticRanges.jl)

Conceptually it’s fairly simple. It has a one type `StaticRange` and a convenient interface through `srange` (which is meant to act like `range` from base). I shamelessly copied code from base julia to make it work. My current benchmarks (which litter the README page) show it may be a good alternative if you need to use the same set of indices many times.

Because this was more of a byproduct of something else I’m working on I don’t have a clear vision of the best way to handle it (package it, contribute to StaticArrays, leave it for my posterity to marvel at, etc.), but after benchmarking this morning I thought some people might benefit from it. I imagine there could be problems with ambiguities due to every field of ranges being part of the type’s parameterization, so it may serve as nothing more than an interesting idea.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [February 18, 2019, 5:50am UTC](https://discourse.julialang.org/t/staticranges/20919/2 "2019-02-18T05:50:13Z")

</div>

We have some related stuff in `StaticArrays`; did you see the `SOneTo` type, for instance?

```julia
julia> using StaticArrays

julia> axes(@SMatrix [1 2;
                      3 4;
                      5 6])
(SOneTo(3), SOneTo(2))

```

Though `SOneTo` is a subtype of `AbstractUnitRange` rather than `StaticArray`. CC @andyferris

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [February 18, 2019, 8:29am UTC](https://discourse.julialang.org/t/staticranges/20919/3 "2019-02-18T08:29:50Z")

</div>

Yes. But I specifically needed steps in my ranges.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [February 19, 2019, 3:33am UTC](https://discourse.julialang.org/t/staticranges/20919/4 "2019-02-19T03:33:59Z")

</div>

Yeah, I’m not suggesting you’re duplicating functionality, just pointing out some similarities with existing code. The functionality you’ve got looks useful and it probably makes sense to put it in `StaticArrays`. Though we’re already maintaining a rather large amount of code in that package because there’s statically sized equivalents of large parts of stdlib/LinearAlgebra as well as the Base abstract array functionality. It feels like it’s getting a little unweildy 😉

One thing I noticed is that your range type is not a subtype of `AbstractRange` which makes sense because you’d like it to subtype `StaticVector`, which is obviously not a range. That does mean you might have to duplicate some functionality which is automatically provided for `AbstractRange`? There’s not really any good solution to that without formalized traits or interfaces in the language.

---

<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:** [February 19, 2019, 3:57am UTC](https://discourse.julialang.org/t/staticranges/20919/5 "2019-02-19T03:57:22Z")

</div>

My guess would be that the few default behaviors `AbstractRange` provides would want to be specialized in any case. The biggest things you’d be giving up would be functions limited to `AbstractRange` — which I think are very few beyond `AbstractUnitRange`’s use as an axis type.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [February 19, 2019, 4:45am UTC](https://discourse.julialang.org/t/staticranges/20919/6 "2019-02-19T04:45:37Z")

</div>

> [@c42f](#):
>
> we’re already maintaining a rather large amount of code in that package

Yeah, this is why I didn’t bring it up over there. This is kind of just thrown together and may get some TLC if I keep finding urgent uses for it.

> [@c42f](#):
>
> One thing I noticed is that your range type is not a subtype of `AbstractRange`

I tried subtyping `AbstractRange` but found little immediate benefit. It does make logical sense but that only seemed to inherit an error when I had yet to define `step`. By subtyping `StaticVector` I believe it inherits some nice boosts in indexing under the hood.

> [@mbauman](#):
>
> few beyond `AbstractUnitRange` ’s use as an axis type.

I haven’t yet ran into any issues with this but I also haven’t tried to push it that far. I’ve thought about defining something like `StaticIndices` but I haven’t investigated how much effort that would take to make useful. I assume it would be pretty involved.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [March 14, 2019, 12:40am UTC](https://discourse.julialang.org/t/staticranges/20919/7 "2019-03-14T00:40:20Z")

</div>

Been putting together more organized benchmarks and finding a more appealing way of presenting it. Also updated the github repo.

 ![indexing](https://global.discourse-cdn.com/julialang/original/3X/4/9/490e86d6cccd9881060285ead1b44b2a3ab32167.png)

The four groups are the structures being indexed into and the labels at the bottom describe the structure used to perform the indexing. The “by\_nd\_\*” is performing indexing along each axis instead of just linearly. I will probably replace these as I finish up some multidimensional indexing.
