Skip to content

Commit

Permalink
Optimize Option<Range>::clone
Browse files Browse the repository at this point in the history
Range<T> is not Copy even if T is Copy, which pessimizes the implementation
of Option<Range>::clone. Specialize Option::clone for Range so that it can
be more efficient. The specialization uses ptr::read to emulate Copy.
  • Loading branch information
ridiculousfish authored and Peter Ammon committed Sep 10, 2020
1 parent 46176eb commit bebfcfd
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
use crate::pin::Pin;
use crate::{
convert, fmt, hint, mem,
ops::{self, Deref, DerefMut},
convert, fmt, hint, mem, ptr,
ops::{self, Deref, DerefMut, Range},
};

/// The `Option` type. See [the module level documentation](self) for more.
Expand Down Expand Up @@ -1257,6 +1257,16 @@ impl<T: Copy> Clone for Option<T> {
}
}

// Range<T> is not Copy even if T is copy (see #27186),
// so provide an efficient implementation.
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Copy> Clone for Option<Range<T>> {
#[inline]
fn clone(&self) -> Self {
// SAFETY: 'self' is not Drop so memcpy is OK.
unsafe { ptr::read(self as *const Self) }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for Option<T> {
Expand Down

0 comments on commit bebfcfd

Please sign in to comment.