Skip to content

Commit f29f938

Browse files
authored
Merge pull request #2868 from cyqsimon/builtin-offload-v2
Faster startup by offloading glob matcher building to a worker thread
2 parents c290bff + 26ac179 commit f29f938

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- Improve performance when color output disabled, see #2397 and #2857 (@eth-p)
3333
- Relax syntax mapping rule restrictions to allow brace expansion #2865 (@cyqsimon)
3434
- Apply clippy fixes #2864 (@cyqsimon)
35+
- Faster startup by offloading glob matcher building to a worker thread #2868 (@cyqsimon)
3536

3637
## Syntaxes
3738

src/bin/bat/app.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ impl App {
122122
};
123123

124124
let mut syntax_mapping = SyntaxMapping::new();
125+
// start building glob matchers for builtin mappings immediately
126+
// this is an appropriate approach because it's statistically likely that
127+
// all the custom mappings need to be checked
128+
syntax_mapping.start_offload_build_all();
125129

126130
if let Some(values) = self.matches.get_many::<String>("ignored-suffix") {
127131
for suffix in values {

src/syntax_mapping.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
use std::path::Path;
1+
use std::{
2+
path::Path,
3+
sync::{
4+
atomic::{AtomicBool, Ordering},
5+
Arc,
6+
},
7+
thread,
8+
};
29

310
use globset::{Candidate, GlobBuilder, GlobMatcher};
11+
use once_cell::sync::Lazy;
412

513
use crate::error::Result;
614
use builtin::BUILTIN_MAPPINGS;
@@ -44,14 +52,50 @@ pub struct SyntaxMapping<'a> {
4452
///
4553
/// Rules in front have precedence.
4654
custom_mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
55+
4756
pub(crate) ignored_suffixes: IgnoredSuffixes<'a>,
57+
58+
/// A flag to halt glob matcher building, which is offloaded to another thread.
59+
///
60+
/// We have this so that we can signal the thread to halt early when appropriate.
61+
halt_glob_build: Arc<AtomicBool>,
62+
}
63+
64+
impl<'a> Drop for SyntaxMapping<'a> {
65+
fn drop(&mut self) {
66+
// signal the offload thread to halt early
67+
self.halt_glob_build.store(true, Ordering::Relaxed);
68+
}
4869
}
4970

5071
impl<'a> SyntaxMapping<'a> {
5172
pub fn new() -> SyntaxMapping<'a> {
5273
Default::default()
5374
}
5475

76+
/// Start a thread to build the glob matchers for all builtin mappings.
77+
///
78+
/// The use of this function while not necessary, is useful to speed up startup
79+
/// times by starting this work early in parallel.
80+
///
81+
/// The thread halts if/when `halt_glob_build` is set to true.
82+
pub fn start_offload_build_all(&self) {
83+
let halt = Arc::clone(&self.halt_glob_build);
84+
thread::spawn(move || {
85+
for (matcher, _) in BUILTIN_MAPPINGS.iter() {
86+
if halt.load(Ordering::Relaxed) {
87+
break;
88+
}
89+
Lazy::force(matcher);
90+
}
91+
});
92+
// Note that this thread is not joined upon completion because there's
93+
// no shared resources that need synchronization to be safely dropped.
94+
// If we later add code into this thread that requires interesting
95+
// resources (e.g. IO), it would be a good idea to store the handle
96+
// and join it on drop.
97+
}
98+
5599
pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
56100
let matcher = make_glob_matcher(from)?;
57101
self.custom_mappings.push((matcher, to));

0 commit comments

Comments
 (0)