Skip to content

Commit

Permalink
chore: Remove explicit lazy_static in project (#11243)
Browse files Browse the repository at this point in the history
* Remove explicit lazy_static in project

This commit removes the use of lazy_static! in favor of OnceCell's Lazy. They
are functionally similar although OnceCell's implementation has better
characteristics in contended situations. This change was motivated by
examination of the profiling results from #11206 where a regex to do with date
parsing was seen before a CPU quiet period. It's unknown if this change will
materially impact that, however we should no longer use lazy_static and so the
time seemed ripe.

Signed-off-by: Brian L. Troutwine <brian@troutwine.us>

* unused ding in vrl/stdlib

Signed-off-by: Brian L. Troutwine <brian@troutwine.us>
  • Loading branch information
blt committed Feb 8, 2022
1 parent 1a7bf03 commit ed0ca37
Show file tree
Hide file tree
Showing 40 changed files with 244 additions and 264 deletions.
10 changes: 4 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ indexmap = { version = "~1.8.0", default-features = false, features = ["serde"]
indoc = { version = "1.0.3", default-features = false }
inventory = { version = "0.1.10", default-features = false }
k8s-openapi = { version = "0.14.0", default-features = true, features = ["api", "v1_16"], optional = true }
lazy_static = { version = "1.4.0", default-features = false }
listenfd = { version = "0.5.0", default-features = false, optional = true }
logfmt = { version = "0.0.2", default-features = false, optional = true }
lru = { version = "0.7.2", default-features = false, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/search-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ pest_derive = "2.1.0"
ordered-float = "2"
regex = "1"
itertools = "0.10.3"
lazy_static = { version = "1.4.0", default-features = false }
once_cell = { version = "1.9", default-features = false }
9 changes: 4 additions & 5 deletions lib/datadog/search-syntax/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use regex::Regex;

use crate::grammar::{unescape, DEFAULT_FIELD};
Expand Down Expand Up @@ -357,11 +358,9 @@ pub struct LuceneClause {
pub node: QueryNode,
}

static ESCAPE_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^\"(.+)\"$").unwrap());

/// Escapes surrounding `"` quotes when distinguishing between quoted terms isn't needed.
fn escape_quotes<T: AsRef<str>>(value: T) -> String {
lazy_static::lazy_static! {
static ref RE: Regex = Regex::new("^\"(.+)\"$").unwrap();
}

RE.replace_all(value.as_ref(), "$1").to_string()
ESCAPE_RE.replace_all(value.as_ref(), "$1").to_string()
}
2 changes: 1 addition & 1 deletion lib/lookup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ snafu = { version = "0.7", default-features = false }
regex = { version = "1.5.4", default-features = false, features = ["std", "perf"] }
inherent = "1.0"
lalrpop-util = { version = "0.19.7", features = ["lexer"] }
lazy_static = "1.3.0"
once_cell = { version = "1.9" }
quickcheck = { version = "1.0.3", optional = true }

[dev-dependencies]
Expand Down
7 changes: 3 additions & 4 deletions lib/lookup/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;

lazy_static! {
static ref VALID_FIELD: Regex = Regex::new("^[0-9]*[a-zA-Z_][0-9a-zA-Z_]*$").unwrap();
}
static VALID_FIELD: Lazy<Regex> =
Lazy::new(|| Regex::new("^[0-9]*[a-zA-Z_][0-9a-zA-Z_]*$").unwrap());

/// A valid fieldname can contain alphanumeric characters and an underscore.
/// It may start with a number, but has to consist of more than just a number.
Expand Down
9 changes: 5 additions & 4 deletions lib/lookup/src/lookup_buf/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fs, io::Read, path::Path, str::FromStr};

use once_cell::sync::Lazy;
use quickcheck::{QuickCheck, TestResult};
use tracing::trace;

Expand All @@ -8,8 +9,8 @@ use crate::*;
const SUFFICIENTLY_COMPLEX: &str =
r#"regular."quoted"."quoted but spaces"."quoted.but.periods".lookup[0].nested_lookup[0][0]"#;

