style: switch to using rustfmt

This commit is contained in:
Andrew Gallant
2020-01-10 17:12:15 -05:00
parent 080bd6d39d
commit 15f8f1c45d
13 changed files with 115 additions and 197 deletions
+16 -44
View File
@@ -8,15 +8,11 @@ extern crate memchr;
use criterion::{Bencher, Benchmark, Criterion, Throughput};
use imp::{
memchr1_count, memchr2_count, memchr3_count,
memrchr1_count, memrchr2_count, memrchr3_count,
fallback1_count, fallback2_count, fallback3_count,
naive1_count, naive2_count, naive3_count,
};
use inputs::{
Input, Search1, Search2, Search3,
HUGE, SMALL, TINY, EMPTY,
fallback1_count, fallback2_count, fallback3_count, memchr1_count,
memchr2_count, memchr3_count, memrchr1_count, memrchr2_count,
memrchr3_count, naive1_count, naive2_count, naive3_count,
};
use inputs::{Input, Search1, Search2, Search3, EMPTY, HUGE, SMALL, TINY};
#[cfg(target_arch = "x86_64")]
#[path = "../../src/c.rs"]
@@ -651,27 +647,19 @@ fn define_input1<'i>(
) {
if let Some(search) = input.never1() {
let mut bench = bench.clone();
define(c, group, "never", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "never", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.rare1() {
let mut bench = bench.clone();
define(c, group, "rare", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "rare", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.uncommon1() {
let mut bench = bench.clone();
define(c, group, "uncommon", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "uncommon", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.common1() {
let mut bench = bench.clone();
define(c, group, "common", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "common", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.verycommon1() {
let mut bench = bench.clone();
@@ -695,27 +683,19 @@ fn define_input2<'i>(
) {
if let Some(search) = input.never2() {
let mut bench = bench.clone();
define(c, group, "never", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "never", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.rare2() {
let mut bench = bench.clone();
define(c, group, "rare", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "rare", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.uncommon2() {
let mut bench = bench.clone();
define(c, group, "uncommon", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "uncommon", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.common2() {
let mut bench = bench.clone();
define(c, group, "common", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "common", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.verycommon2() {
let mut bench = bench.clone();
@@ -739,27 +719,19 @@ fn define_input3<'i>(
) {
if let Some(search) = input.never3() {
let mut bench = bench.clone();
define(c, group, "never", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "never", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.rare3() {
let mut bench = bench.clone();
define(c, group, "rare", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "rare", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.uncommon3() {
let mut bench = bench.clone();
define(c, group, "uncommon", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "uncommon", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.common3() {
let mut bench = bench.clone();
define(c, group, "common", input.corpus, move |b| {
bench(search, b)
});
define(c, group, "common", input.corpus, move |b| bench(search, b));
}
if let Some(search) = input.verycommon3() {
let mut bench = bench.clone();
+1 -1
View File
@@ -1,7 +1,7 @@
#[cfg(target_arch = "x86_64")]
use c;
use fallback;
use memchr::{Memchr, Memchr2, Memchr3, memrchr, memrchr2, memrchr3};
use memchr::{memrchr, memrchr2, memrchr3, Memchr, Memchr2, Memchr3};
use naive;
pub fn memchr1_count(b1: u8, haystack: &[u8]) -> usize {
+3 -9
View File
@@ -39,9 +39,7 @@ pub const HUGE: Input = Input {
SearchByte { byte: b't', count: 39268 },
SearchByte { byte: b'o', count: 34495 },
],
verycommon: &[
SearchByte { byte: b' ', count: 97626 },
],
verycommon: &[SearchByte { byte: b' ', count: 97626 }],
supercommon: &[],
};
@@ -62,9 +60,7 @@ pub const TINY: Input = Input {
SearchByte { byte: b's', count: 5 },
SearchByte { byte: b'e', count: 6 },
],
common: &[
SearchByte { byte: b' ', count: 11 },
],
common: &[SearchByte { byte: b' ', count: 11 }],
verycommon: &[],
supercommon: &[],
};
@@ -91,9 +87,7 @@ pub const SMALL: Input = Input {
SearchByte { byte: b'h', count: 34 },
SearchByte { byte: b'i', count: 35 },
],
verycommon: &[
SearchByte { byte: b' ', count: 106 },
],
verycommon: &[SearchByte { byte: b' ', count: 106 }],
supercommon: &[],
};
+1 -1
View File
@@ -53,7 +53,7 @@ fn enable_libc() {
}
fn is_feature_set(name: &str) -> bool {
is_env_set(&format!("CARGO_FEATURE_{}", name))
is_env_set(&format!("CARGO_FEATURE_{}", name))
}
fn is_env_set(name: &str) -> bool {
+2
View File
@@ -0,0 +1,2 @@
max_width = 79
use_small_heuristics = "max"
+3 -7
View File
@@ -17,7 +17,7 @@ macro_rules! iter_next {
$self_.position = found_position + 1;
found_position
})
}
};
}
macro_rules! iter_next_back {
@@ -27,7 +27,7 @@ macro_rules! iter_next_back {
$self_.haystack = $self_.haystack.split_at(index).0;
$self_.position + index
})
}
};
}
/// An iterator for `memchr`.
@@ -43,11 +43,7 @@ impl<'a> Memchr<'a> {
/// Creates a new iterator that yields all positions of needle in haystack.
#[inline]
pub fn new(needle: u8, haystack: &[u8]) -> Memchr {
Memchr {
needle: needle,
haystack: haystack,
position: 0,
}
Memchr { needle: needle, haystack: haystack, position: 0 }
}
}
+3 -8
View File
@@ -21,7 +21,6 @@ instead of one. Similarly for `memchr3`.
*/
#![cfg_attr(not(feature = "use_std"), no_std)]
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/memchr/2.0.0")]
@@ -52,10 +51,10 @@ mod c;
mod fallback;
mod iter;
mod naive;
#[cfg(all(target_arch = "x86_64", memchr_runtime_simd))]
mod x86;
#[cfg(test)]
mod tests;
#[cfg(all(target_arch = "x86_64", memchr_runtime_simd))]
mod x86;
/// An iterator over all occurrences of the needle in a haystack.
#[inline]
@@ -65,11 +64,7 @@ pub fn memchr_iter(needle: u8, haystack: &[u8]) -> Memchr {
/// An iterator over all occurrences of the needles in a haystack.
#[inline]
pub fn memchr2_iter(
needle1: u8,
needle2: u8,
haystack: &[u8],
) -> Memchr2 {
pub fn memchr2_iter(needle1: u8, needle2: u8, haystack: &[u8]) -> Memchr2 {
Memchr2::new(needle1, needle2, haystack)
}
+6 -18
View File
@@ -1,37 +1,25 @@
#![allow(dead_code)]
pub fn memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
haystack
.iter()
.position(|&b| b == n1)
haystack.iter().position(|&b| b == n1)
}
pub fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
haystack
.iter()
.position(|&b| b == n1 || b == n2)
haystack.iter().position(|&b| b == n1 || b == n2)
}
pub fn memchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option<usize> {
haystack
.iter()
.position(|&b| b == n1 || b == n2 || b == n3)
haystack.iter().position(|&b| b == n1 || b == n2 || b == n3)
}
pub fn memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
haystack
.iter()
.rposition(|&b| b == n1)
haystack.iter().rposition(|&b| b == n1)
}
pub fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
haystack
.iter()
.rposition(|&b| b == n1 || b == n2)
haystack.iter().rposition(|&b| b == n1 || b == n2)
}
pub fn memrchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option<usize> {
haystack
.iter()
.rposition(|&b| b == n1 || b == n2 || b == n3)
haystack.iter().rposition(|&b| b == n1 || b == n2 || b == n3)
}
+6 -5
View File
@@ -160,8 +160,9 @@ quickcheck! {
// Return a vector with the concatenation of the fronts and the reverse of the
// backs.
fn double_ended_take<I, J>(mut iter: I, take_side: J) -> Vec<I::Item>
where I: DoubleEndedIterator,
J: Iterator<Item=bool>,
where
I: DoubleEndedIterator,
J: Iterator<Item = bool>,
{
let mut found_front = Vec::new();
let mut found_back = Vec::new();
@@ -191,7 +192,7 @@ fn double_ended_take<I, J>(mut iter: I, take_side: J) -> Vec<I::Item>
fn positions1<'a>(
n1: u8,
haystack: &'a [u8],
) -> Box<dyn DoubleEndedIterator<Item=usize> + 'a> {
) -> Box<dyn DoubleEndedIterator<Item = usize> + 'a> {
let it = haystack
.iter()
.enumerate()
@@ -204,7 +205,7 @@ fn positions2<'a>(
n1: u8,
n2: u8,
haystack: &'a [u8],
) -> Box<dyn DoubleEndedIterator<Item=usize> + 'a> {
) -> Box<dyn DoubleEndedIterator<Item = usize> + 'a> {
let it = haystack
.iter()
.enumerate()
@@ -218,7 +219,7 @@ fn positions3<'a>(
n2: u8,
n3: u8,
haystack: &'a [u8],
) -> Box<dyn DoubleEndedIterator<Item=usize> + 'a> {
) -> Box<dyn DoubleEndedIterator<Item = usize> + 'a> {
let it = haystack
.iter()
.enumerate()
+32 -92
View File
@@ -44,61 +44,21 @@ fn memchr_tests() -> Vec<MemchrTest> {
/// in the tests by varying the starting point of the slice.
const MEMCHR_TESTS: &[MemchrTestStatic] = &[
// one needle (applied to memchr + memchr2 + memchr3)
MemchrTestStatic {
corpus: "a",
needles: &[b'a'],
positions: &[0],
},
MemchrTestStatic {
corpus: "aa",
needles: &[b'a'],
positions: &[0, 1],
},
MemchrTestStatic { corpus: "a", needles: &[b'a'], positions: &[0] },
MemchrTestStatic { corpus: "aa", needles: &[b'a'], positions: &[0, 1] },
MemchrTestStatic {
corpus: "aaa",
needles: &[b'a'],
positions: &[0, 1, 2],
},
MemchrTestStatic {
corpus: "",
needles: &[b'a'],
positions: &[],
},
MemchrTestStatic {
corpus: "z",
needles: &[b'a'],
positions: &[],
},
MemchrTestStatic {
corpus: "zz",
needles: &[b'a'],
positions: &[],
},
MemchrTestStatic {
corpus: "zza",
needles: &[b'a'],
positions: &[2],
},
MemchrTestStatic {
corpus: "zaza",
needles: &[b'a'],
positions: &[1, 3],
},
MemchrTestStatic {
corpus: "zzza",
needles: &[b'a'],
positions: &[3],
},
MemchrTestStatic {
corpus: "\x00a",
needles: &[b'a'],
positions: &[1],
},
MemchrTestStatic {
corpus: "\x00",
needles: &[b'\x00'],
positions: &[0],
},
MemchrTestStatic { corpus: "", needles: &[b'a'], positions: &[] },
MemchrTestStatic { corpus: "z", needles: &[b'a'], positions: &[] },
MemchrTestStatic { corpus: "zz", needles: &[b'a'], positions: &[] },
MemchrTestStatic { corpus: "zza", needles: &[b'a'], positions: &[2] },
MemchrTestStatic { corpus: "zaza", needles: &[b'a'], positions: &[1, 3] },
MemchrTestStatic { corpus: "zzza", needles: &[b'a'], positions: &[3] },
MemchrTestStatic { corpus: "\x00a", needles: &[b'a'], positions: &[1] },
MemchrTestStatic { corpus: "\x00", needles: &[b'\x00'], positions: &[0] },
MemchrTestStatic {
corpus: "\x00\x00",
needles: &[b'\x00'],
@@ -119,7 +79,6 @@ const MEMCHR_TESTS: &[MemchrTestStatic] = &[
needles: &[b'a'],
positions: &[32],
},
// two needles (applied to memchr2 + memchr3)
MemchrTestStatic {
corpus: "az",
@@ -131,21 +90,9 @@ const MEMCHR_TESTS: &[MemchrTestStatic] = &[
needles: &[b'a', b'z'],
positions: &[0, 1],
},
MemchrTestStatic {
corpus: "az",
needles: &[b'x', b'y'],
positions: &[],
},
MemchrTestStatic {
corpus: "az",
needles: &[b'a', b'y'],
positions: &[0],
},
MemchrTestStatic {
corpus: "az",
needles: &[b'x', b'z'],
positions: &[1],
},
MemchrTestStatic { corpus: "az", needles: &[b'x', b'y'], positions: &[] },
MemchrTestStatic { corpus: "az", needles: &[b'a', b'y'], positions: &[0] },
MemchrTestStatic { corpus: "az", needles: &[b'x', b'z'], positions: &[1] },
MemchrTestStatic {
corpus: "yyyyaz",
needles: &[b'a', b'z'],
@@ -156,7 +103,6 @@ const MEMCHR_TESTS: &[MemchrTestStatic] = &[
needles: &[b'z', b'a'],
positions: &[4, 5],
},
// three needles (applied to memchr3)
MemchrTestStatic {
corpus: "xyz",
@@ -217,11 +163,7 @@ struct MemchrTestStatic {
}
impl MemchrTest {
fn one<F: Fn(u8, &[u8]) -> Option<usize>>(
&self,
reverse: bool,
f: F,
) {
fn one<F: Fn(u8, &[u8]) -> Option<usize>>(&self, reverse: bool, f: F) {
let needles = match self.needles(1) {
None => return,
Some(needles) => needles,
@@ -248,11 +190,7 @@ impl MemchrTest {
}
}
fn two<F: Fn(u8, u8, &[u8]) -> Option<usize>>(
&self,
reverse: bool,
f: F,
) {
fn two<F: Fn(u8, u8, &[u8]) -> Option<usize>>(&self, reverse: bool, f: F) {
let needles = match self.needles(2) {
None => return,
Some(needles) => needles,
@@ -300,8 +238,9 @@ impl MemchrTest {
}
fn iter_one<'a, I, F>(&'a self, reverse: bool, f: F)
where F: FnOnce(u8, &'a [u8]) -> I,
I: Iterator<Item=usize>
where
F: FnOnce(u8, &'a [u8]) -> I,
I: Iterator<Item = usize>,
{
if let Some(ns) = self.needles(1) {
self.iter(reverse, f(ns[0], self.corpus.as_bytes()));
@@ -309,8 +248,9 @@ impl MemchrTest {
}
fn iter_two<'a, I, F>(&'a self, reverse: bool, f: F)
where F: FnOnce(u8, u8, &'a [u8]) -> I,
I: Iterator<Item=usize>
where
F: FnOnce(u8, u8, &'a [u8]) -> I,
I: Iterator<Item = usize>,
{
if let Some(ns) = self.needles(2) {
self.iter(reverse, f(ns[0], ns[1], self.corpus.as_bytes()));
@@ -318,8 +258,9 @@ impl MemchrTest {
}
fn iter_three<'a, I, F>(&'a self, reverse: bool, f: F)
where F: FnOnce(u8, u8, u8, &'a [u8]) -> I,
I: Iterator<Item=usize>
where
F: FnOnce(u8, u8, u8, &'a [u8]) -> I,
I: Iterator<Item = usize>,
{
if let Some(ns) = self.needles(3) {
self.iter(reverse, f(ns[0], ns[1], ns[2], self.corpus.as_bytes()));
@@ -329,7 +270,7 @@ impl MemchrTest {
/// Test that the positions yielded by the given iterator match the
/// positions in this test. If reverse is true, then reverse the positions
/// before comparing them.
fn iter<I: Iterator<Item=usize>>(&self, reverse: bool, it: I) {
fn iter<I: Iterator<Item = usize>>(&self, reverse: bool, it: I) {
assert_eq!(
self.positions(0, reverse),
it.collect::<Vec<usize>>(),
@@ -405,14 +346,13 @@ impl MemchrTest {
/// alignment are offset by the alignment. Positions less than the
/// alignment are dropped.
fn positions(&self, align: usize, reverse: bool) -> Vec<usize> {
let positions =
if reverse {
let mut positions = self.positions.to_vec();
positions.reverse();
positions
} else {
self.positions.to_vec()
};
let positions = if reverse {
let mut positions = self.positions.to_vec();
positions.reverse();
positions
} else {
self.positions.to_vec()
};
positions
.into_iter()
.filter(|&p| p >= align)
+13 -5
View File
@@ -169,8 +169,10 @@ pub unsafe fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
#[target_feature(enable = "avx2")]
pub unsafe fn memchr3(
n1: u8, n2: u8, n3: u8,
haystack: &[u8]
n1: u8,
n2: u8,
n3: u8,
haystack: &[u8],
) -> Option<usize> {
let vn1 = _mm256_set1_epi8(n1 as i8);
let vn2 = _mm256_set1_epi8(n2 as i8);
@@ -231,7 +233,9 @@ pub unsafe fn memchr3(
ptr = ptr.add(loop_size);
}
while ptr <= end_ptr.sub(VECTOR_SIZE) {
if let Some(i) = forward_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3) {
if let Some(i) =
forward_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3)
{
return Some(i);
}
ptr = ptr.add(VECTOR_SIZE);
@@ -394,7 +398,9 @@ pub unsafe fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
#[target_feature(enable = "avx2")]
pub unsafe fn memrchr3(
n1: u8, n2: u8, n3: u8,
n1: u8,
n2: u8,
n3: u8,
haystack: &[u8],
) -> Option<usize> {
let vn1 = _mm256_set1_epi8(n1 as i8);
@@ -458,7 +464,9 @@ pub unsafe fn memrchr3(
}
while ptr >= start_ptr.add(VECTOR_SIZE) {
ptr = ptr.sub(VECTOR_SIZE);
if let Some(i) = reverse_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3) {
if let Some(i) =
reverse_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3)
{
return Some(i);
}
}
+16 -2
View File
@@ -86,7 +86,14 @@ pub fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
#[inline(always)]
pub fn memchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option<usize> {
ifunc!(fn(u8, u8, u8, &[u8]) -> Option<usize>, memchr3, haystack, n1, n2, n3)
ifunc!(
fn(u8, u8, u8, &[u8]) -> Option<usize>,
memchr3,
haystack,
n1,
n2,
n3
)
}
#[inline(always)]
@@ -101,5 +108,12 @@ pub fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
#[inline(always)]
pub fn memrchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option<usize> {
ifunc!(fn(u8, u8, u8, &[u8]) -> Option<usize>, memrchr3, haystack, n1, n2, n3)
ifunc!(
fn(u8, u8, u8, &[u8]) -> Option<usize>,
memrchr3,
haystack,
n1,
n2,
n3
)
}
+13 -5
View File
@@ -259,8 +259,10 @@ pub unsafe fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
#[target_feature(enable = "sse2")]
pub unsafe fn memchr3(
n1: u8, n2: u8, n3: u8,
haystack: &[u8]
n1: u8,
n2: u8,
n3: u8,
haystack: &[u8],
) -> Option<usize> {
let vn1 = _mm_set1_epi8(n1 as i8);
let vn2 = _mm_set1_epi8(n2 as i8);
@@ -321,7 +323,9 @@ pub unsafe fn memchr3(
ptr = ptr.add(loop_size);
}
while ptr <= end_ptr.sub(VECTOR_SIZE) {
if let Some(i) = forward_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3) {
if let Some(i) =
forward_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3)
{
return Some(i);
}
ptr = ptr.add(VECTOR_SIZE);
@@ -484,7 +488,9 @@ pub unsafe fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
#[target_feature(enable = "sse2")]
pub unsafe fn memrchr3(
n1: u8, n2: u8, n3: u8,
n1: u8,
n2: u8,
n3: u8,
haystack: &[u8],
) -> Option<usize> {
let vn1 = _mm_set1_epi8(n1 as i8);
@@ -548,7 +554,9 @@ pub unsafe fn memrchr3(
}
while ptr >= start_ptr.add(VECTOR_SIZE) {
ptr = ptr.sub(VECTOR_SIZE);
if let Some(i) = reverse_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3) {
if let Some(i) =
reverse_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3)
{
return Some(i);
}
}