Comments (2)
Yes, I very intentionally omitted
Deque.capacity
, until I see a valid use case of it.All code I've seen that tries to make decisions about shrinking storage based on
capacity
is overly sensitive to malloc behavior.Deque
is usingmalloc_size
to make use of every byte that it managed to allocate -- and if it happens to be given enough "free" space to put its capacity beyond some shrinking threshold naively implemented by Deque's client, then every operation may trigger the shrinkage, which would be quite bad.
Oh I see, that makes sense. Thanks for shedding some light on why capacity
was omitted 🙂.
Given this, I'd prefer to figure out a more direct way to detect and recover from temporary allocation spikes.
I generally dislike the idea of having the core resizing logic live in client code -- it ought to be part of the container implementation, as it needs to evolve with it. Making Deque automatically shrink itself is one way to achieve this; another (possibly less disruptive) idea would be to expose an explicit operation to shrink the container to be near a certain target size.
To do this, I think
Deque
would need to either give up on using malloc_size, or it would need to keep track of how much extra space malloc gave us. (Either of these would be doable.)I'm very much open to adding direct support for shrinking. (PRs are welcome, if you have time to experiment!) I'd need a bit more convincing to understand why exposing a public
capacity
would be a good idea. 😉
Yeah, that seems absolutely fair, I'm more interested in having a way to shrink the storage. The lack of capacity
only stops us reimplementing CircularBuffer
in terms of Deque
which isn't an issue as we can just switch to Deque
in the next major version and remove CircularBuffer
. We tend to reach for Deque
in most places now anyway.
I'll try to find some time to experiment with the less disruptive explicit shrink operation.
from swift-collections.
Yes, I very intentionally omitted Deque.capacity
, until I see a valid use case of it.
All code I've seen that tries to make decisions about shrinking storage based on capacity
is overly sensitive to malloc behavior. Deque
is using malloc_size
to make use of every byte that it managed to allocate -- and if it happens to be given enough "free" space to put its capacity beyond some shrinking threshold naively implemented by Deque's client, then every operation may trigger the shrinkage, which would be quite bad.
The example code in the discussion you linked to was especially alarming to me:
self.buffers.removeAll(keepingCapacity: self.buffers.capacity < 16) // don't grow too much
16 seemed like a rather low value -- depending on the Element type and the whims of the system allocator, malloc may sometimes give us 16 items' worth of bytes even if we only ask for just a couple of them.
Given this, I'd prefer to figure out a more direct way to detect and recover from temporary allocation spikes.
I generally dislike the idea of having the core resizing logic live in client code -- it ought to be part of the container implementation, as it needs to evolve with it. Making Deque automatically shrink itself is one way to achieve this; another (possibly less disruptive) idea would be to expose an explicit operation to shrink the container to be near a certain target size.
To do this, I think Deque
would need to either give up on using malloc_size, or it would need to keep track of how much extra space malloc gave us. (Either of these would be doable.)
I'm very much open to adding direct support for shrinking. (PRs are welcome, if you have time to experiment!) I'd need a bit more convincing to understand why exposing a public capacity
would be a good idea. 😉
from swift-collections.
Related Issues (20)
- Support fixed-size `Deque` HOT 2
- OrderedDictionary decoding. HOT 1
- OrderedDictionary insert(at:) HOT 1
- OrderedDictionary `updateValue` autoclosure for insertion index
- Failed to resolve dependencies Dependencies could not be resolved because no versions HOT 1
- Ship Sendable annotations in 1.0.6 HOT 1
- `Heap` documentation does not explain what a heap is
- `Heap.insert(contentsOf:)` is quasilinear instead of linear complexity
- OrderedSet `append(contentsOf:)` with insertion check result/results
- OrderedDictionary.values and OrderedSet Performance Problems when SwiftUI.List is paired with SwiftUI.NavigationStack HOT 6
- Ship release 1.0.6 HOT 11
- Ship release 1.1.0 HOT 19
- BitArray's bitwise operations should allow operating on slices HOT 2
- Version 1.1.0 fails to compile HOT 11
- Add Privacy Manifest HOT 7
- The bundle at path Payload/MyApp.app/Frameworks/_CollectionsUtilities.framework has an invalid CFBundleIdentifier '_CollectionsUtilities' HOT 13
- Incorrect calculating offset for _BTree.findAnyIndex(forKey: Key)
- [1.1] Randomized test failure on CI
- Please Add Support for Multiset HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-collections.