Skip to content

Add mem::needs_drop() optimization to Drop impl #12

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 2 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- The `Drop` impl now uses the `mem::needs_drop()` optimization hint to avoid
unnecessary overhead.

## [0.2.0] - 2017-09-17
### Added
Expand Down
19 changes: 13 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,19 @@ impl<T> Drop for StableVec<T> {
// To achieve all this, we manually drop all remaining elements, then
// tell the Vec that its length is 0 (its capacity stays the same!) and
// let the Vec drop itself in the end.
let living_indices = self.deleted.iter()
.enumerate()
.filter_map(|(i, deleted)| if deleted { None } else { Some(i) });
for i in living_indices {
unsafe {
ptr::drop_in_place(&mut self.data[i]);
//
// When `T` doesn't need to be dropped, we can skip this next step.
// While `ptr::drop_in_place()` already uses the `mem::needs_drop()`
// check, it's still useful to check it here, to avoid executing these
// two loops completely.
if mem::needs_drop::<T>() {
let living_indices = self.deleted.iter()
.enumerate()
.filter_map(|(i, deleted)| if deleted { None } else { Some(i) });
for i in living_indices {
unsafe {
ptr::drop_in_place(&mut self.data[i]);
}
}
}

Expand Down