auto merge of #15256 : erickt/rust/optimizations, r=alexcrichton

The bug #11084 causes `option::collect` and `result::collect` about twice as slower as it should because llvm is having some trouble optimizing away the scan closure. This gets rid of it so now those functions perform equivalent to a hand written version.

This also adds an impl of `Default` for `Rc` along the way.
This commit is contained in:
bors 2014-06-30 03:46:25 +00:00
commit e25eb6b223
4 changed files with 52 additions and 22 deletions

View File

@ -27,6 +27,7 @@ use core::mem::transmute;
use core::cell::Cell; use core::cell::Cell;
use core::clone::Clone; use core::clone::Clone;
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering}; use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
use core::default::Default;
use core::kinds::marker; use core::kinds::marker;
use core::ops::{Deref, Drop}; use core::ops::{Deref, Drop};
use core::option::{Option, Some, None}; use core::option::{Option, Some, None};
@ -152,6 +153,13 @@ impl<T> Clone for Rc<T> {
} }
} }
impl<T: Default> Default for Rc<T> {
#[inline]
fn default() -> Rc<T> {
Rc::new(Default::default())
}
}
impl<T: PartialEq> PartialEq for Rc<T> { impl<T: PartialEq> PartialEq for Rc<T> {
#[inline(always)] #[inline(always)]
fn eq(&self, other: &Rc<T>) -> bool { **self == **other } fn eq(&self, other: &Rc<T>) -> bool { **self == **other }

View File

@ -265,8 +265,6 @@ pub fn hash_with_keys<T: Hash<SipState>>(k0: u64, k1: u64, value: &T) -> u64 {
state.result() state.result()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use test::Bencher; use test::Bencher;

View File

@ -587,20 +587,32 @@ impl<A> ExactSize<A> for Item<A> {}
/// ``` /// ```
#[inline] #[inline]
pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V> { pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V> {
// FIXME(#11084): This should be twice as fast once this bug is closed. // FIXME(#11084): This could be replaced with Iterator::scan when this
let mut iter = iter.scan(false, |state, x| { // performance bug is closed.
match x {
Some(x) => Some(x), struct Adapter<Iter> {
None => { iter: Iter,
*state = true; found_none: bool,
None }
impl<T, Iter: Iterator<Option<T>>> Iterator<T> for Adapter<Iter> {
#[inline]
fn next(&mut self) -> Option<T> {
match self.iter.next() {
Some(Some(value)) => Some(value),
Some(None) => {
self.found_none = true;
None
}
None => None,
} }
} }
}); }
let v: V = FromIterator::from_iter(iter.by_ref()); let mut adapter = Adapter { iter: iter, found_none: false };
let v: V = FromIterator::from_iter(adapter.by_ref());
if iter.state { if adapter.found_none {
None None
} else { } else {
Some(v) Some(v)

View File

@ -585,20 +585,32 @@ impl<T: Show, E> Result<T, E> {
/// ``` /// ```
#[inline] #[inline]
pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E> { pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E> {
// FIXME(#11084): This should be twice as fast once this bug is closed. // FIXME(#11084): This could be replaced with Iterator::scan when this
let mut iter = iter.scan(None, |state, x| { // performance bug is closed.
match x {
Ok(x) => Some(x), struct Adapter<Iter, E> {
Err(err) => { iter: Iter,
*state = Some(err); err: Option<E>,
None }
impl<T, E, Iter: Iterator<Result<T, E>>> Iterator<T> for Adapter<Iter, E> {
#[inline]
fn next(&mut self) -> Option<T> {
match self.iter.next() {
Some(Ok(value)) => Some(value),
Some(Err(err)) => {
self.err = Some(err);
None
}
None => None,
} }
} }
}); }
let v: V = FromIterator::from_iter(iter.by_ref()); let mut adapter = Adapter { iter: iter, err: None };
let v: V = FromIterator::from_iter(adapter.by_ref());
match iter.state { match adapter.err {
Some(err) => Err(err), Some(err) => Err(err),
None => Ok(v), None => Ok(v),
} }