deps: drop thread_local, lazy_static and once_cell

This is largely made possible by the addition of std::sync::OnceLock to
the standard library, and the memory pool available in regex-automata.
This commit is contained in:
Andrew Gallant
2023-09-28 16:01:59 -04:00
parent f16ea0812d
commit 6d17b3ed68
7 changed files with 43 additions and 75 deletions

View File

@@ -21,14 +21,16 @@ bench = false
[dependencies]
crossbeam-deque = "0.8.3"
globset = { version = "0.4.10", path = "../globset" }
lazy_static = "1.1"
log = "0.4.20"
memchr = "2.6.3"
regex = { version = "1.9.5", default-features = false, features = ["perf", "std", "unicode-gencat"] }
same-file = "1.0.6"
thread_local = "1"
walkdir = "2.4.0"
[dependencies.regex-automata]
version = "0.3.8"
default-features = false
features = ["std", "perf", "syntax", "meta", "nfa", "hybrid", "dfa-onepass"]
[target.'cfg(windows)'.dependencies.winapi-util]
version = "0.1.2"

View File

@@ -8,7 +8,6 @@ the `git` command line tool.
*/
use std::{
cell::RefCell,
fs::File,
io::{BufRead, BufReader, Read},
path::{Path, PathBuf},
@@ -17,8 +16,7 @@ use std::{
use {
globset::{Candidate, GlobBuilder, GlobSet, GlobSetBuilder},
regex::bytes::Regex,
thread_local::ThreadLocal,
regex_automata::util::pool::Pool,
};
use crate::{
@@ -86,7 +84,7 @@ pub struct Gitignore {
globs: Vec<Glob>,
num_ignores: u64,
num_whitelists: u64,
matches: Option<Arc<ThreadLocal<RefCell<Vec<usize>>>>>,
matches: Option<Arc<Pool<Vec<usize>>>>,
}
impl Gitignore {
@@ -253,8 +251,7 @@ impl Gitignore {
return Match::None;
}
let path = path.as_ref();
let _matches = self.matches.as_ref().unwrap().get_or_default();
let mut matches = _matches.borrow_mut();
let mut matches = self.matches.as_ref().unwrap().get();
let candidate = Candidate::new(path);
self.set.matches_candidate_into(&candidate, &mut *matches);
for &i in matches.iter().rev() {
@@ -346,7 +343,7 @@ impl GitignoreBuilder {
globs: self.globs.clone(),
num_ignores: nignore as u64,
num_whitelists: nwhite as u64,
matches: Some(Arc::new(ThreadLocal::default())),
matches: Some(Arc::new(Pool::new(|| vec![]))),
})
}
@@ -596,23 +593,28 @@ fn excludes_file_default() -> Option<PathBuf> {
/// Extract git's `core.excludesfile` config setting from the raw file contents
/// given.
fn parse_excludes_file(data: &[u8]) -> Option<PathBuf> {
use std::sync::OnceLock;
use regex_automata::{meta::Regex, util::syntax};
// N.B. This is the lazy approach, and isn't technically correct, but
// probably works in more circumstances. I guess we would ideally have
// a full INI parser. Yuck.
lazy_static::lazy_static! {
static ref RE: Regex = Regex::new(
r"(?xim-u)
^[[:space:]]*excludesfile[[:space:]]*
=
[[:space:]]*(.+)[[:space:]]*$
"
).unwrap();
};
let caps = match RE.captures(data) {
None => return None,
Some(caps) => caps,
};
std::str::from_utf8(&caps[1]).ok().map(|s| PathBuf::from(expand_tilde(s)))
static RE: OnceLock<Regex> = OnceLock::new();
let re = RE.get_or_init(|| {
Regex::builder()
.configure(Regex::config().utf8_empty(false))
.syntax(syntax::Config::new().utf8(false))
.build(r"(?im-u)^\s*excludesfile\s*=\s*(\S+)\s*$")
.unwrap()
});
// We don't care about amortizing allocs here I think. This should only
// be called ~once per traversal or so? (Although it's not guaranteed...)
let mut caps = re.create_captures();
re.captures(data, &mut caps);
let span = caps.get_group(1)?;
let candidate = &data[span];
std::str::from_utf8(candidate).ok().map(|s| PathBuf::from(expand_tilde(s)))
}
/// Expands ~ in file paths to the value of $HOME.

View File

@@ -84,12 +84,11 @@ assert!(matcher.matched("y.cpp", false).is_whitelist());
```
*/
use std::{cell::RefCell, collections::HashMap, path::Path, sync::Arc};
use std::{collections::HashMap, path::Path, sync::Arc};
use {
globset::{GlobBuilder, GlobSet, GlobSetBuilder},
regex::Regex,
thread_local::ThreadLocal,
regex_automata::util::pool::Pool,
};
use crate::{default_types::DEFAULT_TYPES, pathutil::file_name, Error, Match};
@@ -178,7 +177,7 @@ pub struct Types {
/// The set of all glob selections, used for actual matching.
set: GlobSet,
/// Temporary storage for globs that match.
matches: Arc<ThreadLocal<RefCell<Vec<usize>>>>,
matches: Arc<Pool<Vec<usize>>>,
}
/// Indicates the type of a selection for a particular file type.
@@ -232,7 +231,7 @@ impl Types {
has_selected: false,
glob_to_selection: vec![],
set: GlobSetBuilder::new().build().unwrap(),
matches: Arc::new(ThreadLocal::default()),
matches: Arc::new(Pool::new(|| vec![])),
}
}
@@ -280,7 +279,7 @@ impl Types {
return Match::None;
}
};
let mut matches = self.matches.get_or_default().borrow_mut();
let mut matches = self.matches.get();
self.set.matches_into(name, &mut *matches);
// The highest precedent match is the last one.
if let Some(&i) = matches.last() {
@@ -358,7 +357,7 @@ impl TypesBuilder {
has_selected,
glob_to_selection,
set,
matches: Arc::new(ThreadLocal::default()),
matches: Arc::new(Pool::new(|| vec![])),
})
}
@@ -416,10 +415,7 @@ impl TypesBuilder {
/// If `name` is `all` or otherwise contains any character that is not a
/// Unicode letter or number, then an error is returned.
pub fn add(&mut self, name: &str, glob: &str) -> Result<(), Error> {
lazy_static::lazy_static! {
static ref RE: Regex = Regex::new(r"^[\pL\pN]+$").unwrap();
};
if name == "all" || !RE.is_match(name) {
if name == "all" || !name.chars().all(|c| c.is_alphanumeric()) {
return Err(Error::InvalidDefinition);
}
let (key, glob) = (name.to_string(), glob.to_string());