Skip to content

std::io::fs::walk_dir has O(n^2) behaviour #13411

Closed
@huonw

Description

@huonw

It calls .shift on a vector repeatedly.

Example:

// walk_dir.rs
fn main() {
    let dir = Path::new(std::os::args()[1]);
    for _ in std::io::fs::walk_dir(&dir).unwrap() {}
}
$ n=50000; mkdir -p $n; bash -c "touch $n/{1..$n}"
$ time ./walk_dir 50000/

real    0m1.442s
user    0m1.404s
sys     0m0.032s

$ n=100000; mkdir -p $n; bash -c "touch $n/{1..$n}"
$ time ./walk_dir 100000/

real    0m6.228s
user    0m6.096s
sys     0m0.132s

50000 and 100000 are directories with that many files in them. Theoretically this wants a deque.

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions