comparison rust/hg-core/src/filepatterns.rs @ 50692:1c31b343e514

match: add `filepath:` pattern to match an exact filepath relative to the root It's useful in certain automated workflows to make sure we recurse in directories whose name conflicts with files in other revisions. In addition it makes it possible to avoid building a potentially costly regex, improving performance when the set of files to match explicitly is large. The benchmark below are run in the following configuration : # data-env-vars.name = mozilla-central-2018-08-01-zstd-sparse-revlog # benchmark.name = files # benchmark.variants.rev = tip # benchmark.variants.files = all-list-filepath-sorted # bin-env-vars.hg.flavor = no-rust It also includes timings using the re2 engine (through the `google-re2` module) to show how much can be saved by just using a better regexp engine. Pattern time (seconds) time using re2 ----------------------------------------------------------- just "." 0.4 0.4 list of "filepath:?" 1.3 1.3 list of "path:?" 25.7 3.9 list of patterns 29.7 10.4 As you can see, Without re2, using "filepath:" instead of "path:" is a huge win. With re2, it is still about three times faster to not have to build the regex.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 12 Jun 2023 16:51:08 +0200
parents e98fd81bb151
children 796b5d6693a4
comparison
equal deleted inserted replaced
50683:a41eeb877d07 50692:1c31b343e514
48 /// Glob that matches at any suffix of the path (still anchored at 48 /// Glob that matches at any suffix of the path (still anchored at
49 /// slashes) 49 /// slashes)
50 Glob, 50 Glob,
51 /// a path relative to repository root, which is matched recursively 51 /// a path relative to repository root, which is matched recursively
52 Path, 52 Path,
53 /// a single exact path relative to repository root
54 FilePath,
53 /// A path relative to cwd 55 /// A path relative to cwd
54 RelPath, 56 RelPath,
55 /// an unrooted glob (*.rs matches Rust files in all dirs) 57 /// an unrooted glob (*.rs matches Rust files in all dirs)
56 RelGlob, 58 RelGlob,
57 /// A regexp that needn't match the start of a name 59 /// A regexp that needn't match the start of a name
155 kind: &[u8], 157 kind: &[u8],
156 ) -> Result<PatternSyntax, PatternError> { 158 ) -> Result<PatternSyntax, PatternError> {
157 match kind { 159 match kind {
158 b"re:" => Ok(PatternSyntax::Regexp), 160 b"re:" => Ok(PatternSyntax::Regexp),
159 b"path:" => Ok(PatternSyntax::Path), 161 b"path:" => Ok(PatternSyntax::Path),
162 b"filepath:" => Ok(PatternSyntax::FilePath),
160 b"relpath:" => Ok(PatternSyntax::RelPath), 163 b"relpath:" => Ok(PatternSyntax::RelPath),
161 b"rootfilesin:" => Ok(PatternSyntax::RootFiles), 164 b"rootfilesin:" => Ok(PatternSyntax::RootFiles),
162 b"relglob:" => Ok(PatternSyntax::RelGlob), 165 b"relglob:" => Ok(PatternSyntax::RelGlob),
163 b"relre:" => Ok(PatternSyntax::RelRegexp), 166 b"relre:" => Ok(PatternSyntax::RelRegexp),
164 b"glob:" => Ok(PatternSyntax::Glob), 167 b"glob:" => Ok(PatternSyntax::Glob),
250 PatternSyntax::Glob | PatternSyntax::RootGlob => { 253 PatternSyntax::Glob | PatternSyntax::RootGlob => {
251 [glob_to_re(pattern).as_slice(), GLOB_SUFFIX].concat() 254 [glob_to_re(pattern).as_slice(), GLOB_SUFFIX].concat()
252 } 255 }
253 PatternSyntax::Include 256 PatternSyntax::Include
254 | PatternSyntax::SubInclude 257 | PatternSyntax::SubInclude
255 | PatternSyntax::ExpandedSubInclude(_) => unreachable!(), 258 | PatternSyntax::ExpandedSubInclude(_)
259 | PatternSyntax::FilePath => unreachable!(),
256 } 260 }
257 } 261 }
258 262
259 const GLOB_SPECIAL_CHARACTERS: [u8; 7] = 263 const GLOB_SPECIAL_CHARACTERS: [u8; 7] =
260 [b'*', b'?', b'[', b']', b'{', b'}', b'\\']; 264 [b'*', b'?', b'[', b']', b'{', b'}', b'\\'];
317 PatternSyntax::Include | PatternSyntax::SubInclude => { 321 PatternSyntax::Include | PatternSyntax::SubInclude => {
318 return Err(PatternError::NonRegexPattern(entry.clone())) 322 return Err(PatternError::NonRegexPattern(entry.clone()))
319 } 323 }
320 _ => pattern.to_owned(), 324 _ => pattern.to_owned(),
321 }; 325 };
322 if *syntax == PatternSyntax::RootGlob 326 let is_simple_rootglob = *syntax == PatternSyntax::RootGlob
323 && !pattern.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b)) 327 && !pattern.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b));
324 { 328 if is_simple_rootglob || syntax == &PatternSyntax::FilePath {
325 Ok(None) 329 Ok(None)
326 } else { 330 } else {
327 let mut entry = entry.clone(); 331 let mut entry = entry.clone();
328 entry.pattern = pattern; 332 entry.pattern = pattern;
329 Ok(Some(_build_single_regex(&entry))) 333 Ok(Some(_build_single_regex(&entry)))