Skip to content

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

Open
@emilio

Description

@emilio

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions