Implement VecDeque::truncate_to_range#156220
Conversation
|
r? @nia-e rustbot has assigned @nia-e. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| Some(c) => { | ||
| let _g_a = Dropper(&mut *drop_a); | ||
| let _g_b = Dropper(&mut *drop_b); | ||
| ptr::drop_in_place(c); |
There was a problem hiding this comment.
without dropper, won't this leak on panic ? same for line 1537
There was a problem hiding this comment.
Where would it panic? _g_{a,b} will only have their destructors run after we already attempt to drop c
| let bptr = back.as_mut_ptr(); | ||
|
|
||
| unsafe { | ||
| let (drop_a, drop_b, drop_c) = if end <= flen { |
There was a problem hiding this comment.
Would it make sense to add a fast path for types that don’t need dropping? Something like:
around line 1493
if !mem::needs_drop::<T>() {
self.head = self.to_physical_idx(start);
self.len = end - start;
return;
}what do you think ?
|
See @qaijuang's point on checking for drop; otherwise, r=me |
8cab6b8 to
ecb66eb
Compare
|
Could you also add a test for the drop impl? e.g. // in a test fn
static DROPPED: AtomicUsize = AtomicUsize::new(0);
struct Foo(/* whatever */);
impl Drop for Foo {
fn drop(&mut self) {
DROPPED.fetch_add(1, Ordering::Relaxed);
}
}
let mut dq = /* get a vecdeque of Foo, say 12 elems long */
dq.truncate_to_range(1..5);
assert_eq!(8, DROPPED.load(Ordering::Relaxed)); |
|
I will make the requested updates, but I suspect LLVM could drop all the drop code since there would be no side effect. I also note that |
|
True, but that sounds to me like it should be used more ^^ and this all is unlikely to be optimised away since the drop glue will be defined in the crate where the element type is defined, and thus may not get properly inlined |
ecb66eb to
3167c69
Compare
|
This patch is now blocked behind the rename of I am able to report that the pessimism here is half right and half wrong. In a release build, when calling regular The location of the So do you actually want this call? Do you want more of them through all the collection types? Sorry if I have, through any ignorance, failed to construct or analyze this properly. use std::collections::VecDeque;
#[derive(Debug, Default)]
pub struct Point {
pub x: i32,
pub y: i32,
}
#[unsafe(no_mangle)]
pub fn truncate_point(d: &mut VecDeque<Point>, len: usize) {
d.truncate(len);
}
fn main() {
let mut d = VecDeque::new();
d.push_back(Point::default());
truncate_point(&mut d, 0);
println!("{d:?}");
}truncate_point:
.cfi_startproc
cmp rsi, qword ptr [rdi + 24]
jae .LBB5_2
mov qword ptr [rdi + 24], rsi
.LBB5_2:
ret |
Interesting, the elimination is real on release builds, i was able to reproduce(external crate included). Safe to say, since the |

Tracking issue: #156215