Skip to content

Commit 9d1d0e0

Browse files
Merge pull request #526 from ChrisRackauckas-Claude/fix/type-stability-end-indexing
Fix type instability for `vec[1, end]` indexing
2 parents c98f542 + 810b624 commit 9d1d0e0

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/vector_of_array.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,37 @@ function Base.:(:)(start::RaggedEnd, step::Integer, stop::Integer)
576576
end
577577
Base.broadcastable(x::RaggedRange) = Ref(x)
578578

579+
# Specialized method for type stability when last index is RaggedEnd with dim=0 (resolved column index)
580+
# This handles the common case: vec[i, end] where end -> RaggedEnd(0, lastindex)
581+
Base.@propagate_inbounds function Base.getindex(
582+
A::AbstractVectorOfArray, i::Int, re::RaggedEnd
583+
)
584+
if re.dim == 0
585+
# Sentinel case: RaggedEnd(0, offset) means offset is the resolved column index
586+
return A.u[re.offset][i]
587+
else
588+
# Non-sentinel case: resolve the ragged index for the last column
589+
col = lastindex(A.u)
590+
resolved_idx = lastindex(A.u[col], re.dim) + re.offset
591+
return A.u[col][i, resolved_idx]
592+
end
593+
end
594+
595+
# Specialized method for type stability when first index is RaggedEnd (row dimension)
596+
# This handles the common case: vec[end, col] where end -> RaggedEnd(1, 0)
597+
Base.@propagate_inbounds function Base.getindex(
598+
A::AbstractVectorOfArray, re::RaggedEnd, col::Int
599+
)
600+
if re.dim == 0
601+
# Sentinel case: RaggedEnd(0, offset) means offset is a plain index
602+
return A.u[col][re.offset]
603+
else
604+
# Non-sentinel case: resolve the ragged index for the given column
605+
resolved_idx = lastindex(A.u[col], re.dim) + re.offset
606+
return A.u[col][resolved_idx]
607+
end
608+
end
609+
579610
@inline function _is_ragged_dim(VA::AbstractVectorOfArray, d::Integer)
580611
length(VA.u) <= 1 && return false
581612
first_size = size(VA.u[1], d)

test/interface_tests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ push!(testda, [-1, -2, -3, -4])
6666
@inferred sum(testva)
6767
@inferred sum(VectorOfArray([VectorOfArray([zeros(4, 4)])]))
6868
@inferred mapreduce(string, *, testva)
69+
# Type stability for `end` indexing (issue #525)
70+
testva_end = VectorOfArray([fill(2.0, 2) for i in 1:10])
71+
# Use lastindex directly since `end` doesn't work in SafeTestsets
72+
last_col = lastindex(testva_end, 2)
73+
@inferred testva_end[1, last_col]
74+
@test testva_end[1, last_col] == 2.0
75+
last_col = lastindex(testva_end)
76+
@inferred testva_end[1, last_col]
77+
@test testva_end[1, last_col] == 2.0
78+
last_row = lastindex(testva_end, 1)
79+
@inferred testva_end[last_row, 1]
80+
@test testva_end[last_row, 1] == 2.0
6981

7082
# mapreduce
7183
testva = VectorOfArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

0 commit comments

Comments
 (0)