Skip to content

Commit 60b60f1

Browse files
stevengjStefanKarpinski
authored andcommitted
some basic views documentation (#34390)
Added a little basic discussion at the beginning of the "views" section of the standard-library documentation, based on [this discourse post](https://discourse.julialang.org/t/could-you-explain-what-are-views/17535/3?u=stevengj) which people seem to have found helpful.
1 parent 5578a0c commit 60b60f1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

doc/src/base/arrays.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ Base.checkindex
104104

105105
## Views (SubArrays and other view types)
106106

107+
A “view” is a data structure that acts like an array (it is a subtype of `AbstractArray`), but the underlying data is actually
108+
part of another array.
109+
110+
For example, if `x` is an array and `v = @view x[1:10]`, then `v` acts like a 10-element array, but its data is actually
111+
accessing the first 10 elements of `x`. Writing to a view, e.g. `v[3] = 2`, writes directly to the underlying array `x`
112+
(in this case modifying `x[3]`).
113+
114+
Slicing operations like `x[1:10]` create a copy by default in Julia. `@view x[1:10]` changes it to make a view. The
115+
`@views` macro can be used on a whole block of code (e.g. `@views function foo() .... end` or `@views begin ... end`)
116+
to change all the slicing operations in that block to use views. Sometimes making a copy of the data is faster and
117+
sometimes using a view is faster, as described in the [performance tips](@ref man-performance-views).
118+
107119
```@docs
108120
Base.view
109121
Base.@view

doc/src/manual/performance-tips.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ example, but in many contexts it is more convenient to just sprinkle
10131013
some dots in your expressions rather than defining a separate function
10141014
for each vectorized operation.)
10151015

1016-
## Consider using views for slices
1016+
## [Consider using views for slices](@id man-performance-views)
10171017

10181018
In Julia, an array "slice" expression like `array[1:5, :]` creates
10191019
a copy of that data (except on the left-hand side of an assignment,

0 commit comments

Comments
 (0)