# Slices: should they default to views?

**URL:** https://discourse.julialang.org/t/slices-should-they-default-to-views/83023
**Category:** General Usage
**Created:** [June 19, 2022, 4:28pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023 "2022-06-19T16:28:01Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 19, 2022, 4:28pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/1 "2022-06-19T16:28:01Z")

</div>

I wonder what is more common, slicing a matrix to create a copy of the entries, or to create a view? If we desire the latter, we have to do `@views`. But if the latter is the more common use, wouldn’t it be nice to dispense with the need for an explicit macro?

Also, it seems that the expectation among new-comers to Julia is the latter. A case in point: if I come from Fortran, I expect slicing to be without a copy penalty (storage and speed).

The consequences of these incorrect assumptions are often corrected in this forum in order to re-gain performance. It might be good to match these expectations rather than to kick off a re-education process, wouldn’t you agree?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 19, 2022, 5:12pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/2 "2022-06-19T17:12:18Z")

</div>

This has been [discussed so many times already](https://github.com/JuliaLang/julia/issues/13157)… please let’s not rehash old topics 😩 In particular I think [this comment](https://github.com/JuliaLang/julia/issues/13157#issuecomment-221294186) is most relevant. Also Ref. [Array slices as views - what's the state of affairs?](https://discourse.julialang.org/t/array-slices-as-views-whats-the-state-of-affairs/1169)

The only thing that has changed since then is that views are stack allocated now most of the time. On the other hand, we have A LOT of code now that assumes this copies. So let’s either hold off on this discussion until 2.0, when it’s even possible to change that (though I have my doubts that this _would_ change, either way).

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [June 19, 2022, 5:12pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/3 "2022-06-19T17:12:23Z")

</div>

Yeah. The problem is that in the 0.6/0.7 Era, the compiler want as good so it wasn’t as clear.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 19, 2022, 5:14pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/4 "2022-06-19T17:14:54Z")

</div>

The topic is older than that - at the very least, 0.4 from what I could find. 0.6/0.7 was just the last time this was considered in any capacity, because that was the last breaking release before 1.0. No matter what, this change cannot happen before 2.0, which is nowhere near on the horizon from what I can tell.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [June 19, 2022, 5:29pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/5 "2022-06-19T17:29:01Z")

</div>

I thought an argument for not providing views when slicing was safety: you can accidentally overwrite your data if you aren’t aware/careful. Having to use `view`/`@view` is a way to opt into the unsafe mechanism, much like `@inbounds` is opt-in. But maybe I completely made up this argument?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 19, 2022, 5:30pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/6 "2022-06-19T17:30:25Z")

</div>

You may be right. Fortran has mechanisms for enforcing the semantics assumed in the callee (intent in, out, inout).

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 19, 2022, 5:35pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/7 "2022-06-19T17:35:40Z")

</div>

> [@Sukera](#):
>
> think [this comment](https://github.com/JuliaLang/julia/issues/13157#issuecomment-221294186) is most relevant

This is good.

Sorry about the rehash. But it is not straightforward to find out about the history of something, especially going back several years.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [June 19, 2022, 5:42pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/8 "2022-06-19T17:42:38Z")

</div>

> [@PetrKryslUCSD](#):
>
> Fortran has mechanisms for enforcing the semantics assumed in the callee (intent in, out, inout).

Yeah, as a side note I must say that’s a thing I like about fortran: we use the convention of the `!` to signal mutation, but it isn’t clear _what_ is being mutated: it’s usually the first argument, but not always, in some cases it’s the second, or the first two, or something else, or just none (or not even using `!` when mutation does happen in some of the arguments). The intent in fortran makes it clear, but I don’t think we can directly translate that to Julia, not all arguments are in principle mutable.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 19, 2022, 5:46pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/9 "2022-06-19T17:46:20Z")

</div>

> [@PetrKryslUCSD](#):
>
> Sorry about the rehash. But it is not straightforward to find out about the history of something, especially going back several years.

It’s alright, happens from time to time. I myself only searched for “view as slice” here on this very forum, found the discourse thread I linked and then through that found the issue where this was discussed.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 19, 2022, 11:17pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/10 "2022-06-19T23:17:24Z")

</div>

Julia doesn’t seem to do that weird NumPy thing where indices sometimes make a view and sometimes make a copy (if indices are irregular), so I think making `x[i]` syntax do views by default in v2 would be fine, especially for consistency with left-side (set)indexing writing to a view in a way. Maybe merge `getindex` with `copy` and `deepcopy`.

On the other hand, I wonder how much we’re taking things for granted. Maybe a switch would just turn complaints about copy-as-slice ruining performance with [repeated or large allocations](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-views) into complaints about views-as-slice ruining performance with [poorly cached accesses and impossible SIMD](https://docs.julialang.org/en/v1/manual/performance-tips/#Copying-data-is-not-always-bad). How could we know which complaints are worse?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 20, 2022, 12:18am UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/11 "2022-06-20T00:18:51Z")

</div>

I think you are right, copying data to coallesce memory accesses and to enable SIMD ops may well be the right thing to do sometimes.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [June 20, 2022, 3:27pm UTC](https://discourse.julialang.org/t/slices-should-they-default-to-views/83023/12 "2022-06-20T15:27:23Z")

</div>

People have also discussed that making broadcasting work nicely over slices (this is already possible via arcane syntax like `getindex.(Ref(A),inds)` instead of `A[inds]`) could alleviate the concern for the difference in many places. I don’t remember where this discussion took place, however, and don’t think it’s very close to being realized.
