Pre-allocate output contents during autofix application (#2340)

This commit is contained in:
Charlie Marsh 2023-01-29 22:40:27 -05:00 committed by GitHub
parent 74e3cdfd7c
commit 7a83b65fbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -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;
@ -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));

View file

@ -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)]