Skip to content

Commit

Permalink
add fast path
Browse files Browse the repository at this point in the history
  • Loading branch information
nsajko committed Jan 29, 2025
1 parent fa15a14 commit 9aeff19
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/collect_as.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ function throw_bottom_type()
throw(ArgumentError("`Union{}` not expected"))
end

function eltype_is_known(::Type{Storage}) where {S, Storage <: AbstractVector{S}}
true

Check warning on line 8 in src/collect_as.jl

View check run for this annotation

Codecov / codecov/patch

src/collect_as.jl#L7-L8

Added lines #L7 - L8 were not covered by tests
end
function eltype_is_known(::Type{Storage}) where {Storage <: AbstractVector}
false

Check warning on line 11 in src/collect_as.jl

View check run for this annotation

Codecov / codecov/patch

src/collect_as.jl#L10-L11

Added lines #L10 - L11 were not covered by tests
end

function collect_as_storage_type_helper(::Type{Storage}, ::Type) where {S, Storage <: AbstractVector{S}}
Storage
end
Expand All @@ -20,6 +27,12 @@ function fsv_type_from_underlying_storage_type(::Type{V}) where {E, V <: DenseVe
FixedSizeVector{E, V}
end

function make_fsv_from_collection_with_length(::Type{V}, elems) where {V <: DenseVector}
stor = collect_as_storage_type_helper(V, eltype(elems))
ret_type = fsv_type_from_underlying_storage_type(stor)
make_abstract_vector_from_collection_with_length(ret_type, elems)
end

function make_vector_from_tuple(::Type{V}, elems::Tuple) where {V <: DenseVector}
stor = collect_as_storage_type_helper(V, eltype(elems))
ret_type = Vector{eltype(stor)}
Expand Down Expand Up @@ -159,7 +172,14 @@ function collect_as(::Type{T}, iterator) where {T<:FixedSizeArray}
end
mem = parent_type_with_default(T)
output_dimension_count = checked_dimension_count_of(T, size_class)
fsv = collect_as_fsv(mem, iterator)
fsv = if (
(size_class isa Union{Base.HasLength, Base.HasShape}) &&
(eltype_is_known(mem) || isconcretetype(eltype(iterator)))
)
make_fsv_from_collection_with_length(mem, iterator) # fast path
else
collect_as_fsv(mem, iterator)
end
if isone(output_dimension_count)
fsv # `size(iterator)` may throw in this branch
else
Expand Down

0 comments on commit 9aeff19

Please sign in to comment.