Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move from lazy_static to once_cell::Lazy #249

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ dashmap = { version = "5", optional = true }
env_logger = { version = "0.9", default-features = false, optional = true }
indexmap = { version = "1.0", optional = true }
itoa = "1"
lazy_static = "1.3.0"
log = "0.4"
num_cpus = { version = "1.10", optional = true }
num-format = { version = "0.4", default-features = false }
quick-xml = { version = "0.23", default-features = false }
rgb = "0.8.13"
str_stack = "0.1"
clap = { version = "3.0.1", optional = true, features = ["derive"] }
once_cell = "1.12.0"

[dev-dependencies]
assert_cmd = "2"
Expand Down
6 changes: 2 additions & 4 deletions benches/collapse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ use std::io::{self, Read};

use criterion::*;
use inferno::collapse::{dtrace, perf, sample, Collapse};
use lazy_static::lazy_static;
use libflate::gzip::Decoder;
use once_cell::sync::Lazy;

const INFILE_DTRACE: &str = "flamegraph/example-dtrace-stacks.txt";
const INFILE_PERF: &str = "flamegraph/example-perf-stacks.txt.gz";
const INFILE_SAMPLE: &str = "tests/data/collapse-sample/large.txt.gz";
const SAMPLE_SIZE: usize = 100;

lazy_static! {
static ref NTHREADS: usize = num_cpus::get();
}
static NTHREADS: Lazy<usize> = Lazy::new(num_cpus::get);

