3 ms·
All the functions mentioned above, even the cpp one, will reserve atleast the number of elements given to resize() or resize_exact(), but may reserve more than
by arka2147483647 1y ago
All the functions mentioned above, even the cpp one, will reserve atleast the number of elements given to resize() or resize_exact(), but may reserve more than that.
After some pondering, and reading the rust documentation, I came to the conclusion that te difference is this:
reserve() will grow the underlaying memory area to the next increment, or more than one increment, while
reserve_exact() will only grow the underlaying memory area to the next increment, but no more than that.
Eg, if grow strategy is powers of two, and we are at pow(2), then reserve() may skip from pow(2) to pow(4), but reserve_exact() would be constrained to pow(3) as the next increment.
Or so i read the documentation. Hopefully someone can confirm?
- swiftcoder 1y ago> even the cpp one, will reserve atleast the number of elements given The C++ one, however, will not reserve more than you ask for (in the case that you reserve greater than the current capacity). It's an exact reservation in the rust sense. > reserve() will grow the underlaying memory area to the next increment, or more than one increment, while reserve_exact() will only grow the underlaying memory area to the next increment, but no more than that No, not quite. Reserve will request as many increments as it needs, and reserve_exact will request the exact total capacity it needs. Where the docs get confusing, is that the allocator also has a say here. In either case, if you ask for 21 items, and the allocator decides it prefers to give you a full page of memory that can contain, say, 32 items... then the Vec will use all the capacity returned by the allocator.
- kbolino 1y agoAs far as I can tell, in the current implementation, reserve_exact is indeed exact. The only situation in which the capacity after calling reserve_exact will not equal length + additional is when it was already greater than that. Even if the allocator gives more than the requested amount of memory, the excess is ignored for the purposes of Vec's capacity: https://github.com/rust-lang/rust/blob/4b57d8154a6a74d2514cd3a674c1f29a588f224a/library/alloc/src/raw_vec/mod.rs#L728 https://github.com/rust-lang/rust/blob/4b57d8154a6a74d2514cd... Of course, this can change in the future; in particular, the entire allocator API is still unstable and likely won't stabilize any time soon.
- tialaramex 1y agoMaybe more interestingly, line 659, slightly above that, explains that we know we got [u8] but today the ordinary Rust allocator promises capacity is correct, so we just ignore the length of that slice. We could, as that comment suggests, check the slice and see if there's enough room for more than our chosen capacity. We could also debug_assert that it's not less room, 'cos the Allocator promised it would be big enough. I dunno if that's worthwhile.
- arka2147483647 1y agohttps://en.cppreference.com/w/cpp/container/vector/reserve.html https://en.cppreference.com/w/cpp/container/vector/reserve.h... says > Increase the capacity of the vector (the total number of elements that the vector can hold without requiring reallocation) to a value that's greater or equal to new_cap. I belive that the behaviour of reserve() is implementation defined.
- tialaramex 1y agoBecause there's only a single function here, it has to either be Vec::reserve or Vec::reserve_exact If you don't offer Vec::reserve_exact then people who needed that run out of RAM and will dub your stdlib garbage. If you don't offer Vec::reserve as we've seen C++ programmers will say "Skill issue" whenever a noob gets awful performance as a result. So, it's an easy choice.
- mandarax8 1y agoThat said MSVC,GCC and clang all implement it to allocate an exact value.
- vlovich123 1y ago> In either case, if you ask for 21 items, and the allocator decides it prefers to give you a full page of memory that can contain, say, 32 items... then the Vec will use all the capacity returned by the allocator. It would be nice if this were true but AFAIK the memory allocator interface is busted - Rust inherits the malloc-style from C/C++ which doesn’t permit the allocator to tell the application “you asked for 128 bytes but I gave you an allocation for 256”. The alloc method just returns a naked u8 pointer.
- tialaramex 1y agoThe global allocator GlobalAlloc::alloc method does indeed return a naked pointer But the (not yet stable) Allocator::allocate returns Result<NonNull<[u8]>, AllocError> --- that is, either a slice of bytes OR a failure. Vec actually relies on Allocator not GlobalAlloc (it's part of the standard library so it's allowed to use unstable features) So that interface is allowed to say you asked for 128 bytes but here's 256. Or, more likely, you asked for 940 bytes, but here's 1024. So if you were trying to make a Vec<TwentyByteThing> and Vec::with_capacity(47) it would be practical to adjust this so that when the allocator has 1024 bytes available but not 940 we get back a Vec with capacity 51 not 47.
- deleted 1y ago[deleted]
- vlovich123 1y agoYou misread the documentation. Reserve-exact is precisely that - the growth strategy is ignored and you are ensured that at least that many more elements can be inserted without a reallocation. Eg reserve_exact(100) on an empty Vec allocates space for 100 elements. By contrast reserve will allocate space for the extra elements following the growth strategy. If you reserve(100) on an empty Vec the allocation will be able to actually insert 128 (assuming the growth strategy is pow(n))
- tialaramex 1y agoActually that's not quite correct. Vec::reserve(100) on an empty Vec will give you capacity 100, not 128 even though our amortization is indeed doubling. The rules go roughly like this, suppose length is L, present capacity is C, reserve(N): 1. L + N < C ? Enough capacity already, we're done, return 2. L + N <= C * 2 ? Ordinary doubling, grow to capacity C * 2 3. Otherwise, try to grow to L + N This means we can grow any amount more quickly than the amortized growth strategy or at the same speed - but never less quickly. We can go 100, 250, 600, 1300 and we can go 100, 200, 400, 800, 1600 - but we can''t do 100, 150, 200, 250, 300, 350, 400, 450, 500...