Skip to content

Commit

Permalink
ref(glob): Switch glob3 implemention to pattern (#3937)
Browse files Browse the repository at this point in the history
Switches the `glob3` implementation to `Pattern`.
  • Loading branch information
Dav1dde authored Aug 16, 2024
1 parent fbd7092 commit 08af9f6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 46 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion relay-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ workspace = true

[dependencies]
chrono = { workspace = true }
globset = { workspace = true }
once_cell = { workspace = true }
regex-lite = { workspace = true }
relay-pattern = { workspace = true }
sentry-types = { workspace = true }
serde = { workspace = true }

Expand Down
43 changes: 10 additions & 33 deletions relay-common/src/glob3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
use std::fmt;
use std::sync::OnceLock;

use globset::GlobBuilder;
use regex_lite::{Regex, RegexBuilder};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Returns `true` if any of the patterns match the given message.
fn is_match(globs: &[Regex], message: &str) -> bool {
globs.iter().any(|regex| regex.is_match(message))
}
use relay_pattern::Pattern;

/// A list of patterns for glob matching.
#[derive(Clone, Default)]
pub struct GlobPatterns {
patterns: Vec<String>,
globs: OnceLock<Vec<Regex>>,
compiled: OnceLock<Vec<Pattern>>,
}

impl GlobPatterns {
/// Creates a new
pub fn new(patterns: Vec<String>) -> Self {
Self {
patterns,
globs: OnceLock::new(),
compiled: OnceLock::new(),
}
}

Expand All @@ -45,32 +40,14 @@ impl GlobPatterns {
return false;
}

let globs = self.globs.get_or_init(|| self.parse_globs());
is_match(globs, message)
}

/// Parses valid patterns from the list.
fn parse_globs(&self) -> Vec<Regex> {
let mut globs = Vec::with_capacity(self.patterns.len());

for pattern in &self.patterns {
let glob_result = GlobBuilder::new(pattern)
.case_insensitive(true)
.backslash_escape(true)
.build();

if let Ok(glob) = glob_result {
let regex_result = RegexBuilder::new(glob.regex())
.dot_matches_new_line(true)
.build();

if let Ok(regex) = regex_result {
globs.push(regex);
}
}
}
let compiled = self.compiled.get_or_init(|| {
self.patterns
.iter()
.filter_map(|p| Pattern::builder(p).case_insensitive(true).build().ok())
.collect()
});

globs
compiled.iter().any(|pattern| pattern.is_match(message))
}
}

Expand Down
1 change: 0 additions & 1 deletion relay-pattern/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ regex-lite = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
relay-common = { workspace = true }

[[bench]]
name = "benches"
Expand Down
7 changes: 0 additions & 7 deletions relay-pattern/benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion};

use relay_common::glob3::GlobPatterns;
use relay_pattern::Pattern;

fn bench(group: &mut BenchmarkGroup<'_, WallTime>, haystack: &str, needle: &str) {
group.bench_function("pattern", |b| {
let pattern = Pattern::new(needle).unwrap();
b.iter(|| assert!(pattern.is_match(haystack)))
});

group.bench_function("glob3", |b| {
let glob = GlobPatterns::new(vec![needle.to_owned()]);
assert!(glob.is_match(haystack)); // Force initialization
b.iter(|| assert!(glob.is_match(haystack)))
});
}

fn literal_match(c: &mut Criterion) {
Expand Down
4 changes: 2 additions & 2 deletions relay-pattern/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl fmt::Display for Error {
}

/// `Pattern` represents a successfully parsed Relay pattern.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Pattern {
pattern: String,
options: Options,
Expand Down Expand Up @@ -163,7 +163,7 @@ struct Options {
///
/// Certain patterns can be matched more efficiently while the complex
/// patterns fallback to a regex.
#[derive(Debug)]
#[derive(Debug, Clone)]
enum MatchStrategy {
/// The pattern is a single literal string.
///
Expand Down

0 comments on commit 08af9f6

Please sign in to comment.