aboutsummaryrefslogtreecommitdiffstats
path: root/crates/atuin-client/src/secrets.rs
diff options
context:
space:
mode:
authorDaniPopes <57450786+DaniPopes@users.noreply.github.com>2025-02-19 06:07:30 +0100
committerGitHub <noreply@github.com>2025-02-18 21:07:30 -0800
commit17223da04892aaf80a6372e8825deb952e4e7f0b (patch)
tree8296cc6e544132cce23eeda87bd89d538fd7e017 /crates/atuin-client/src/secrets.rs
parentchore(deps): bump lukemathwalker/cargo-chef (#2571) (diff)
downloadatuin-17223da04892aaf80a6372e8825deb952e4e7f0b.zip
perf: cache `SECRET_PATTERNS`'s `RegexSet` (#2570)
Improves the performance of `History::should_save` by constructing the `SECRET_PATTERNS` `RegexSet` only once with a `LazyLock`. This speeds up `atuin history prune` by ~100x (~7s to ~70ms on my machine) (lol).
Diffstat (limited to 'crates/atuin-client/src/secrets.rs')
-rw-r--r--crates/atuin-client/src/secrets.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/crates/atuin-client/src/secrets.rs b/crates/atuin-client/src/secrets.rs
index c3053b79..e249910d 100644
--- a/crates/atuin-client/src/secrets.rs
+++ b/crates/atuin-client/src/secrets.rs
@@ -1,11 +1,14 @@
// This file will probably trigger a lot of scanners. Sorry.
+use regex::RegexSet;
+use std::sync::LazyLock;
+
pub enum TestValue<'a> {
Single(&'a str),
Multiple(&'a [&'a str]),
}
-// A list of (name, regex, test), where test should match against regex
+/// A list of `(name, regex, test)`, where `test` should match against `regex`.
pub static SECRET_PATTERNS: &[(&str, &str, TestValue)] = &[
(
"AWS Access Key ID",
@@ -114,6 +117,12 @@ pub static SECRET_PATTERNS: &[(&str, &str, TestValue)] = &[
),
];
+/// The `regex` expressions from [`SECRET_PATTERNS`] compiled into a `RegexSet`.
+pub static SECRET_PATTERNS_RE: LazyLock<RegexSet> = LazyLock::new(|| {
+ let exprs = SECRET_PATTERNS.iter().map(|f| f.1);
+ RegexSet::new(exprs).expect("Failed to build secrets regex")
+});
+
#[cfg(test)]
mod tests {
use regex::Regex;