From 6ad133443a7975b2e86a3bbba16da33c769eeabc Mon Sep 17 00:00:00 2001 From: The8472 Date: Sat, 25 Jan 2020 20:08:46 +0100 Subject: [PATCH] add benchmark to cover in-place extend --- library/alloc/benches/vec.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/alloc/benches/vec.rs b/library/alloc/benches/vec.rs index 39f67c664d4..1019503a841 100644 --- a/library/alloc/benches/vec.rs +++ b/library/alloc/benches/vec.rs @@ -236,6 +236,20 @@ fn do_bench_extend_from_slice(b: &mut Bencher, dst_len: usize, src_len: usize) { }); } +#[bench] +fn bench_extend_recycle(b: &mut Bencher) { + let mut data = vec![0; 1000]; + + b.iter(|| { + let tmp = std::mem::replace(&mut data, Vec::new()); + let mut to_extend = black_box(Vec::new()); + to_extend.extend(tmp.into_iter()); + std::mem::replace(&mut data, black_box(to_extend)); + }); + + black_box(data); +} + #[bench] fn bench_extend_from_slice_0000_0000(b: &mut Bencher) { do_bench_extend_from_slice(b, 0, 0)