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

read_to_end unintuitive performance. #23815

Closed
blackbeam opened this issue Mar 28, 2015 · 2 comments
Closed

read_to_end unintuitive performance. #23815

blackbeam opened this issue Mar 28, 2015 · 2 comments

Comments

@blackbeam
Copy link
Contributor

Performance problem in read_to_end was uncovered with PR #23668.
The reason is that in case of data to read equals vec's capacity, read_to_end anyway will reserve (and with #23668 will initialize) another 64KB in vec just to read nothing to it because of lack of any other way to get a EOF hint from Read implementor.

Simple illustration:

#![feature(test)]
extern crate test;
use std::io::Read;

#[bench]
fn read_to_end_1024_to_1024(b: &mut test::Bencher) {
    let buf = [1u8; 1024];
    let mut src = &buf[..];
    let mut dst = Vec::with_capacity(1024);
    b.iter(|| {
        test::black_box(src.read_to_end(&mut dst))
    });
}

#[bench]
fn read_to_end_1024_to_1025(b: &mut test::Bencher) {
    let buf = [1u8; 1024];
    let mut src = &buf[..];
    let mut dst = Vec::with_capacity(1025);
    b.iter(|| {
        test::black_box(src.read_to_end(&mut dst))
    });
}

Result:

// Opt 0
running 2 tests
test read_to_end_1024_to_1024 ... bench:   9033646 ns/iter (+/- 456609)
test read_to_end_1024_to_1025 ... bench:       488 ns/iter (+/- 24)

// Opt 3
running 2 tests
test read_to_end_1024_to_1024 ... bench:    105434 ns/iter (+/- 3655)
test read_to_end_1024_to_1025 ... bench:        13 ns/iter (+/- 1)

Edit:
Same with no initialization:

// Opt 0
running 2 tests
test read_to_end_1024_to_1024 ... bench:       375 ns/iter (+/- 8)
test read_to_end_1024_to_1025 ... bench:       387 ns/iter (+/- 16)

// Opt 3
running 2 tests
test read_to_end_1024_to_1024 ... bench:        16 ns/iter (+/- 1)
test read_to_end_1024_to_1025 ... bench:        16 ns/iter (+/- 1)
@blackbeam
Copy link
Contributor Author

I found affected call in libterm compiled.rs:327.
PistonDevelopers/image is also seriously affected.

@sfackler
Copy link
Member

Fixed in #23820

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants