Skip to content

Visual Studio Code, rebuilt in Rust

Rust

perf(glob): parse each pattern once, as glob.ts CACHE does

rokarturd0eba62ab32f
diff --git a/native/crates/files/src/glob.rs b/native/crates/files/src/glob.rs
index 3c58c479bb5..1d3ac8416c3 100644
--- a/native/crates/files/src/glob.rs
+++ b/native/crates/files/src/glob.rs
@@ -3,8 +3,8 @@
//! sibling clauses, and exclude expressions with `!` exceptions. Paths are matched as given, on macOS (`/` separator,
//! `isLinux` false). Relative patterns (`IRelativePattern`) are not ported: callers pass folder-relative paths.
-use std::collections::HashSet;
-use std::sync::LazyLock;
+use std::collections::{HashMap, HashSet};
+use std::sync::{LazyLock, Mutex};
use regex::Regex;
use serde_json::Value;
@@ -15,7 +15,7 @@ pub const GLOB_SPLIT: char = '/';
const PATH_REGEX: &str = r"[/\\]";
const NO_PATH_REGEX: &str = r"[^/\\]";
-#[derive(Clone, Copy, Debug, Default)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Options {
pub trim_for_exclusions: bool,
pub ignore_case: bool,
@@ -308,7 +308,24 @@ static T3_2: LazyLock<Regex> =
static T4: LazyLock<Regex> = LazyLock::new(|| Regex::new(&format!(r"^\*\*((/{WORD}+)+)/?$")).expect("valid"));
static T5: LazyLock<Regex> = LazyLock::new(|| Regex::new(&format!(r"^({WORD}+(/{WORD}+)*)/?$")).expect("valid"));
+/// glob.ts `CACHE`: a pattern is parsed, and its regex compiled, once per options.
fn parse_pattern(pattern: &str, options: Options) -> Glob {
+ // shortcut: emptied when full where upstream's LRUCache drops the oldest entry; LRU if a workspace nears 10000 patterns.
+ static CACHE: LazyLock<Mutex<HashMap<(String, Options), Glob>>> = LazyLock::new(Mutex::default);
+ let key = (pattern.to_string(), options);
+ if let Some(glob) = CACHE.lock().unwrap().get(&key) {
+ return glob.clone();
+ }
+ let glob = parse_uncached(pattern, options);
+ let mut cache = CACHE.lock().unwrap();
+ if cache.len() >= 10000 {
+ cache.clear();
+ }
+ cache.insert(key, glob.clone());
+ glob
+}
+
+fn parse_uncached(pattern: &str, options: Options) -> Glob {
let ignore_case = options.ignore_case;
let pattern = pattern.trim();
if pattern.is_empty() {