Comments (4)
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.
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.
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.
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)
- change std.io.BufferedReader and std.io.BufferedWriter to accept the buffer as a runtime parameter, and use AnyReader / AnyWriter
- Make trailing whitespace at the end of multiline strings an error HOT 1
- Failed to build on ObjCopyStep using cwd_relative
- rsqrt as builtin HOT 5
- Should build.zig have a programmatic interface and support the use case of acting as the low level build system of another programming language. If yes, how?
- Undefined usage before assignment not detected if variable is mutable and assignment is deferred HOT 1
- translate-c generates incorrect alignments for struct fields
- Store Build relative information in LazyPath HOT 3
- Printing functions incorrectly round float values with precision 0 HOT 1
- `comptime` not required for param with comptime-only type in function called at runtime HOT 2
- Proposal: Substitute Char Literals With `utf8Decode` HOT 4
- mem.Allocator struct field gets corrupted by presence of neighbouring time.Instant field in debug builds HOT 2
- introduce pointer stability safety locks to array lists HOT 1
- introduce pointer stability safety locks to MultiArrayList
- add stack traces to pointer stability safety locks
- x86 FPATAN instruction
- llvm error using `@reduce` with optimized float mode
- Segfault when using curly braces instead of parenthesis in function call HOT 5
- [Regression] Opening absolute paths no longer works with WASI HOT 6
- suggestion: unitify the object create/destory/initialize/deinitialize methods
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 zig.