fn read_infile(infile: &str, buf: &mut Vec<u8>) -> io::Result<()> {
let mut f = File::open(infile)?;
Expand Down
6 changes: 2 additions & 4 deletions src/bin/collapse-dtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use clap::Parser;
use env_logger::Env;
use inferno::collapse::dtrace::{Folder, Options};
use inferno::collapse::{Collapse, DEFAULT_NTHREADS};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;

lazy_static! {
static ref NTHREADS: String = format!("{}", *DEFAULT_NTHREADS);
}
static NTHREADS: Lazy<String> = Lazy::new(|| format!("{}", *DEFAULT_NTHREADS));

#[derive(Debug, Parser)]
#[clap(
Expand Down
6 changes: 2 additions & 4 deletions src/bin/collapse-guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use clap::Parser;
use env_logger::Env;
use inferno::collapse::guess::{Folder, Options};
use inferno::collapse::{Collapse, DEFAULT_NTHREADS};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;

lazy_static! {
static ref NTHREADS: String = format!("{}", *DEFAULT_NTHREADS);
}
static NTHREADS: Lazy<String> = Lazy::new(|| format!("{}", *DEFAULT_NTHREADS));

#[derive(Debug, Parser)]
#[clap(
Expand Down
6 changes: 2 additions & 4 deletions src/bin/collapse-perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use clap::Parser;
use env_logger::Env;
use inferno::collapse::perf::{Folder, Options};
use inferno::collapse::{Collapse, DEFAULT_NTHREADS};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;

lazy_static! {
static ref NTHREADS: String = format!("{}", *DEFAULT_NTHREADS);
}
static NTHREADS: Lazy<String> = Lazy::new(|| format!("{}", *DEFAULT_NTHREADS));

#[derive(Debug, Parser)]
#[clap(
Expand Down
15 changes: 5 additions & 10 deletions src/collapse/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::Arc;
use ahash::AHashMap;
#[cfg(feature = "multithreaded")]
use dashmap::DashMap;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;

macro_rules! invalid_data_error {
($($arg:tt)*) => {{
Expand Down Expand Up @@ -39,16 +39,11 @@ const NBYTES_PER_STACK_GUESS: usize = 1024;
const RUST_HASH_LENGTH: usize = 17;

#[cfg(feature = "multithreaded")]
lazy_static! {
#[doc(hidden)]
pub static ref DEFAULT_NTHREADS: usize = num_cpus::get();
}

#[doc(hidden)]
pub static DEFAULT_NTHREADS: Lazy<usize> = Lazy::new(|| num_cpus::get());
#[cfg(not(feature = "multithreaded"))]
lazy_static! {
#[doc(hidden)]
pub static ref DEFAULT_NTHREADS: usize = 1;
}
#[doc(hidden)]
pub static DEFAULT_NTHREADS: Lazy<usize> = Lazy::new(|| 1);

/// Private trait for internal library authors.
///
Expand Down
30 changes: 14 additions & 16 deletions src/collapse/dtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,29 +406,27 @@ mod tests {
use std::fs;
use std::path::PathBuf;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use pretty_assertions::assert_eq;
use rand::prelude::*;

use super::*;
use crate::collapse::common;
use crate::collapse::Collapse;

lazy_static! {
static ref INPUT: Vec<PathBuf> = {
[
"./flamegraph/example-dtrace-stacks.txt",
"./tests/data/collapse-dtrace/flamegraph-bug.txt",
"./tests/data/collapse-dtrace/hex-addresses.txt",
"./tests/data/collapse-dtrace/java.txt",
"./tests/data/collapse-dtrace/only-header-lines.txt",
"./tests/data/collapse-dtrace/scope_with_no_argument_list.txt",
]
.iter()
.map(PathBuf::from)
.collect::<Vec<_>>()
};
}
static INPUT: Lazy<Vec<PathBuf>> = Lazy::new(|| {
[
"./flamegraph/example-dtrace-stacks.txt",
"./tests/data/collapse-dtrace/flamegraph-bug.txt",
"./tests/data/collapse-dtrace/hex-addresses.txt",
"./tests/data/collapse-dtrace/java.txt",
"./tests/data/collapse-dtrace/only-header-lines.txt",
"./tests/data/collapse-dtrace/scope_with_no_argument_list.txt",
]
.iter()
.map(PathBuf::from)
.collect::<Vec<_>>()
});

#[test]
fn cpp_test() {
Expand Down
56 changes: 27 additions & 29 deletions src/collapse/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ mod tests {
use std::io::Read;
use std::path::PathBuf;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use pretty_assertions::assert_eq;
use rand::prelude::*;

Expand Down Expand Up @@ -743,34 +743,32 @@ mod tests {
}
}

lazy_static! {
static ref INPUT: Vec<PathBuf> = {
[
"./flamegraph/example-perf-stacks.txt.gz",
"./flamegraph/test/perf-cycles-instructions-01.txt",
"./flamegraph/test/perf-dd-stacks-01.txt",
"./flamegraph/test/perf-funcab-cmd-01.txt",
"./flamegraph/test/perf-funcab-pid-01.txt",
"./flamegraph/test/perf-iperf-stacks-pidtid-01.txt",
"./flamegraph/test/perf-java-faults-01.txt",
"./flamegraph/test/perf-java-stacks-01.txt",
"./flamegraph/test/perf-java-stacks-02.txt",
"./flamegraph/test/perf-js-stacks-01.txt",
"./flamegraph/test/perf-mirageos-stacks-01.txt",
"./flamegraph/test/perf-numa-stacks-01.txt",
"./flamegraph/test/perf-rust-Yamakaky-dcpu.txt",
"./flamegraph/test/perf-vertx-stacks-01.txt",
"./tests/data/collapse-perf/empty-line.txt",
"./tests/data/collapse-perf/go-stacks.txt",
"./tests/data/collapse-perf/java-inline.txt",
"./tests/data/collapse-perf/weird-stack-line.txt",
"./tests/data/collapse-perf/cpp-stacks-std-function.txt",
]
.iter()
.map(PathBuf::from)
.collect::<Vec<_>>()
};
}
static INPUT: Lazy<Vec<PathBuf>> = Lazy::new(|| {
[
"./flamegraph/example-perf-stacks.txt.gz",
"./flamegraph/test/perf-cycles-instructions-01.txt",
"./flamegraph/test/perf-dd-stacks-01.txt",
"./flamegraph/test/perf-funcab-cmd-01.txt",
"./flamegraph/test/perf-funcab-pid-01.txt",
"./flamegraph/test/perf-iperf-stacks-pidtid-01.txt",
"./flamegraph/test/perf-java-faults-01.txt",
"./flamegraph/test/perf-java-stacks-01.txt",
"./flamegraph/test/perf-java-stacks-02.txt",
"./flamegraph/test/perf-js-stacks-01.txt",
"./flamegraph/test/perf-mirageos-stacks-01.txt",
"./flamegraph/test/perf-numa-stacks-01.txt",
"./flamegraph/test/perf-rust-Yamakaky-dcpu.txt",
"./flamegraph/test/perf-vertx-stacks-01.txt",
"./tests/data/collapse-perf/empty-line.txt",
"./tests/data/collapse-perf/go-stacks.txt",
"./tests/data/collapse-perf/java-inline.txt",
"./tests/data/collapse-perf/weird-stack-line.txt",
"./tests/data/collapse-perf/cpp-stacks-std-function.txt",
]
.iter()
.map(PathBuf::from)
.collect::<Vec<_>>()
});

#[test]
fn test_collapse_multi_perf() -> io::Result<()> {
Expand Down
7 changes: 3 additions & 4 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ pub mod defaults {
);
)*


#[doc(hidden)]
pub mod str {
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
$(
lazy_static! {
pub static ref $name: String = ($val).to_string();
}
pub static $name: Lazy<String> = Lazy::new(|| ($val).to_string());
)*
}
}
Expand Down