Skip to content

Commit bf51da7

Browse files
committed
Use once_cell in rayon-demo
1 parent de7f24f commit bf51da7

File tree

5 files changed

+28
-32
lines changed

5 files changed

+28
-32
lines changed

ci/compat-Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rayon-demo/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cgmath = "0.18"
1212
docopt = "1"
1313
fixedbitset = "0.4"
1414
glium = "0.32"
15-
lazy_static = "1"
15+
once_cell = "1.17.1"
1616
rand = "0.8"
1717
rand_xorshift = "0.3"
1818
regex = "1"

rayon-demo/src/find/mod.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
macro_rules! make_tests {
44
($n:expr, $m:ident) => {
55
mod $m {
6+
use once_cell::sync::Lazy;
67
use rand::distributions::Standard;
78
use rand::Rng;
89
use rayon::prelude::*;
910
use test::Bencher;
1011

11-
lazy_static::lazy_static! {
12-
static ref HAYSTACK: Vec<[u32; $n]> = {
13-
let rng = crate::seeded_rng();
14-
rng.sample_iter(&Standard)
15-
.map(|x| {
16-
let mut result: [u32; $n] = [0; $n];
17-
result[0] = x;
18-
result
19-
})
20-
.take(10_000_000)
21-
.collect()
22-
};
23-
}
12+
static HAYSTACK: Lazy<Box<[[u32; $n]]>> = Lazy::new(|| {
13+
let rng = crate::seeded_rng();
14+
rng.sample_iter(&Standard)
15+
.map(|x| {
16+
let mut result: [u32; $n] = [0; $n];
17+
result[0] = x;
18+
result
19+
})
20+
.take(10_000_000)
21+
.collect()
22+
});
2423

2524
#[bench]
2625
fn parallel_find_first(b: &mut Bencher) {

rayon-demo/src/str_split.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
//! Some microbenchmarks for splitting strings
22
3+
use once_cell::sync::Lazy;
34
use rand::seq::SliceRandom;
45
use rayon::prelude::*;
56
use test::Bencher;
67

7-
lazy_static::lazy_static! {
8-
static ref HAYSTACK: String = {
9-
let mut rng = crate::seeded_rng();
10-
let mut bytes: Vec<u8> = "abcdefg ".bytes().cycle().take(1_000_000).collect();
11-
bytes.shuffle(&mut rng);
12-
String::from_utf8(bytes).unwrap()
13-
};
14-
static ref COUNT: usize = HAYSTACK.split(' ').count();
15-
}
8+
static HAYSTACK: Lazy<String> = Lazy::new(|| {
9+
let mut rng = crate::seeded_rng();
10+
let mut bytes: Vec<u8> = "abcdefg ".bytes().cycle().take(1_000_000).collect();
11+
bytes.shuffle(&mut rng);
12+
String::from_utf8(bytes).unwrap()
13+
});
14+
15+
static COUNT: Lazy<usize> = Lazy::new(|| HAYSTACK.split(' ').count());
1616

1717
// Try multiple kinds of whitespace, but HAYSTACK only contains plain spaces.
1818
const WHITESPACE: &[char] = &['\r', '\n', ' ', '\t'];

rayon-demo/src/tsp/parser.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use once_cell::sync::Lazy;
12
use regex::Regex;
23
use std::collections::HashMap;
34
use std::str::{FromStr, Lines};
@@ -23,13 +24,9 @@ use super::weight::Weight;
2324
// NODE_COORD_SECTION
2425
// 1 11003.611100 42102.500000
2526

26-
lazy_static::lazy_static! {
27-
static ref HEADER: Regex = Regex::new(r"([A-Z_]+)\s*:(.*)").unwrap();
28-
}
27+
static HEADER: Lazy<Regex> = Lazy::new(|| Regex::new(r"([A-Z_]+)\s*:(.*)").unwrap());
2928

30-
lazy_static::lazy_static! {
31-
static ref COORD: Regex = Regex::new(r"([0-9]+) ([0-9.]+) ([0-9.]+)").unwrap();
32-
}
29+
static COORD: Lazy<Regex> = Lazy::new(|| Regex::new(r"([0-9]+) ([0-9.]+) ([0-9.]+)").unwrap());
3330

3431
pub fn parse_tsp_data(text: &str) -> Result<Graph, String> {
3532
let mut data = Data::new(text);

0 commit comments

Comments
 (0)