Skip to content

Commit 17223da

Browse files
authored
perf: cache SECRET_PATTERNS's RegexSet (atuinsh#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).
1 parent 7be6694 commit 17223da

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

crates/atuin-client/src/history.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use atuin_common::record::DecryptedData;
88
use atuin_common::utils::uuid_v7;
99

1010
use eyre::{bail, eyre, Result};
11-
use regex::RegexSet;
1211

12+
use crate::secrets::SECRET_PATTERNS_RE;
13+
use crate::settings::Settings;
1314
use crate::utils::get_host_user;
14-
use crate::{secrets::SECRET_PATTERNS, settings::Settings};
1515
use time::OffsetDateTime;
1616

1717
mod builder;
@@ -374,13 +374,10 @@ impl History {
374374
}
375375

376376
pub fn should_save(&self, settings: &Settings) -> bool {
377-
let secret_regex = SECRET_PATTERNS.iter().map(|f| f.1);
378-
let secret_regex = RegexSet::new(secret_regex).expect("Failed to build secrets regex");
379-
380377
!(self.command.starts_with(' ')
381378
|| settings.history_filter.is_match(&self.command)
382379
|| settings.cwd_filter.is_match(&self.cwd)
383-
|| (secret_regex.is_match(&self.command)) && settings.secrets_filter)
380+
|| (settings.secrets_filter && SECRET_PATTERNS_RE.is_match(&self.command)))
384381
}
385382
}
386383

crates/atuin-client/src/secrets.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// This file will probably trigger a lot of scanners. Sorry.
22

3+
use regex::RegexSet;
4+
use std::sync::LazyLock;
5+
36
pub enum TestValue<'a> {
47
Single(&'a str),
58
Multiple(&'a [&'a str]),
69
}
710

8-
// A list of (name, regex, test), where test should match against regex
11+
/// A list of `(name, regex, test)`, where `test` should match against `regex`.
912
pub static SECRET_PATTERNS: &[(&str, &str, TestValue)] = &[
1013
(
1114
"AWS Access Key ID",
@@ -114,6 +117,12 @@ pub static SECRET_PATTERNS: &[(&str, &str, TestValue)] = &[
114117
),
115118
];
116119

120+
/// The `regex` expressions from [`SECRET_PATTERNS`] compiled into a `RegexSet`.
121+
pub static SECRET_PATTERNS_RE: LazyLock<RegexSet> = LazyLock::new(|| {
122+
let exprs = SECRET_PATTERNS.iter().map(|f| f.1);
123+
RegexSet::new(exprs).expect("Failed to build secrets regex")
124+
});
125+
117126
#[cfg(test)]
118127
mod tests {
119128
use regex::Regex;

0 commit comments

Comments
 (0)