From 15f8f1c45dddab2bfb4a53969486bddcb13f8256 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 10 Jan 2020 17:12:15 -0500 Subject: [PATCH] style: switch to using rustfmt --- bench/src/bench.rs | 60 ++++++--------------- bench/src/imp.rs | 2 +- bench/src/inputs.rs | 12 ++--- build.rs | 2 +- rustfmt.toml | 2 + src/iter.rs | 10 ++-- src/lib.rs | 11 ++-- src/naive.rs | 24 +++------ src/tests/iter.rs | 11 ++-- src/tests/mod.rs | 124 ++++++++++++-------------------------------- src/x86/avx.rs | 18 +++++-- src/x86/mod.rs | 18 ++++++- src/x86/sse2.rs | 18 +++++-- 13 files changed, 115 insertions(+), 197 deletions(-) create mode 100644 rustfmt.toml diff --git a/bench/src/bench.rs b/bench/src/bench.rs index cecae9c..99bb4c5 100644 --- a/bench/src/bench.rs +++ b/bench/src/bench.rs @@ -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(); diff --git a/bench/src/imp.rs b/bench/src/imp.rs index de1b43e..776d3c1 100644 --- a/bench/src/imp.rs +++ b/bench/src/imp.rs @@ -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 { diff --git a/bench/src/inputs.rs b/bench/src/inputs.rs index 49a777a..d85a5fc 100644 --- a/bench/src/inputs.rs +++ b/bench/src/inputs.rs @@ -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: &[], }; diff --git a/build.rs b/build.rs index a8a6341..4ae3184 100644 --- a/build.rs +++ b/build.rs @@ -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 { diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..aa37a21 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 79 +use_small_heuristics = "max" diff --git a/src/iter.rs b/src/iter.rs index 0ee5f86..6217ae4 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -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 } } } diff --git a/src/lib.rs b/src/lib.rs index 216c7ed..9274819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) } diff --git a/src/naive.rs b/src/naive.rs index 68cecf0..3f3053d 100644 --- a/src/naive.rs +++ b/src/naive.rs @@ -1,37 +1,25 @@ #![allow(dead_code)] pub fn memchr(n1: u8, haystack: &[u8]) -> Option { - haystack - .iter() - .position(|&b| b == n1) + haystack.iter().position(|&b| b == n1) } pub fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option { - 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 { - 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 { - haystack - .iter() - .rposition(|&b| b == n1) + haystack.iter().rposition(|&b| b == n1) } pub fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option { - 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 { - haystack - .iter() - .rposition(|&b| b == n1 || b == n2 || b == n3) + haystack.iter().rposition(|&b| b == n1 || b == n2 || b == n3) } diff --git a/src/tests/iter.rs b/src/tests/iter.rs index 07339e5..8f33500 100644 --- a/src/tests/iter.rs +++ b/src/tests/iter.rs @@ -160,8 +160,9 @@ quickcheck! { // Return a vector with the concatenation of the fronts and the reverse of the // backs. fn double_ended_take(mut iter: I, take_side: J) -> Vec - where I: DoubleEndedIterator, - J: Iterator, +where + I: DoubleEndedIterator, + J: Iterator, { let mut found_front = Vec::new(); let mut found_back = Vec::new(); @@ -191,7 +192,7 @@ fn double_ended_take(mut iter: I, take_side: J) -> Vec fn positions1<'a>( n1: u8, haystack: &'a [u8], -) -> Box + 'a> { +) -> Box + 'a> { let it = haystack .iter() .enumerate() @@ -204,7 +205,7 @@ fn positions2<'a>( n1: u8, n2: u8, haystack: &'a [u8], -) -> Box + 'a> { +) -> Box + 'a> { let it = haystack .iter() .enumerate() @@ -218,7 +219,7 @@ fn positions3<'a>( n2: u8, n3: u8, haystack: &'a [u8], -) -> Box + 'a> { +) -> Box + 'a> { let it = haystack .iter() .enumerate() diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c203336..82c1a24 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -44,61 +44,21 @@ fn memchr_tests() -> Vec { /// 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 Option>( - &self, - reverse: bool, - f: F, - ) { + fn one Option>(&self, reverse: bool, f: F) { let needles = match self.needles(1) { None => return, Some(needles) => needles, @@ -248,11 +190,7 @@ impl MemchrTest { } } - fn two Option>( - &self, - reverse: bool, - f: F, - ) { + fn two Option>(&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 + where + F: FnOnce(u8, &'a [u8]) -> I, + I: Iterator, { 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 + where + F: FnOnce(u8, u8, &'a [u8]) -> I, + I: Iterator, { 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 + where + F: FnOnce(u8, u8, u8, &'a [u8]) -> I, + I: Iterator, { 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>(&self, reverse: bool, it: I) { + fn iter>(&self, reverse: bool, it: I) { assert_eq!( self.positions(0, reverse), it.collect::>(), @@ -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 { - 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) diff --git a/src/x86/avx.rs b/src/x86/avx.rs index 776d11e..e3d8e89 100644 --- a/src/x86/avx.rs +++ b/src/x86/avx.rs @@ -169,8 +169,10 @@ pub unsafe fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option { #[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 { 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 { #[target_feature(enable = "avx2")] pub unsafe fn memrchr3( - n1: u8, n2: u8, n3: u8, + n1: u8, + n2: u8, + n3: u8, haystack: &[u8], ) -> Option { 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); } } diff --git a/src/x86/mod.rs b/src/x86/mod.rs index b8bdd7b..7a7fbfe 100644 --- a/src/x86/mod.rs +++ b/src/x86/mod.rs @@ -86,7 +86,14 @@ pub fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option { #[inline(always)] pub fn memchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option { - ifunc!(fn(u8, u8, u8, &[u8]) -> Option, memchr3, haystack, n1, n2, n3) + ifunc!( + fn(u8, u8, u8, &[u8]) -> Option, + memchr3, + haystack, + n1, + n2, + n3 + ) } #[inline(always)] @@ -101,5 +108,12 @@ pub fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option { #[inline(always)] pub fn memrchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option { - ifunc!(fn(u8, u8, u8, &[u8]) -> Option, memrchr3, haystack, n1, n2, n3) + ifunc!( + fn(u8, u8, u8, &[u8]) -> Option, + memrchr3, + haystack, + n1, + n2, + n3 + ) } diff --git a/src/x86/sse2.rs b/src/x86/sse2.rs index ac24f5a..76f5a78 100644 --- a/src/x86/sse2.rs +++ b/src/x86/sse2.rs @@ -259,8 +259,10 @@ pub unsafe fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option { #[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 { 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 { #[target_feature(enable = "sse2")] pub unsafe fn memrchr3( - n1: u8, n2: u8, n3: u8, + n1: u8, + n2: u8, + n3: u8, haystack: &[u8], ) -> Option { 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); } }