You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A small software package for the Julia programming language providing zero-dimensional array types. `Ref`-killer.
9
+
A tiny software package for the Julia programming language, providing zero-dimensional array types. `Ref`-killer.
10
10
11
-
Exports three zero-dimensional subtypes of `AbstractArray`:
11
+
Exports these zero-dimensional subtypes of `AbstractArray`, differing on topics such as mutability and identity:
12
12
*`ZeroDimArray`
13
13
* declared with `struct`, not with `mutable struct`
14
14
* does not support `setfield!`, or mutating the element otherwise
15
15
*`isbits` when the element is `isbits`
16
16
*`Box`
17
17
* declared with `mutable struct`
18
-
* supports `setfield!`
18
+
* supports `setfield!` for mutating the element
19
+
* acts as a reference to its element
19
20
*`BoxConstField`
20
21
* declared with `mutable struct`
21
22
* does not support `setfield!`, or mutating the element otherwise
22
-
* included for completeness, but not likely to be useful often
23
+
* acts as a reference to its element
24
+
25
+
Any zero-dimensional array is an iterator containing exactly one element (this follows from the zero-dimensional shape). `Ref`, too, is a zero-dimensional iterator, however it's not an array. Even though `Ref` supports being indexed like a zero-dimensional array is commonly indexed, without an index: `x[]`.
23
26
24
27
The motivation for creating this package is:
25
28
* To prevent the frequent confusion regarding `Ref` vs `Base.RefValue` by offering a replacement that makes `Ref` unnecessary in many use cases. Previous discussion:
* to provide a wrapper type for treating a value as a scalar in broadcasting, something `Ref` is often used for:
48
-
*`ZeroDimArray` can be a good replacement:
51
+
* To provide a [*boxing*](https://en.wikipedia.org/wiki/Boxing_(computer_programming)) feature, for example for data deduplication to avoid excessive memory use. Either `Box` or `BoxConstField` might be a good choice here, depending on whether mutability is desired. Compare:
52
+
*```julia-repl
53
+
julia> large_data = ntuple(identity, 8)
54
+
(1, 2, 3, 4, 5, 6, 7, 8)
55
+
56
+
julia> for _ ∈ 1:4
57
+
large_data = (large_data, large_data)
58
+
end
59
+
60
+
julia> Base.summarysize(large_data)
61
+
1024
62
+
63
+
julia> Base.summarysize([large_data for _ ∈ 1:1000]) # duplicates `large_data` a thousand times
julia> isa.(ZeroDimArray([1,2,3]), [Array, Dict, Int]) # now escape the vector from broadcasting using `ZeroDimArray`
53
85
3-element BitVector:
54
86
1
55
87
0
56
88
0
57
89
```
90
+
The other types, `Box` or `BoxConstField` would work for this use case, too, as would any zero-dimensional array, but `ZeroDimArray` is more likely to have zero cost for performance.
* `fill(x)`, creating a zero-dimensional `Array` containing `x` as its element, is often used instead of `Ref(x)`.
98
+
* `Array{T, 0} where {T}` is very similar to `Box`, albeit less efficient. The inefficiency is due to the fact that the implementation of `Array` supports resizeability (even though that's currently only available to users in the one-dimensional case of `Vector`), implying extra indirection, leading to extra pointer dereferences and extra allocation.
0 commit comments