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

VecDeque::split_off has unexpectedly poor performance characteristics when splitting off from the front. #127281

Open
emilio opened this issue Jul 3, 2024 · 0 comments
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@emilio
Copy link
Contributor

emilio commented Jul 3, 2024

I tried this code:

use std::collections::VecDeque;

fn huge_queue() -> VecDeque<usize> {
    const HUGE: usize = 1000000;
    let mut queue = VecDeque::with_capacity(HUGE);
    for i in 0..HUGE {
        queue.push_back(i);
    }
    queue
}

const CHUNK: usize = 123;
const FAST: bool = false;

fn main() {
    let mut queue = huge_queue();
    if FAST {
        while queue.len() > CHUNK {
            queue.rotate_left(CHUNK);
            let rest = queue.split_off(queue.len() - CHUNK);
            println!("Chunk of {}", rest.len());
        }
    } else {
        while queue.len() > CHUNK {
            let rest = queue.split_off(CHUNK);
            println!("Chunk of {}", queue.len());
            queue = rest;
        }
    }
    println!("Remaining: {}", queue.len());
}

I expected the FAST branch and the else branch to have comparable performance, instead it seems VecDeque::split_off from the front is much slower than expected (as one would expect of e.g. Vec::split_off which needs to shift all the elements).

@emilio emilio added the C-bug Category: This is a bug. label Jul 3, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

2 participants