-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Non-naive implementation of VecDeque.append
#52553
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
24bc854
Non-naive implementation for `VecDeque.append`
Pazzaz 9f1fdec
Simplify vecdeque append test
Pazzaz 6ebd62b
Make VecDeque append safer and easier to understand
Pazzaz 894c9ca
Add benchmark for VecDeque append
Pazzaz 8d3554c
Don't drop values in other, just move the tail
Pazzaz ae0f254
Clarify dst condition
Pazzaz 7662528
Clarify unused_as_mut_slices
Pazzaz b063bd4
Test VecDeque append not dropping twice
Pazzaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(duration_as_u128)] | ||
use std::{collections::VecDeque, time::Instant}; | ||
|
||
const VECDEQUE_LEN: i32 = 100000; | ||
const WARMUP_N: usize = 100; | ||
const BENCH_N: usize = 1000; | ||
|
||
fn main() { | ||
let a: VecDeque<i32> = (0..VECDEQUE_LEN).collect(); | ||
let b: VecDeque<i32> = (0..VECDEQUE_LEN).collect(); | ||
|
||
for _ in 0..WARMUP_N { | ||
let mut c = a.clone(); | ||
let mut d = b.clone(); | ||
c.append(&mut d); | ||
} | ||
|
||
let mut durations = Vec::with_capacity(BENCH_N); | ||
|
||
for _ in 0..BENCH_N { | ||
let mut c = a.clone(); | ||
let mut d = b.clone(); | ||
let before = Instant::now(); | ||
c.append(&mut d); | ||
let after = Instant::now(); | ||
durations.push(after.duration_since(before)); | ||
} | ||
|
||
let l = durations.len(); | ||
durations.sort(); | ||
|
||
assert!(BENCH_N % 2 == 0); | ||
let median = (durations[(l / 2) - 1] + durations[l / 2]) / 2; | ||
println!( | ||
"\ncustom-bench vec_deque_append {:?} ns/iter\n", | ||
median.as_nanos() | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is like
[T]::copy_from_slice
but withoutT: Copy
? Then it is up to callers to be careful to not "duplicate" items and have for example some heap memory be double-freed.… in fact this code ends up calling
other.clear()
which will incorrectly drop the items that have just been copied. Instead it should probably do something likeother.tail = 0; other.head = 0
orother.tail = other.head
.Consider modifying the test to have not just
T = usize
, but a type with aDrop
impl that does something non-trivial, ideally detect double drops. PerhapsT = Box<usize>
is enough, if you can verify that CI runs tests with ASAN.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new test that checks for double drops similar to the Vec tests has been added.