Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] better performance in collect_similar #200

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ function Base.collect(itr::DiskGenerator{<:AbstractArray{<:Any,N}}) where {N}
return dest
end

# Warning: this is not public API!
function Base.collect_similar(A::AbstractArray, itr::DiskGenerator{<:AbstractArray{<:Any,N}}) where {N}
input = itr.iter # this is known to be an array
y = iterate(itr)
shp = axes(input)
if y === nothing
et = Base.@default_eltype(itr)
return similar(A, et, shp)
end
v1, st = y
dest = similar(A, typeof(v1), shp)# TODO: should this be `Base.return_type(itr.f, Tuple{eltype(input)})`?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Base.promote_op is preferred now

i = y
# If the array is chunked, read each chunk and apply the function
# via broadcasting.
if DiskArrays.haschunks(input) isa DiskArrays.Chunked
chunks = eachchunk(input)
for chunk_inds in chunks
dest[chunk_inds...] .= itr.f.(input[chunk_inds...])
end
else # iterate as normal array
for I in eachindex(itr.iter)
if i isa Nothing # Mainly to keep JET clean
error(
"Should not be reached: iterator is shorter than its `eachindex` iterator"
)
else
dest[I] = first(i)
i = iterate(itr, last(i))
end
end
end
return dest

end

macro implement_generator(t)
t = esc(t)
quote
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -953,3 +953,17 @@ end
@test getindex_count(A) == 0
end

@testset "Map over indices correctly" begin
# This is a regression test for issue #144
# `map` should always work over the correct indices,
# especially since we overload generators to `DiskArrayGenerator`.

data = [i+j for i in 1:200, j in 1:100]
da = AccessCountDiskArray(data, chunksize=(10,10))
@test map(identity, da) == data
@test all(map(identity, da) .== data)

# Make sure that type inference works
@inferred Matrix{Int} map(identity, da)
@inferred Matrix{Float64} map(x -> x * 5.0, da)
end