Skip to content

Commit ef21782

Browse files
mtfishmanclaude
andauthored
Use type inference for blocktype instead of runtime mapreduce (#255)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b89c568 commit ef21782

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
3-
version = "0.10.35"
3+
version = "0.10.36"
44
authors = ["ITensor developers <support@itensor.org> and contributors"]
55

66
[workspace]

src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ArrayLayouts: ArrayLayouts
22
using BlockArrays: BlockArrays, AbstractBlockVector, Block, BlockIndex, BlockRange,
3-
BlockSlice, BlockVector, BlockedUnitRange, BlockedVector, block, blockcheckbounds,
4-
blockisequal, blocklength, blocklengths, blocks, findblockindex
3+
BlockSlice, BlockVector, BlockedUnitRange, BlockedVector, BlocksView, block,
4+
blockcheckbounds, blockisequal, blocklength, blocklengths, blocks, findblockindex
55
using FunctionImplementations: FunctionImplementations, permuteddims, zero!
66
using LinearAlgebra: Adjoint, Transpose
77
using SparseArraysBase: AbstractSparseArrayImplementationStyle, eachstoredindex,
@@ -75,13 +75,13 @@ julia> length(blocks(BlockedVector{Float64}(randn(0))))
7575
1
7676
```
7777
=#
78-
function blocktype(a::AbstractArray)
79-
if isempty(blocks(a))
80-
error("`blocktype` can't be determined if `isempty(blocks(a))`.")
81-
end
82-
return mapreduce(typeof, promote_type, blocks(a))
78+
eltype_inferred(a::AbstractArray{T}) where {T} = T
79+
function eltype_inferred(a::BlocksView{<:Any, N}) where {N}
80+
return Base.promote_op(getindex, typeof(a), ntuple(Returns(Int), Val(N))...)
8381
end
8482

83+
blocktype(a::AbstractArray) = eltype_inferred(blocks(a))
84+
8585
using BlockArrays: BlockArray
8686
blockstype(::Type{<:BlockArray{<:Any, <:Any, B}}) where {B} = B
8787
blockstype(a::BlockArray) = blockstype(typeof(a))
@@ -526,5 +526,4 @@ function blocks_blocksparse(
526526
return @view blocks(parent(a))[map(to_blocks_indices, parentindices(a))...]
527527
end
528528

529-
using BlockArrays: BlocksView
530529
SparseArraysBase.storedlength(a::BlocksView) = length(a)

test/test_basics.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ arrayts = (Array, JLArray)
174174
@test (@constinferred blockstype(a)) <: BlockArrays.BlocksView{elt, 2}
175175
# TODO: This is difficult to determine just from type information.
176176
@test_broken blockstype(typeof(a)) <: BlockArrays.BlocksView{elt, 2}
177-
@test (@constinferred blocktype(a)) <: SubArray{elt, 2, arrayt{elt, 2}}
178-
# TODO: This is difficult to determine just from type information.
179-
@test_broken blocktype(typeof(a)) <: SubArray{elt, 2, arrayt{elt, 2}}
177+
@test blocktype(a) <: AbstractMatrix{elt}
178+
@test_broken blocktype(typeof(a)) <: AbstractMatrix{elt}
180179

181180
# sparsemortar
182181
for ax in (

0 commit comments

Comments
 (0)