Skip to content

fix O(n^2) perf bug for std::io::fs::walk_dir #13720

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

Merged
merged 1 commit into from
Apr 24, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/libstd/io/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ pub fn readdir(path: &Path) -> IoResult<Vec<Path>> {

/// Returns an iterator which will recursively walk the directory structure
/// rooted at `path`. The path given will not be iterated over, and this will
/// perform iteration in a top-down order.
/// perform iteration in some top-down order. The contents of unreadable
/// subdirectories are ignored.
pub fn walk_dir(path: &Path) -> IoResult<Directories> {
Ok(Directories { stack: try!(readdir(path)) })
}
Expand All @@ -503,7 +504,7 @@ pub struct Directories {

impl Iterator<Path> for Directories {
fn next(&mut self) -> Option<Path> {
match self.stack.shift() {
match self.stack.pop() {
Some(path) => {
if path.is_dir() {
match readdir(&path) {
Expand Down Expand Up @@ -970,6 +971,32 @@ mod test {
check!(rmdir(dir));
})

iotest!(fn file_test_walk_dir() {
let tmpdir = tmpdir();
let dir = &tmpdir.join("walk_dir");
check!(mkdir(dir, io::UserRWX));

let dir1 = &dir.join("01/02/03");
check!(mkdir_recursive(dir1, io::UserRWX));
check!(File::create(&dir1.join("04")));

let dir2 = &dir.join("11/12/13");
check!(mkdir_recursive(dir2, io::UserRWX));
check!(File::create(&dir2.join("14")));

let mut files = check!(walk_dir(dir));
let mut cur = [0u8, .. 2];
for f in files {
let stem = f.filestem_str().unwrap();
let root = stem[0] - ('0' as u8);
let name = stem[1] - ('0' as u8);
assert!(cur[root as uint] < name);
cur[root as uint] = name;
}

check!(rmdir_recursive(dir));
})

iotest!(fn recursive_mkdir() {
let tmpdir = tmpdir();
let dir = tmpdir.join("d1/d2");
Expand Down