Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector (and others) .reserve and .reserve_at_least naming is poor #11949

Closed
huonw opened this issue Jan 31, 2014 · 0 comments · Fixed by #11951
Closed

Vector (and others) .reserve and .reserve_at_least naming is poor #11949

huonw opened this issue Jan 31, 2014 · 0 comments · Fixed by #11951
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@huonw
Copy link
Member

huonw commented Jan 31, 2014

The behaviour of the .reserve_at_least is much better asymptotically, since calling .reserve in a loop will cause quadratic behaviour. We should be using the short method name to encourage the faster behaviour.

.reserve(n) allocates exactly enough capacity to store n elements (if there isn't enough space already), while .reserve_at_least(n) will ensure that there is enough space, but overallocate to ensure that reallocations don't occur too often (i.e. increase the allocation size exponentially).

e.g. imagine a protocol which consists of a sets of messages, where set block as an initial message with the count for that set:

let received = ~[];
let mut total_messages = 0;
while listener.more_messages() {
    let next_count = listener.recv();
    total_messages += next_count;

    // we know how many we're going to have total, so
    // might as well allocate it up-front.
    received.reserve(total_messages);

    for _ in range(0, next_count) {
        received.push(listener.recv());
    }
}

using (the current) .reserve causes every single set to do a reallocation and memcpy (i.e. O(n^2) behaviour), while using .reserve_at_least would mean the reallocation and memcpy only happens logarithmically often, preserving O(n) asymptotics.

(For vector specifically the above should really be using .reserve_additional, but other types like ~str and extra::{priority_queue, ring_buf} suffer the same problem.)

bors added a commit that referenced this issue Feb 4, 2014
Changes in std::{str,vec,hashmap} and extra::{priority_queue,ringbuf}.
Fixes #11949
@bors bors closed this as completed in 65f3578 Feb 4, 2014
huonw added a commit to huonw/rust that referenced this issue Feb 23, 2014
`.reserve_exact` can cause pathological O(n^2) behaviour, so providing a
`.reserve` that ensures that capacity doubles (if you step 1, 2, ..., n)
is more efficient.

cc rust-lang#11949
alexcrichton pushed a commit to alexcrichton/rust that referenced this issue Feb 25, 2014
`.reserve_exact` can cause pathological O(n^2) behaviour, so providing a
`.reserve` that ensures that capacity doubles (if you step 1, 2, ..., n)
is more efficient.

cc rust-lang#11949
flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 16, 2023
Add `blyxyas` to `users_on_vacation`

I have a surgery this Wednesday, so I won't be able to review anything from that day up to (at least) Dec. 22, I'll open another PR by then.

I'm not sure if `users_on_vacation` will work here, if it doesn't work, I'll just remove myself from the reviewer rotation

changelog:none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant