Skip to content

Commit

Permalink
Pre-allocate output contents during autofix application (#2340)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jan 30, 2023
1 parent 74e3cdf commit 7a83b65
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn apply_fixes<'a>(
fixes: impl Iterator<Item = &'a Fix>,
locator: &'a Locator<'a>,
) -> (String, usize) {
let mut output = String::new();
let mut output = String::with_capacity(locator.len());
let mut last_pos: Location = Location::new(1, 0);
let mut applied: BTreeSet<&Fix> = BTreeSet::default();
let mut num_fixed: usize = 0;
Expand Down Expand Up @@ -68,7 +68,7 @@ fn apply_fixes<'a>(

/// Apply a single fix.
pub(crate) fn apply_fix(fix: &Fix, locator: &Locator) -> String {
let mut output = String::new();
let mut output = String::with_capacity(locator.len());

// Add all contents from `last_pos` to `fix.location`.
let slice = locator.slice_source_code_range(&Range::new(Location::new(1, 0), fix.location));
Expand Down
8 changes: 8 additions & 0 deletions src/source_code/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ impl<'a> Locator<'a> {
&self.contents[inner_end..outer_end],
)
}

pub fn len(&self) -> usize {
self.contents.len()
}

pub fn is_empty(&self) -> bool {
self.contents.is_empty()
}
}

#[cfg(test)]
Expand Down

0 comments on commit 7a83b65

Please sign in to comment.