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

fix performance #39

Merged
merged 4 commits into from
Dec 10, 2024
Merged
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
42 changes: 37 additions & 5 deletions src/InvertedIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,49 @@ end
InvertedIndexIterator(skips, picks) = InvertedIndexIterator{eltype(picks), typeof(skips), typeof(picks)}(skips, picks)
Base.size(III::InvertedIndexIterator) = (length(III.picks) - length(III.skips),)

@inline Base.iterate(I::InvertedIndexIterator) = iterate(I, (iterate(I.skips), iterate(I.picks)))
Base.iterate(I::InvertedIndexIterator, ::Tuple{Any, Nothing}) = nothing
@inline function Base.iterate(I::InvertedIndexIterator, (skipitr, pickitr))
@inline function Base.iterate(I::InvertedIndexIterator)
skipitr = iterate(I.skips)
pickitr = iterate(I.picks)
pickitr === nothing && return nothing
while should_skip(skipitr, pickitr)
skipitr = iterate(I.skips, skipitr[2])
pickitr = iterate(I.picks, pickitr[2])
pickitr === nothing && return nothing
end
# This is a little silly, but splitting the tuple here allows inference to normalize
# Tuple{Union{Nothing, Tuple}, Tuple} to Union{Tuple{Nothing, Tuple}, Tuple{Tuple, Tuple}}
Comment on lines +113 to +114
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, have you noticed this across all supported versions or just in e.g. 1.11?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The type stability unit tests fail without this manual union-split ternary, but pass with it. It's the difference between putting a type-unstable value in a tuple v.s. returning two different tuples... and those fundamentals hold true going back to 1.0:

▶ julia +1.0 -q
julia> function f()
           x = rand([nothing, 1.0])
           return (x,)
       end
f (generic function with 1 method)

julia> @code_warntype f()
Body::Tuple{Union{Nothing, Float64}}
# ...

julia> function g()
           x = rand([nothing, 1.0])
           return x===nothing ? (nothing,) : (x,)
       end
g (generic function with 1 method)

julia> @code_warntype g()
Body::Union{Tuple{Nothing}, Tuple{Float64}}

Copy link
Member

Choose a reason for hiding this comment

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

Oh interesting, I don't think I knew that. Thanks, I appreciate the explanation!

return skipitr === nothing ?
(pickitr[1], (nothing, pickitr[2])) :
(pickitr[1], (skipitr, pickitr[2]))
end
@inline function Base.iterate(I::InvertedIndexIterator, (_, pickstate)::Tuple{Nothing, Any})
pickitr = iterate(I.picks, pickstate)
pickitr === nothing && return nothing
return (pickitr[1], (nothing, pickitr[2]))
end
@inline function Base.iterate(I::InvertedIndexIterator, (skipitr, pickstate)::Tuple)
pickitr = iterate(I.picks, pickstate)
pickitr === nothing && return nothing
while should_skip(skipitr, pickitr)
skipitr = iterate(I.skips, tail(skipitr)...)
pickitr = iterate(I.picks, tail(pickitr)...)
pickitr === nothing && return nothing
end
return (pickitr[1], (skipitr, iterate(I.picks, tail(pickitr)...)))
return skipitr === nothing ?
(pickitr[1], (nothing, pickitr[2])) :
(pickitr[1], (skipitr, pickitr[2]))
end
function Base.collect(III::InvertedIndexIterator{T}) where {T}
!isconcretetype(T) && return [i for i in III] # use widening if T is not concrete
v = Vector{T}(undef, length(III))
i = 0
for elt in III
i += 1
@inbounds v[i] = elt
end
i != length(v) && throw(AssertionError("length of inverted index does not match iterated count"))
return v
end
Base.collect(III::InvertedIndexIterator) = [i for i in III]

should_skip(::Nothing, ::Any) = false
should_skip(s::Tuple, p::Tuple) = _should_skip(s[1], p[1])
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,21 @@ end
@test x isa InvertedIndex{InvertedIndices.NotMultiIndex}
@test_throws ArgumentError v[x]
end

returns(val) = _->val
@testset "type stability" begin
for arr in (
1:5,
reshape(1:5*3, 5, 3),
reshape(1:5*3*7, 5, 3, 7),
reshape(1:5*3*7*11, 5, 3, 7, 11),
)
I = to_indices(arr, (Not(iseven.(arr)),))[1]
@test all(isodd, arr[I])
@test all(isodd, LinearIndices(arr)[I])
@test all(isodd, LinearIndices(arr)[collect(I)])
@allocated(foreach(returns(nothing), I))
@test @allocated(foreach(returns(nothing), I)) == 0
@test @inferred(LinearIndices(arr)[collect(I)]) == vec(filter(!iseven, arr))
end
end
Loading