Skip to content

Commit 9e8113c

Browse files
committed
---
yaml --- r: 212927 b: refs/heads/master c: 032804b h: refs/heads/master i: 212925: f6cf9b2 212923: b83b2c1 212919: cd5f062 212911: 586e3c0 212895: 3473940 212863: 9addb74 v: v3
1 parent f7e3d38 commit 9e8113c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: a118b936faa7f802c41214cb908622a7893bebfd
2+
refs/heads/master: 032804bf68e2ac13add895206f2173409acff836
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: ba0e1cd8147d452c356aacb29fb87568ca26f111
55
refs/heads/try: 1864973ae17213c5a58c4dd3f9af6d1b6c7d2e05

trunk/src/libcollections/vec.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,25 @@ impl<T> ops::DerefMut for Vec<T> {
14101410
impl<T> FromIterator<T> for Vec<T> {
14111411
#[inline]
14121412
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> Vec<T> {
1413-
let mut vector = Vec::new();
1413+
// Unroll the first iteration, as the vector is going to be
1414+
// expanded on this iteration in every case when the iterable is not
1415+
// empty, but the loop in extend_desugared() is not going to see the
1416+
// vector being full in the few subsequent loop iterations.
1417+
// So we get better branch prediction and the possibility to
1418+
// construct the vector with initial estimated capacity.
1419+
let mut iterator = iterable.into_iter();
1420+
let mut vector = match iterator.next() {
1421+
None => return Vec::new(),
1422+
Some(element) => {
1423+
let (lower, _) = iterator.size_hint();
1424+
let mut vector = Vec::with_capacity(1 + lower);
1425+
unsafe {
1426+
ptr::write(vector.get_unchecked_mut(0), element);
1427+
vector.set_len(1);
1428+
}
1429+
vector
1430+
}
1431+
};
14141432
vector.extend(iterable);
14151433
vector
14161434
}

0 commit comments

Comments
 (0)