Skip to content

Commit b8fef1f

Browse files
authored
Make length 1 blocks non-allocating (#189)
* Make length 1 blocks non-allocating * Faster views for subarrays * ambiguity in triangular getindex * Update test_blockrange.jl * Update .gitignore * fix replace_in_print when with undef_blocks
1 parent cc3d03c commit b8fef1f

File tree

9 files changed

+42
-16
lines changed

9 files changed

+42
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ benchmark/*.md
88
src/.DS_Store
99
/Manifest.toml
1010
.DS_Store
11+
build

Project.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "0.16.5"
3+
version = "0.16.6"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
@@ -14,11 +14,10 @@ julia = "1.6"
1414

1515
[extras]
1616
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
17-
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
1817
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1918
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2019
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2120
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2221

2322
[targets]
24-
test = ["Base64", "FillArrays", "OffsetArrays", "SparseArrays", "StaticArrays", "Test"]
23+
test = ["Base64", "OffsetArrays", "SparseArrays", "StaticArrays", "Test"]

src/abstractblockarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ end
180180
@inline Base.getindex(A::AbstractMatrix, kr::AbstractVector, jr::Block) = ArrayLayouts.layout_getindex(A, kr, jr)
181181
@inline Base.getindex(A::AbstractMatrix, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
182182
@inline Base.getindex(A::LayoutMatrix, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
183+
@inline Base.getindex(A::AbstractTriangular{<:Any,<:LayoutMatrix}, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
183184

184185
###
185186
# permutedims

src/blockarray.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,20 @@ function _replace_in_print_matrix_inds(block_arr, i...)
423423
bl, inds
424424
end
425425
function Base.replace_in_print_matrix(block_arr::BlockArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString)
426-
bl, inds = _replace_in_print_matrix_inds(block_arr, i, j)
427-
Base.replace_in_print_matrix(bl, inds..., s)
426+
try
427+
bl, inds = _replace_in_print_matrix_inds(block_arr, i, j)
428+
Base.replace_in_print_matrix(bl, inds..., s)
429+
catch UndefRefError # thrown with undef_blocks
430+
s
431+
end
428432
end
429433
function Base.replace_in_print_matrix(block_arr::BlockArray{<:Any,1}, i::Integer, j::Integer, s::AbstractString)
430-
bl, inds = _replace_in_print_matrix_inds(block_arr, i)
431-
Base.replace_in_print_matrix(bl, inds..., j, s)
434+
try
435+
bl, inds = _replace_in_print_matrix_inds(block_arr, i)
436+
Base.replace_in_print_matrix(bl, inds..., j, s)
437+
catch UndefRefError
438+
s
439+
end
432440
end
433441

434442
########

src/blockaxis.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ end
269269
270270
returns the first index of each block of `a`.
271271
"""
272-
blockfirsts(a::AbstractUnitRange{Int}) = [1]
272+
blockfirsts(a::AbstractUnitRange{Int}) = Ones{Int}(1)
273273
"""
274274
blocklasts(a::AbstractUnitRange{Int})
275275
276276
returns the last index of each block of `a`.
277277
"""
278-
blocklasts(a::AbstractUnitRange{Int}) = [length(a)]
278+
blocklasts(a::AbstractUnitRange{Int}) = Fill(length(a),1)
279279
"""
280280
blocklengths(a::AbstractUnitRange{Int})
281281

src/pseudo_blockarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,6 @@ end
286286

287287
Base.replace_in_print_matrix(f::PseudoBlockVecOrMat, i::Integer, j::Integer, s::AbstractString) =
288288
Base.replace_in_print_matrix(f.blocks, i, j, s)
289+
290+
291+
LinearAlgebra.norm(A::PseudoBlockArray, p::Real=2) = norm(A.blocks, p)

src/views.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,24 @@ block(A::BlockSlice) = block(A.block)
129129
block(A::Block) = A
130130

131131
# unwind BLockSlice1 for AbstractBlockArray
132-
@inline Base.view(block_arr::AbstractBlockArray{<:Any,N}, blocks::Vararg{BlockSlice1, N}) where N =
132+
@inline Base.view(block_arr::AbstractBlockArray{<:Any,N}, blocks::Vararg{BlockSlice1, N}) where N =
133133
view(block_arr, map(block,blocks)...)
134134

135-
@inline Base.view(V::SubArray{<:Any,N,<:AbstractBlockArray,<:NTuple{N,BlockSlice{<:BlockRange{1}}}}, block::Block{N}) where N =
136-
view(parent(V), map((b, i) -> b.block[i], parentindices(V), block.n)...)
135+
const BlockSlices = Union{Base.Slice,BlockSlice{<:BlockRange{1}}}
136+
# Base.view(V::SubArray{<:Any,N,NTuple{N,BlockSlices}},
137+
138+
_block_reindex(b::BlockSlice, i::Block{1}) = b.block[Int(i)]
139+
_block_reindex(b::Slice, i::Block{1}) = i
140+
141+
@inline Base.view(V::SubArray{<:Any,N,<:AbstractBlockArray,<:NTuple{N,BlockSlices}}, block::Block{N}) where N =
142+
view(parent(V), _block_reindex.(parentindices(V), Block.(block.n))...)
143+
@inline Base.view(V::SubArray{<:Any,N,<:AbstractBlockArray,<:NTuple{N,BlockSlices}}, block::Vararg{Block{1},N}) where N =
144+
view(parent(V), _block_reindex.(parentindices(V), block)...)
145+
@inline Base.view(V::SubArray{<:Any,1,<:AbstractBlockArray,<:Tuple{BlockSlices}}, block::Block{1}) =
146+
view(parent(V), _block_reindex(parentindices(V)[1], block))
147+
148+
149+
137150

138151
function Base.view(A::Adjoint{<:Any,<:BlockArray}, b::Block{2})
139152
k, j = b.n

test/test_blockarrays.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ end
402402
@test stringmime("text/plain",A) == "1×2-blocked 6×9 PseudoBlockMatrix{Int16}:\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0\n 0 0 0 0 │ 0 0 0 0 0"
403403
D = PseudoBlockArray(Diagonal(1:3), [1,2], [2,1])
404404
@test stringmime("text/plain", D) == "2×2-blocked 3×3 $(PseudoBlockMatrix{Int, Diagonal{Int, UnitRange{Int}}, Tuple{BlockedUnitRange{Vector{Int}}, BlockedUnitRange{Vector{Int}}}}):\n 1 ⋅ │ ⋅\n ──────┼───\n ⋅ 2 │ ⋅\n ⋅ ⋅ │ 3"
405+
406+
a = BlockArray{Int}(undef_blocks, [1,2])
407+
@test stringmime("text/plain", a) == "2-blocked 3-element BlockVector{Int64}:\n #undef\n ──────\n #undef\n #undef"
408+
B = BlockArray{Int}(undef_blocks, [1,2], [1,1])
409+
@test stringmime("text/plain", B) == "2×2-blocked 3×2 BlockMatrix{Int64}:\n #undef │ #undef\n ────────┼────────\n #undef │ #undef\n #undef │ #undef"
405410
end
406411

407412
@testset "AbstractVector{Int} blocks" begin

test/test_blockrange.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ using BlockArrays, Test
5151

5252
A = BlockArray(reshape(collect(1:(6*12)),6,12), 1:3, 3:5)
5353
V = view(view(A, Block.(2:3), Block.(1:3)), Block(2), Block(2))
54-
@test parent(V) == A
55-
@test parentindices(V)[1] isa BlockArrays.BlockSlice{Block{1,Int}}
56-
@test parentindices(V)[1].block == Block(3)
57-
@test all(ind -> ind isa BlockArrays.BlockSlice, parentindices(V))
5854
@test V == view(A, Block.(2:3), Block.(1:3))[Block(2,2)] == A[Block(3, 2)]
5955

6056

0 commit comments

Comments
 (0)