Giter Site home page Giter Site logo

Comments (2)

glbrntt avatar glbrntt commented on June 11, 2024 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 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.

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.

lorentey avatar lorentey commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.