# Array views becoming dominant source of memory allocation

**URL:** <https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465>\
**Category:** Performance\
**Created:** [October 16, 2017, 11:31am UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465 "2017-10-16T11:31:24Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Nitin\_Arora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitin_arora/32/1174_2.png) [@Nitin\_Arora](https://discourse.julialang.org/u/Nitin_Arora)\
**Post date:** [October 20, 2017, 8:23am UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/21 "2017-10-20T08:23:01Z")

</div>

I would be really interested in understanding how you implemented that getindex!

---

<div class="post-metadata">

**Author:** ![foobar\_lv](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@foobar\_lv](https://discourse.julialang.org/u/foobar_lv)\
**Post date:** [October 20, 2017, 8:46am UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/22 "2017-10-20T08:46:34Z")

</div>

A very simple approach would be to split the SubArray type into a bitstype part (call it semi\_view) and a reference part (called parent).

Then you change the signature of all functions including views to take one argument more: A parent and a semi\_view. Because semi\_view is bitstype there are no allocations.

This would be kinda similar to the token/semi-token API used for the trees in datastructures.jl.

---

<div class="post-metadata">

**Author:** ![bingcrosby](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bingcrosby](https://discourse.julialang.org/u/bingcrosby)\
**Post date:** [October 24, 2017, 2:06pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/23 "2017-10-24T14:06:25Z")

</div>

Thanks for all the replies.

In my case, because the slices are often small, the allocations really slow down the performance. I might try something like the Unsafe VectorView type.

I had a look for open issues and I didn’t find this topic. Did I miss it or should I file something?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 24, 2017, 2:19pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/24 "2017-10-24T14:19:28Z")

</div>

> [@bingcrosby](#):
>
> I might try something like the Unsafe VectorView type.

**Don’t do it!!!**

If you need the performance now, just pass the two parts separately.

> [@bingcrosby](#):
>
> should I file something?

There are already enough issues about allocation optimization.

---

<div class="post-metadata">

**Author:** ![bingcrosby](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bingcrosby](https://discourse.julialang.org/u/bingcrosby)\
**Post date:** [October 24, 2017, 2:41pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/25 "2017-10-24T14:41:16Z")

</div>

Thanks yuyichao. Are you referring to foobar\_lv’s approach?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 24, 2017, 3:06pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/26 "2017-10-24T15:06:10Z")

</div>

> [@bingcrosby](#):
>
> foobar\_lv’s approach

Roughly `split the SubArray type into a bitstype part (call it semi_view) and a reference part (called parent).` yes. Definately not the other parts.

---

<div class="post-metadata">

**Author:** ![foobar\_lv](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@foobar\_lv](https://discourse.julialang.org/u/foobar_lv)\
**Post date:** [October 25, 2017, 2:50pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/27 "2017-10-25T14:50:17Z")

</div>

I completely agree with yoyichao. Using pointers to save subarray allocations is an amusing and educational hack, but a terrible idea in practice. I just did the pointer thing to get a non-allocating view in 10 lines of code, and learn more about julia on the way.

Whether split array views are worth the effort is up to you (the resulting API will suck and you need to spend the time to write a split\_view module).

Depending on your specific usecase, it might be easier to just write off array views as unusable in performance-critical sections, at least until the language improves. You can always just write the loops verbatim; this is what I am doing at the moment.

The resulting code doesn’t look as nice and is longer, but you will quickly find additional optimization opportunities once array accesses are source-code inlined (move a multiply for index calculations one loop-level up, improve memory access patterns, sprinkle @simd and @fastmath, the resulting code\_native becomes more recognizable and it becomes easier to spot performance problems in it).

Edit: If you want a one-word fix (literally!), you could try making views mutable and then reuse them. This prevents compiler-optimizations based on the immutability assumption, but I am not sure whether these are actually important for array views. This is something you would need to benchmark. I think that this should not change the data layout, i.e. cache-friendlyness and level-of-indirection should not be affected (but this needs to be tested).

---

<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:** [October 25, 2017, 5:50pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/28 "2017-10-25T17:50:34Z")

</div>

Simply defining your own mutable array view so you can increment or otherwise change the offset instead of defining new views, as in my example above, worked well and struck me as reasonable.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [December 25, 2017, 12:57pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/29 "2017-12-25T12:57:39Z")

</div>

> [@yuyichao](#):
>
> > I am not sure that this is fixable at all without changing the API.
> 
> Yes it is.

Is this going to be fixed by 1.0, or is it more likely to be 1.x?

---

<div class="post-metadata">

**Author:** ![bingcrosby](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bingcrosby](https://discourse.julialang.org/u/bingcrosby)\
**Post date:** [December 25, 2017, 1:15pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/30 "2017-12-25T13:15:13Z")

</div>

I certainly hope so!

---

<div class="post-metadata">

**Author:** ![bingcrosby](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bingcrosby](https://discourse.julialang.org/u/bingcrosby)\
**Post date:** [May 10, 2018, 3:56am UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/31 "2018-05-10T03:56:17Z")

</div>

I had a quick try with this my original post on Julia 0.7.0-DEV nightly windows 64 bit. It seems the performance limitation from my original post still remains.

Is this to be expected? If not, are there open issues that would address this problem?

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 14, 2018, 12:27pm UTC](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465/32 "2018-11-14T12:27:27Z")

</div>

You can try

[github.com/oschulz/UnsafeArrays.jl](http://github.com/oschulz/UnsafeArrays.jl)

UnsafeArrays.jl provides allocation-free arrays views, with some limitations. Most importantly, as @yuyichao already noted, pointer-based array access has to be used with great care and deliberation. In my experience, even frequent allocation of views doesn’t usually have a serious impact on performance in single-threaded use. With heavy multi-threading, though, view allocation time can currently become a limiting factor.

[Previous page](https://discourse.julialang.org/t/array-views-becoming-dominant-source-of-memory-allocation/6465.md?page=1)
