Skip to content

Commit c94e80a

Browse files
committed
Introduce sizehint!(s, n; shrink = true) to controll shrinkage
1 parent d75a00f commit c94e80a

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

base/abstractdict.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ Dict{Int64, Int64} with 3 entries:
220220
function merge!(d::AbstractDict, others::AbstractDict...)
221221
for other in others
222222
if haslength(d) && haslength(other)
223-
# TODO - do not shrink
224-
sizehint!(d, length(d) + length(other))
223+
sizehint!(d, length(d) + length(other); shrink = false)
225224
end
226225
for (k,v) in other
227226
d[k] = v

base/abstractset.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ max_values(::Type{Bool}) = 2
101101
max_values(::Type{Nothing}) = 1
102102

103103
function union!(s::AbstractSet{T}, itr) where T
104-
haslength(itr) && _sizehint!(s, length(s) + Int(length(itr))::Int; shrink = false)
104+
haslength(itr) && sizehint!(s, length(s) + Int(length(itr))::Int; shrink = false)
105105
for x in itr
106106
push!(s, x)
107107
length(s) == max_values(T) && break

base/array.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ function resize!(a::Vector, nl::Integer)
14411441
end
14421442

14431443
"""
1444-
sizehint!(s, n) -> s
1444+
sizehint!(s, n; shrink = true) -> s
14451445
14461446
Suggest that collection `s` reserve capacity for at least `n` elements. That is, if
14471447
you expect that you're going to have to push a lot of values onto `s`, you can avoid
@@ -1462,10 +1462,13 @@ For types that support `sizehint!`,
14621462
`Base`.
14631463
14641464
3. `empty!` is nearly costless (and O(1)) for types that support this kind of preallocation.
1465+
1466+
4. `shrink` controls if the collection can be shrunk.
14651467
"""
14661468
function sizehint! end
14671469

14681470
function sizehint!(a::Vector, sz::Integer)
1471+
# TODO - controll shrinkage
14691472
len = length(a)
14701473
ref = a.ref
14711474
mem = ref.mem
@@ -1494,7 +1497,7 @@ function sizehint!(a::Vector, sz::Integer)
14941497
end
14951498

14961499
# Fall-back implementation for non-shrinkable collections
1497-
_sizehint!(a, sz; shrink) = sizehint!(a, sz)
1500+
sizehint!(a, sz; shrink) = sizehint!(a, sz)
14981501

14991502
"""
15001503
pop!(collection) -> item

base/dict.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ end
228228
return h
229229
end
230230

231-
function _sizehint!(d::Dict{T}, newsz; shrink = true) where T
231+
function sizehint!(d::Dict{T}, newsz; shrink = true) where T
232232
oldsz = length(d.slots)
233233
# limit new element count to max_values of the key type
234234
newsz = min(max(newsz, length(d)), max_values(T)::Int)
@@ -237,8 +237,6 @@ function _sizehint!(d::Dict{T}, newsz; shrink = true) where T
237237
return (shrink ? newsz == oldsz : newsz <= oldsz) ? d : rehash!(d, newsz)
238238
end
239239

240-
sizehint!(d::Dict{T}, newsz) where T = _sizehint!(d, newsz)
241-
242240
"""
243241
empty!(collection) -> collection
244242
@@ -777,7 +775,7 @@ function map!(f, iter::ValueIterator{<:Dict})
777775
end
778776

779777
function mergewith!(combine, d1::Dict{K, V}, d2::AbstractDict) where {K, V}
780-
haslength(d2) && _sizehint!(d1, length(d1) + length(d2), shrink = false)
778+
haslength(d2) && sizehint!(d1, length(d1) + length(d2), shrink = false)
781779
for (k, v) in d2
782780
i, sh = ht_keyindex2_shorthash!(d1, k)
783781
if i > 0

base/weakkeydict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function _cleanup_locked(h::WeakKeyDict)
8080
return h
8181
end
8282

83-
sizehint!(d::WeakKeyDict, newsz) = sizehint!(d.ht, newsz)
83+
sizehint!(d::WeakKeyDict, newsz; shrink = true) = sizehint!(d.ht, newsz; shrink = shrink)
8484
empty(d::WeakKeyDict, ::Type{K}, ::Type{V}) where {K, V} = WeakKeyDict{K, V}()
8585

8686
IteratorSize(::Type{<:WeakKeyDict}) = SizeUnknown()

test/dict.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,9 +1492,9 @@ end
14921492
sizehint!(d, 10)
14931493
@test length(d.slots) < 100
14941494
sizehint!(d, 1000)
1495-
Base._sizehint!(d, 1; shrink = false)
1495+
sizehint!(d, 1; shrink = false)
14961496
@test length(d.slots) >= 1000
1497-
Base._sizehint!(d, 1; shrink = true)
1497+
sizehint!(d, 1; shrink = true)
14981498
@test length(d.slots) < 1000
14991499
end
15001500

0 commit comments

Comments
 (0)