Skip to content

Commit 7583d87

Browse files
authored
Make StridedArray display as StridedArray (#31149)
Instead of a huge union. This is just a special-casing for this particular union instead of pursuing the general (and hard\!) typealias search. But given the ubiquity of StridedArray in method lists, I find this special case to be warranted and hugely beneficial. Here is a sampling from `methods(*)`: ```julia [242] *(A::LinearAlgebra.AbstractQ, b::StridedArray{T, 1} where T) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/qr.jl:556 [243] *(A::LinearAlgebra.AbstractQ, B::StridedArray{T, 2} where T) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/qr.jl:568 [244] *(Q::LinearAlgebra.AbstractQ, adjB::LinearAlgebra.Adjoint{#s614,#s613} where #s613<:(StridedVecOrMat{T} where T) where #s614) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/qr.jl:623 [245] *(A::StridedArray{T, 2} where T, Q::LinearAlgebra.AbstractQ) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/qr.jl:668 [246] *(A::StridedArray{T, 2} where T, adjB::LinearAlgebra.Adjoint{#s614,#s613} where #s613<:LinearAlgebra.AbstractQ where #s614) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/qr.jl:708 [247] *(A::LinearAlgebra.LQPackedQ, B::StridedVecOrMat{T} where T) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/lq.jl:153 [248] *(A::LinearAlgebra.LQPackedQ, adjB::LinearAlgebra.Adjoint{#s614,#s613} where #s613<:(StridedVecOrMat{T} where T) where #s614) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/lq.jl:177 [249] *(A::StridedVecOrMat{T} where T, adjQ::LinearAlgebra.Adjoint{#s614,#s613} where #s613<:LinearAlgebra.LQPackedQ where #s614) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/lq.jl:216 [250] *(A::StridedVecOrMat{T} where T, Q::LinearAlgebra.LQPackedQ) in LinearAlgebra at /home/mbauman/julia/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/lq.jl:240 ```
1 parent ab9ff0f commit 7583d87

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ Standard library changes
154154
ambiguity.
155155
* `close` on a file (`IOStream`) can now throw an exception if an error occurs when trying
156156
to flush buffered data to disk ([#35303]).
157+
* The large `StridedArray` `Union` now has special printing to avoid printing out its entire
158+
contents ([#31149]).
157159

158160
#### LinearAlgebra
159161
* The BLAS submodule now supports the level-2 BLAS subroutine `hpmv!` ([#34211]).

base/docs/basedocs.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,9 +2407,10 @@ Base.setproperty!
24072407
"""
24082408
StridedArray{T, N}
24092409
2410-
An `N` dimensional *strided* array with elements of type `T`. These arrays follow
2411-
the [strided array interface](@ref man-interface-strided-arrays). If `A` is a
2412-
`StridedArray`, then its elements are stored in memory with offsets, which may
2410+
A hard-coded [`Union`](@ref) of common array types that follow the [strided array interface](@ref man-interface-strided-arrays),
2411+
with elements of type `T` and `N` dimensions.
2412+
2413+
If `A` is a `StridedArray`, then its elements are stored in memory with offsets, which may
24132414
vary between dimensions but are constant within a dimension. For example, `A` could
24142415
have stride 2 in dimension 1, and stride 3 in dimension 2. Incrementing `A` along
24152416
dimension `d` jumps in memory by [`strides(A, d)`] slots. Strided arrays are

base/show.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,22 @@ function show(io::IO, @nospecialize(x::Type))
490490
show_datatype(io, x)
491491
return
492492
elseif x isa Union
493+
if x.a isa DataType && Core.Compiler.typename(x.a) === Core.Compiler.typename(DenseArray)
494+
T, N = x.a.parameters
495+
if x == StridedArray{T,N}
496+
print(io, "StridedArray")
497+
show_delim_array(io, (T,N), '{', ',', '}', false)
498+
return
499+
elseif x == StridedVecOrMat{T}
500+
print(io, "StridedVecOrMat")
501+
show_delim_array(io, (T,), '{', ',', '}', false)
502+
return
503+
elseif StridedArray{T,N} <: x
504+
print(io, "Union")
505+
show_delim_array(io, vcat(StridedArray{T,N}, uniontypes(Core.Compiler.typesubtract(x, StridedArray{T,N}))), '{', ',', '}', false)
506+
return
507+
end
508+
end
493509
print(io, "Union")
494510
show_delim_array(io, uniontypes(x), '{', ',', '}', false)
495511
return

test/show.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,16 @@ end
19091909
@test replstr(Set(['a'^100])) == "Set{String} with 1 element:\n \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa…"
19101910
end
19111911

1912+
@testset "Simple printing of StridedArray" begin
1913+
@test startswith(sprint(show, StridedArray), "StridedArray")
1914+
@test startswith(sprint(show, StridedVecOrMat), "StridedVecOrMat")
1915+
@test startswith(sprint(show, StridedVector), "Strided")
1916+
@test startswith(sprint(show, StridedMatrix), "Strided")
1917+
@test occursin("StridedArray", sprint(show, SubArray{T, N, A} where {T,N,A<:StridedArray}))
1918+
@test !occursin("Strided", sprint(show, Union{DenseArray, SubArray}))
1919+
@test !occursin("Strided", sprint(show, Union{DenseArray, Base.ReinterpretArray, Base.ReshapedArray, SubArray}))
1920+
end
1921+
19121922
@testset "0-dimensional Array. Issue #31481" begin
19131923
for x in (zeros(Int32), collect('b'), fill(nothing), BitArray(0))
19141924
@test eval(Meta.parse(repr(x))) == x

0 commit comments

Comments
 (0)