Skip to content

Commit 880c366

Browse files
bkaminssimeonschaub
authored andcommitted
improve speed of allunique (JuliaLang#37208)
1 parent 216c4ed commit 880c366

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

base/set.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,26 @@ false
382382
```
383383
"""
384384
function allunique(C)
385-
seen = Set{eltype(C)}()
386-
for x in C
387-
if in(x, seen)
388-
return false
389-
else
390-
push!(seen, x)
385+
seen = Dict{eltype(C), Nothing}()
386+
x = iterate(C)
387+
if haslength(C) && length(C) > 1000
388+
for i in OneTo(1000)
389+
v, s = x
390+
idx = ht_keyindex2!(seen, v)
391+
idx > 0 && return false
392+
_setindex!(seen, nothing, v, -idx)
393+
x = iterate(C, s)
391394
end
395+
sizehint!(seen, length(C))
396+
end
397+
while x !== nothing
398+
v, s = x
399+
idx = ht_keyindex2!(seen, v)
400+
idx > 0 && return false
401+
_setindex!(seen, nothing, v, -idx)
402+
x = iterate(C, s)
392403
end
393-
true
404+
return true
394405
end
395406

396407
allunique(::Union{AbstractSet,AbstractDict}) = true

0 commit comments

Comments
 (0)