Skip to content

Improve Vec::truncate performance in no-opt builds #18097

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

Closed
wants to merge 1 commit into from

Conversation

veddan
Copy link
Contributor

@veddan veddan commented Oct 16, 2014

This significantly improves the performance of Vec::truncate for types
without destructors when building with --opt-level=0 or --opt-level=1.
Fixes #17633.

This significantly improves the performance of `Vec::truncate` for types
without destructors when building with `--opt-level=0` or
`--opt-level=1`. Fixes rust-lang#17633.
@veddan
Copy link
Contributor Author

veddan commented Oct 16, 2014

Benchmark code

extern crate test;
use test::Bencher;

#[deriving(Clone)]
struct Foo { x: u8  }

impl Drop for Foo {
    fn drop(&mut self) { }
}

#[bench]
fn bench_no_drop(b: &mut Bencher) {
    let v = Vec::from_elem(10000, 0u8);
    b.iter(|| { let mut x = v.clone(); x.truncate(0); });
}

#[bench]
fn bench_drop(b: &mut Bencher) {
    let v = Vec::from_elem(10000, Foo { x: 0 });
    b.iter(|| { let mut x = v.clone(); x.truncate(0); });
}

Results

Before, -O0

test bench_drop    ... bench:    799459 ns/iter (+/- 33636)
test bench_no_drop ... bench:    667411 ns/iter (+/- 27678)

After, -O0

test bench_drop    ... bench:    791541 ns/iter (+/- 28567)
test bench_no_drop ... bench:    476229 ns/iter (+/- 19815)

Before, -O1

test bench_drop    ... bench:    394395 ns/iter (+/- 17525)
test bench_no_drop ... bench:    388002 ns/iter (+/- 21695)

After, -O1

test bench_drop    ... bench:    406982 ns/iter (+/- 30450)
test bench_no_drop ... bench:    288509 ns/iter (+/- 13999)

@thestinger
Copy link
Contributor

I don't like the idea of adding complexity to improve runtime performance without optimizations. I don't think a small constant factor slowdown for --opt-level=1 is important. It could be fixed at --opt-level=1 with careful application of #[inline(always)]. In the worst case, the lack of this specialization will double the constant factor for each paired initialization / destruction.

@pcwalton
Copy link
Contributor

Agreed with @thestinger, closing. Many thanks for the patch, however :)

@pcwalton pcwalton closed this Oct 23, 2014
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

Successfully merging this pull request may close these issues.

Optimize vec.truncate and destructors for types with no dtor at -O0
3 participants