Giter VIP home page Giter VIP logo

Comments (4)

mlugg avatar mlugg commented on May 30, 2024 1

Okay, yeah; I think there's a reasonable case to be made for a shrinkCapacity function, maybe something like this:

/// If the capacity exceeds the given limit, resizes the allocated buffer to exactly that limit.
/// Asserts that the given limit is at least the length of the `ArrayList`.
/// Invalidates all element pointers.
pub fn shrinkCapacity(self: *Self, limit: usize) Allocator.Error!void {
    assert(limit >= self.items.len);
    if (self.capacity <= limit) return;
    const allocator = self.allocator;
    if (allocator.resize(self.allocatedSlice(), limit)) {
        self.capacity = limit;
        return;
    }
    const new_memory = try allocator.alignedAlloc(T, alignment, limit);
    @memcpy(new_memory[0..self.items.len], self.items);
    allocator.free(self.items);
    self.items = new_memory;
    self.capacity = limit;
}

from zig.

mlugg avatar mlugg commented on May 30, 2024

This is working as designed. If you want to see this behavior changed, that is a proposal. Realistically, if we want to have this function, it should be under a different name, since the function you are describing does not shrink the ArrayList.


Could I ask what scenario have you encountered where you have an ArrayList which:

  • Has current length X
  • Needs capacity Y > X
  • Should not have a capacity greater than Y?

from zig.

Jarred-Sumner avatar Jarred-Sumner commented on May 30, 2024

Could I ask what scenario have you encountered where you have an ArrayList

Here is the motivating commit: oven-sh/bun@57dcdbf#diff-dde34f421656d5d1df29476c8c358985d1b22bae2c505c70e0f3d922e0af9fe5R383-R388

This is in the code in Bun which writes to a pipe or file streaming (for use from JavaScript).

The usecase: you're using an ArrayList as a growable buffer that is reused repeatedly. The ArrayList is a dynamic allocation. At first, it's growing and growing. Later, you finish writing but you don't want to pay the cost of re-allocating to shrink it unless that allocation gets big enough to "matter". But at that point, the capacity of the list might've grown a lot but you might only be using a small part of it.

from zig.

squeek502 avatar squeek502 commented on May 30, 2024

Side note: shrinkCapacity is very close to what toOwnedSlice wants to do as well, so toOwnedSlice could likely be made to use a shrinkCapacity function.

from zig.

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.