-
Notifications
You must be signed in to change notification settings - Fork 308
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
Let deque have its capacity shrunk #316
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -374,5 +374,40 @@ final class DequeTests: CollectionTestCase { | |
} | ||
} | ||
|
||
func test_shrinkCapacityWhenEmpty() { | ||
withEvery("capacity", in: [0, 1, 2, 5, 10, 50, 100]) { capacity in | ||
var deque = Deque<Int>(minimumCapacity: capacity) | ||
|
||
XCTAssertGreaterThanOrEqual(deque._capacity, capacity) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Please use the custom |
||
XCTAssertLessThanOrEqual(deque._requestedCapacity, capacity) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the |
||
|
||
let actual = deque._capacity | ||
let reducedCapacity = deque._requestedCapacity / 4 | ||
deque.shrinkCapacity(reducedCapacity) | ||
|
||
print(actual, deque._capacity) | ||
// Shrinkage isn't guaranteed. | ||
XCTAssertGreaterThanOrEqual(deque._capacity, reducedCapacity) | ||
XCTAssertLessThanOrEqual(deque._requestedCapacity, reducedCapacity) | ||
} | ||
} | ||
|
||
func test_shrinkCapacityWhenMoreElementsThanTarget() { | ||
withEvery("count", in: [0, 1, 2, 5, 10, 50, 100]) { count in | ||
var deque = Deque(repeating: 0, count: count) | ||
let capacity = deque._capacity | ||
deque.shrinkCapacity(0) | ||
XCTAssertEqual(deque._capacity, capacity) | ||
} | ||
} | ||
|
||
func test_shrinkCapacityWhenTargetIsLargerThanCurrent() { | ||
withEvery("capacity", in: [0, 1, 2, 5, 10, 50, 100]) { capacity in | ||
var deque = Deque<Int>(minimumCapacity: capacity) | ||
let capacity = deque._capacity | ||
deque.shrinkCapacity(capacity + 1) | ||
XCTAssertEqual(deque._capacity, capacity) | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this raises a question: for a
Deque
with a handful of elements that is backed by a humungous buffer, wouldn't we want to allow the buffer to get shrunk to fit by callingshrink(targetCapacity: count)
(orshrink(targetCapacity: 0)
)?I suppose that instead of merely comparing directly against the count and capacity, we would want to only allow reallocation when the resulting capacity delta is "large enough" -- whatever that means. (An absolute cutoff? A percentage of existing capacity? Perhaps a combination of both. Probably the percentage value should be somewhat correlated with the regular growth factor; e.g. it would be wasteful to reallocate storage to 1000 items if it is currently only at 1001...) Something along the lines of this condition, maybe?
The numbers above are rather arbitrary and if we go this way, finding the right ones will require building a bit of a model.