mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
[DynamicArray] Reduce growth factor to 1.5
This commit is contained in:
2 files changed
+13
-7
No files matched your search
@@ -261,7 +261,10 @@ extension DynamicArray where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
internal func _growDynamicArrayCapacity(_ capacity: Int) -> Int {
|
||||
2 * capacity
|
||||
// A growth factor of 1.5 seems like a reasonable compromise between
|
||||
// over-allocating memory and wasting cycles on repeatedly resizing storage.
|
||||
let c = (3 &* UInt(bitPattern: capacity) &+ 1) / 2
|
||||
return Int(bitPattern: c)
|
||||
}
|
||||
|
||||
extension DynamicArray where Element: ~Copyable {
|
||||
|
||||
@@ -360,7 +360,10 @@ class DynamicArrayTests: CollectionTestCase {
|
||||
a.edit { span in
|
||||
expectEqual(span.capacity, layout.capacity)
|
||||
expectEqual(span.count, layout.count)
|
||||
span.removeAll()
|
||||
if layout.capacity > 0 {
|
||||
// FIXME: OutputSpan.removeAll crashes when empty in some 6.2 snapshots (rdar://158440246)
|
||||
span.removeAll()
|
||||
}
|
||||
expectEqual(tracker.instances, 0)
|
||||
}
|
||||
expectEqual(a.count, 0)
|
||||
@@ -582,11 +585,11 @@ class DynamicArrayTests: CollectionTestCase {
|
||||
array.append(tracker.instance(for: 1))
|
||||
expectEqual(array.capacity, 2)
|
||||
array.append(tracker.instance(for: 2))
|
||||
expectEqual(array.capacity, 4)
|
||||
expectEqual(array.capacity, 3)
|
||||
array.append(tracker.instance(for: 3))
|
||||
expectEqual(array.capacity, 4)
|
||||
expectEqual(array.capacity, 5)
|
||||
array.append(tracker.instance(for: 4))
|
||||
expectEqual(array.capacity, 8)
|
||||
expectEqual(array.capacity, 5)
|
||||
array.append(tracker.instance(for: 5))
|
||||
expectEqual(array.capacity, 8)
|
||||
array.append(tracker.instance(for: 6))
|
||||
@@ -594,14 +597,14 @@ class DynamicArrayTests: CollectionTestCase {
|
||||
array.append(tracker.instance(for: 7))
|
||||
expectEqual(array.capacity, 8)
|
||||
array.append(tracker.instance(for: 8))
|
||||
expectEqual(array.capacity, 16)
|
||||
expectEqual(array.capacity, 12)
|
||||
|
||||
for i in 9 ..< 100 {
|
||||
array.append(tracker.instance(for: i))
|
||||
}
|
||||
expectEqual(tracker.instances, 100)
|
||||
expectEqual(array.count, 100)
|
||||
expectEqual(array.capacity, 128)
|
||||
expectEqual(array.capacity, 140)
|
||||
|
||||
do {
|
||||
let additions = RigidArray(capacity: 300) { span in
|
||||
|
||||
Reference in new issue
Block a user