Optimize partition_slice (#341)
* Generate debug symbols in optimized builds. This allows profiling tools to provide more accurate information, especially details about inlined functions. * Rewrite and optimize partition_slice This improves the performance of the register allocation passes which use LiveValueTracker.
This commit is contained in:
committed by
Dan Gohman
parent
4c150907bf
commit
80fdfb2376
@@ -32,3 +32,11 @@ term = "0.5.1"
|
|||||||
capstone = "0.3.1"
|
capstone = "0.3.1"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|
||||||
|
# We want debug symbols on release binaries by default since it allows profiling
|
||||||
|
# tools to give more accurate information. We can always strip them out later if
|
||||||
|
# necessary.
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
[profile.bench]
|
||||||
|
debug = true
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
//! Rearrange the elements in a slice according to a predicate.
|
//! Rearrange the elements in a slice according to a predicate.
|
||||||
|
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
/// Rearrange the elements of the mutable slice `s` such that elements where `p(t)` is true precede
|
/// Rearrange the elements of the mutable slice `s` such that elements where `p(t)` is true precede
|
||||||
/// the elements where `p(t)` is false.
|
/// the elements where `p(t)` is false.
|
||||||
///
|
///
|
||||||
@@ -10,24 +12,42 @@ pub fn partition_slice<T, F>(s: &mut [T], mut p: F) -> usize
|
|||||||
where
|
where
|
||||||
F: FnMut(&T) -> bool,
|
F: FnMut(&T) -> bool,
|
||||||
{
|
{
|
||||||
// Count the length of the prefix where `p` returns true.
|
// The iterator works like a deque which we can pop from both ends.
|
||||||
let mut count = match s.iter().position(|t| !p(t)) {
|
let mut i = s.iter_mut();
|
||||||
Some(t) => t,
|
|
||||||
None => return s.len(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Swap remaining `true` elements into place.
|
// Number of elements for which the predicate is known to be true.
|
||||||
//
|
let mut pos = 0;
|
||||||
// This actually preserves the order of the `true` elements, but the `false` elements get
|
|
||||||
// shuffled.
|
loop {
|
||||||
for i in count + 1..s.len() {
|
// Find the first element for which the predicate fails.
|
||||||
if p(&s[i]) {
|
let head = loop {
|
||||||
s.swap(count, i);
|
match i.next() {
|
||||||
count += 1;
|
Some(head) => {
|
||||||
}
|
if !p(&head) {
|
||||||
|
break head;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => return pos,
|
||||||
|
}
|
||||||
|
pos += 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Find the last element for which the predicate succeeds.
|
||||||
|
let tail = loop {
|
||||||
|
match i.next_back() {
|
||||||
|
Some(tail) => {
|
||||||
|
if p(&tail) {
|
||||||
|
break tail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => return pos,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Swap the two elements into the right order.
|
||||||
|
mem::swap(head, tail);
|
||||||
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -70,8 +90,8 @@ mod tests {
|
|||||||
check(&[1, 2, 3], &[1, 2, 3]);
|
check(&[1, 2, 3], &[1, 2, 3]);
|
||||||
check(&[1, 2, 10], &[10, 2, 1]); // Note: 2, 1 order not required.
|
check(&[1, 2, 10], &[10, 2, 1]); // Note: 2, 1 order not required.
|
||||||
check(&[1, 10, 2], &[10, 1, 2]); // Note: 1, 2 order not required.
|
check(&[1, 10, 2], &[10, 1, 2]); // Note: 1, 2 order not required.
|
||||||
check(&[1, 20, 10], &[20, 10, 1]);
|
check(&[1, 20, 10], &[10, 20, 1]); // Note: 10, 20 order not required.
|
||||||
check(&[1, 20, 3, 10], &[20, 10, 3, 1]);
|
check(&[1, 20, 3, 10], &[10, 20, 3, 1]);
|
||||||
check(&[20, 3, 10, 1], &[20, 10, 3, 1]);
|
check(&[20, 3, 10, 1], &[20, 10, 3, 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user