Skip to content

Commit 3bbbd07

Browse files
tpappc42f
authored andcommitted
document a common idiom of building SArrays iteratively (#668)
1 parent 1c53f4a commit 3bbbd07

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

docs/src/pages/api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,15 @@ analogy with the standard functions `setindex!`, `push!`, `pop!`, etc. (Note tha
326326
if the size of the static array changes, the type of the output will differ from
327327
the input.)
328328

329+
When building static arrays iteratively, it is usually efficient to build up an `MArray` first and then convert. The allocation will be elided by recent Julia compilers, resulting in very efficient code:
330+
```julia
331+
function standard_basis_vector(T, ::Val{I}, ::Val{N}) where {I,N}
332+
v = zero(MVector{N,T})
333+
v[I] = one(T)
334+
SVector(v)
335+
end
336+
```
337+
329338
### SIMD optimizations
330339

331340
It seems Julia and LLVM are smart enough to use processor vectorization

0 commit comments

Comments
 (0)