Skip to content

Make unique(f, itr) and unique!(f, itr) faster #30286

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

Merged
merged 2 commits into from
Dec 9, 2018
Merged
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
75 changes: 58 additions & 17 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,39 @@ julia> unique(x -> x^2, [1, -1, 3, -3, 4])
"""
function unique(f, C)
out = Vector{eltype(C)}()
seen = Set()
for x in C

s = iterate(C)
if s === nothing
return out
end
(x, i) = s
y = f(x)
seen = Set{typeof(y)}()
push!(seen, y)
push!(out, x)

return _unique!(f, out, C, seen, i)
end

function _unique!(f, out::AbstractVector, C, seen::Set, i)
s = iterate(C, i)
while s !== nothing
(x, i) = s
y = f(x)
if !in(y, seen)
push!(seen, y)
if y ∉ seen
push!(out, x)
if y isa eltype(seen)
push!(seen, y)
else
Copy link
Contributor

@chethega chethega Dec 6, 2018

Choose a reason for hiding this comment

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

Can we do

else
    TT = promote_typejoin(eltype(seen), typeof(y))
    if eltype(seen) === TT
        push!(seen, y)
        else
             seen2 = ...
        end
end

Idea is that we don't grow the stack if f is e.g. type-unstable between Float32 and Float64.

Copy link
Contributor

Choose a reason for hiding this comment

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

Eh, maybe disregard. I misunderstoof promote_typejoin, which apparently doesn't do that. Point remains, do we want collect-style widening here (integer and float merge to Float64) or do we want exact widening as you did?

Copy link
Member

Choose a reason for hiding this comment

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

Wait, is this calling _unique! recursively each time a new value is encountered?! @chethega is right that this is bad, a call should only happen when encountering a new type.

Copy link
Member Author

Choose a reason for hiding this comment

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

No - it simply pushes if the value is of type eltype of seen. If not, and only if not, then it constructs seen2 and makes a new call.

seen2 = convert(Set{promote_typejoin(eltype(seen), typeof(y))}, seen)
push!(seen2, y)
return _unique!(f, out, C, seen2, i)
end
end
s = iterate(C, i)
end
out

return out
end

"""
Expand Down Expand Up @@ -205,22 +229,39 @@ julia> unique!(iseven, [2, 3, 5, 7, 9])
```
"""
function unique!(f, A::AbstractVector)
seen = Set()
idxs = eachindex(A)
y = iterate(idxs)
count = 0
for x in A
t = f(x)
if t ∉ seen
push!(seen,t)
count += 1
A[y[1]] = x
y = iterate(idxs, y[2])
if length(A) <= 1
return A
end

i = firstindex(A)
x = @inbounds A[i]
y = f(x)
seen = Set{typeof(y)}()
push!(seen, y)
return _unique!(f, A, seen, i, i+1)
end

function _unique!(f, A::AbstractVector, seen::Set, current::Integer, i::Integer)
while i <= lastindex(A)
x = @inbounds A[i]
y = f(x)
if y ∉ seen
current += 1
@inbounds A[current] = x
if y isa eltype(seen)
push!(seen, y)
else
seen2 = convert(Set{promote_typejoin(eltype(seen), typeof(y))}, seen)
push!(seen2, y)
return _unique!(f, A, seen2, current, i+1)
end
end
i += 1
end
resize!(A, count)
return resize!(A, current - firstindex(A) + 1)
end


# If A is not grouped, then we will need to keep track of all of the elements that we have
# seen so far.
_unique!(A::AbstractVector) = unique!(identity, A::AbstractVector)
Expand Down