Skip to content

Commit

Permalink
perf: skip word parsing if no expansion required (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Jan 28, 2025
1 parent c34bf7a commit 42d75a7
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions brush-core/src/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@ impl<'a> WordExpander<'a> {
async fn basic_expand(&mut self, word: &str) -> Result<Expansion, error::Error> {
tracing::debug!(target: trace_categories::EXPANSION, "Basic expanding: '{word}'");

// Quick short circuit to avoid more expensive parsing. The characters below are
// understood to be the *only* ones indicative of *possible* expansion. There's
// still a possibility no expansion needs to be done, but that's okay; we'll still
// yield a correct result.
if !word.contains(['$', '`', '\\', '\'', '\"', '~', '{']) {
return Ok(Expansion::from(ExpansionPiece::Splittable(word.to_owned())));
}

// Apply brace expansion first, before anything else.
let brace_expanded: String = self.brace_expand_if_needed(word)?.into_iter().join(" ");
if tracing::enabled!(target: trace_categories::EXPANSION, tracing::Level::DEBUG)
Expand Down

0 comments on commit 42d75a7

Please sign in to comment.