lazy_static::lazy_static! {
static ref SUFFICIENTLY_DECOMPOSED: [SegmentBuf; 9] = [
static SUFFICIENTLY_DECOMPOSED: Lazy<[SegmentBuf; 9]> = Lazy::new(|| {
[
SegmentBuf::from(r#"regular"#.to_string()),
SegmentBuf::from(r#""quoted""#.to_string()),
SegmentBuf::from(r#""quoted but spaces""#.to_string()),
Expand All @@ -19,8 +20,8 @@ lazy_static::lazy_static! {
SegmentBuf::from(r#"nested_lookup"#.to_string()),
SegmentBuf::from(0),
SegmentBuf::from(0),
];
}
]
});

#[test]
fn field_is_quoted() {
Expand Down
13 changes: 6 additions & 7 deletions lib/lookup/src/lookup_view/test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::*;
use once_cell::sync::Lazy;
use std::{fs, io::Read, path::Path};

use tracing::trace;

use crate::*;

const SUFFICIENTLY_COMPLEX: &str = r#"regular."quoted"."quoted but spaces"."quoted.but.periods".lookup[0].00numericstart.nested_lookup[0][0]"#;

lazy_static::lazy_static! {
static ref SUFFICIENTLY_DECOMPOSED: [Segment<'static>; 10] = [
static SUFFICIENTLY_DECOMPOSED: Lazy<[Segment<'static>; 10]> = Lazy::new(|| {
[
Segment::from(r#"regular"#),
Segment::from(r#""quoted""#),
Segment::from(r#""quoted but spaces""#),
Expand All @@ -18,8 +17,8 @@ lazy_static::lazy_static! {
Segment::from(r#"nested_lookup"#),
Segment::from(0),
Segment::from(0),
];
}
]
});

#[test]
fn field_is_quoted() {
Expand Down
1 change: 0 additions & 1 deletion lib/vector-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ getset = { version = "0.1.2", default-features = false }
http = { version = "0.2.6", default-features = false }
hyper-proxy = { version = "0.9.1", default-features = false, features = ["openssl-tls"] }
indexmap = { version = "~1.8.0", default-features = false, features = ["serde"] }
lazy_static = { version = "1.4.0", default-features = false }
lookup = { path = "../lookup", features = ["arbitrary"] }
metrics = { version = "0.17.1", default-features = false, features = ["std"]}
metrics-tracing-context = { version = "0.9.0", default-features = false }
Expand Down
7 changes: 2 additions & 5 deletions lib/vector-core/src/config/log_schema.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use getset::{Getters, Setters};
use once_cell::sync::OnceCell;
use once_cell::sync::{Lazy, OnceCell};
use serde::{Deserialize, Serialize};

static LOG_SCHEMA: OnceCell<LogSchema> = OnceCell::new();

lazy_static::lazy_static! {
static ref LOG_SCHEMA_DEFAULT: LogSchema = LogSchema::default();
}
static LOG_SCHEMA_DEFAULT: Lazy<LogSchema> = Lazy::new(LogSchema::default);

/// Loads Log Schema from configurations and sets global schema. Once this is
/// done, configurations can be correctly loaded using configured log schema
Expand Down
9 changes: 5 additions & 4 deletions lib/vector-core/src/event/legacy_lookup/test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use once_cell::sync::Lazy;
use std::{fs, io::Read, path::Path};

use super::*;

const SUFFICIENTLY_COMPLEX: &str =
r#"regular."quoted"."quoted but spaces"."quoted.but.periods".lookup[0].nested_lookup[0][0]"#;
lazy_static::lazy_static! {
static ref SUFFICIENTLY_DECOMPOSED: [Segment; 9] = [
static SUFFICIENTLY_DECOMPOSED: Lazy<[Segment; 9]> = Lazy::new(|| {
[
Segment::field(r#"regular"#.to_string()),
Segment::field(r#""quoted""#.to_string()),
Segment::field(r#""quoted but spaces""#.to_string()),
Expand All @@ -15,8 +16,8 @@ lazy_static::lazy_static! {
Segment::field(r#"nested_lookup"#.to_string()),
Segment::index(0),
Segment::index(0),
];
}
]
});

#[test]
fn zero_len_not_allowed() {
Expand Down
4 changes: 2 additions & 2 deletions lib/vrl/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ path = "src/main.rs"
bytes = "1.1.0"
exitcode = "1"
indoc = "1.0.3"
lazy_static = { version = "1", optional = true }
once_cell = { version = "1.9", optional = true }
prettytable-rs = { version = "0.8", default-features = false, optional = true }
regex = { version = "1", default-features = false, optional = true }
rustyline = { version = "9", default-features = false, optional = true }
Expand All @@ -31,5 +31,5 @@ path = "../stdlib"

[features]
default = ["repl"]
repl = ["lazy_static", "prettytable-rs", "regex", "rustyline", "webbrowser"]
repl = ["once_cell", "prettytable-rs", "regex", "rustyline", "webbrowser"]
vrl-vm = []
12 changes: 6 additions & 6 deletions lib/vrl/cli/src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow::{self, Borrowed, Owned};

use indoc::indoc;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use prettytable::{format, Cell, Row, Table};
use regex::Regex;
use rustyline::{
Expand All @@ -16,16 +16,16 @@ use vector_common::TimeZone;
use vrl::{diagnostic::Formatter, state, value, Runtime, Target, Value};

// Create a list of all possible error values for potential docs lookup
lazy_static! {
static ref ERRORS: Vec<String> = [
static ERRORS: Lazy<Vec<String>> = Lazy::new(|| {
[
100, 101, 102, 103, 104, 105, 106, 107, 108, 110, 203, 204, 205, 206, 207, 208, 209, 300,
301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 400, 401, 402, 403,
601, 620, 630, 640, 650, 651, 652, 660, 701
601, 620, 630, 640, 650, 651, 652, 660, 701,
]
.iter()
.map(|i| i.to_string())
.collect();
}
.collect()
});

const DOCS_URL: &str = "https://vector.dev/docs/reference/vrl";
const ERRORS_URL_ROOT: &str = "https://errors.vrl.dev";
Expand Down
24 changes: 12 additions & 12 deletions lib/vrl/stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ grok = { version = "1", optional = true }
hex = { version = "0.4", optional = true }
hostname = { version = "0.3", optional = true }
indexmap = { version = "~1.8.0", default-features = false, optional = true}
lazy_static = { version = "1", optional = true }
md-5 = { version = "0.10", optional = true }
nom = { version = "7", optional = true }
percent-encoding = { version = "2.1", optional = true }
once_cell = { version = "1.9", optional = true }
regex = { version = "1", optional = true }
rust_decimal = { version = "1", optional = true }
serde_json = { version = "1", optional = true }
Expand Down Expand Up @@ -208,7 +208,7 @@ integer = []
ip_aton = []
ip_cidr_contains = ["cidr-utils"]
ip_ntoa = []
ip_subnet = ["lazy_static", "regex"]
ip_subnet = ["once_cell", "regex"]
ip_to_ipv6 = []
ipv6_to_ipv4 = []
is_array = []
Expand All @@ -227,28 +227,28 @@ log = ["tracing"]
match = ["regex"]
match_any = ["regex"]
match_array = ["regex"]
match_datadog_query = ["datadog-search-syntax", "datadog-filter", "lazy_static", "regex"]
match_datadog_query = ["datadog-search-syntax", "datadog-filter", "once_cell", "regex"]
md5 = ["md-5", "hex"]
merge = []
now = ["chrono"]
object = []
parse_apache_log = ["chrono", "lazy_static", "regex", "vector_common/conversion"]
parse_apache_log = ["chrono", "once_cell", "regex", "vector_common/conversion"]
parse_aws_alb_log = ["nom"]
parse_aws_cloudwatch_log_subscription_message = ["serde_json", "vector_common/aws_cloudwatch_logs_subscription", "vector_common/btreemap"]
parse_aws_vpc_flow_log = []
parse_common_log = ["chrono", "lazy_static", "regex", "vector_common/conversion"]
parse_common_log = ["chrono", "once_cell", "regex", "vector_common/conversion"]
parse_csv = ["csv"]
parse_duration = ["rust_decimal", "lazy_static", "regex"]
parse_glog = ["chrono", "lazy_static", "regex"]
parse_duration = ["rust_decimal", "once_cell", "regex"]
parse_glog = ["chrono", "once_cell", "regex"]
parse_grok = ["grok"]
parse_groks = ["grok", "datadog-grok"]
parse_int = []
parse_json = ["serde_json"]
parse_key_value = ["nom"]
parse_klog = ["chrono", "lazy_static", "regex"]
parse_klog = ["chrono", "once_cell", "regex"]
parse_linux_authorization = ["parse_syslog", "chrono", "vector_common/conversion"]
parse_logfmt = ["parse_key_value"]
parse_nginx_log = ["chrono", "regex", "lazy_static", "vector_common/conversion"]
parse_nginx_log = ["chrono", "regex", "once_cell", "vector_common/conversion"]
parse_query_string = ["url"]
parse_regex = ["regex"]
parse_regex_all = ["regex"]
Expand All @@ -257,10 +257,10 @@ parse_syslog = ["syslog_loose", "chrono", "vector_common/conversion"]
parse_timestamp = ["vector_common/conversion"]
parse_tokens = ["vector_common/tokenize"]
parse_url = ["url"]
parse_user_agent = ["woothee","uaparser","lazy_static"]
parse_xml = ["roxmltree", "lazy_static", "regex"]
parse_user_agent = ["woothee","uaparser","once_cell"]
parse_xml = ["roxmltree", "once_cell", "regex"]
push = []
redact = ["lazy_static", "regex"]
redact = ["once_cell", "regex"]
remove = ["vector_common/btreemap"]
replace = []
reverse_dns = ["dns-lookup"]
Expand Down
6 changes: 2 additions & 4 deletions lib/vrl/stdlib/src/ip_subnet.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;
use vrl::prelude::*;

lazy_static! {
static ref RE: Regex = Regex::new(r"/(?P<subnet>\d*)").unwrap();
}
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"/(?P<subnet>\d*)").unwrap());

#[derive(Clone, Copy, Debug)]
pub struct IpSubnet;
Expand Down

0 comments on commit ed0ca37

Please sign in to comment.