Skip to content

Commit d81dfba

Browse files
committed
perf(linter): allocate strings with some initial capacity
1 parent 7fb7e5d commit d81dfba

File tree

8 files changed

+36
-24
lines changed

8 files changed

+36
-24
lines changed

crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, ops::Deref};
1+
use std::ops::Deref;
22

33
use itertools::Itertools;
44
use oxc_ast::{
@@ -13,10 +13,9 @@ use oxc_span::{CompactStr, GetSpan, Span};
1313

1414
use crate::{LintContext, rule::Rule};
1515

16-
fn class_methods_use_this_diagnostic(span: Span, name: Option<Cow<'_, str>>) -> OxcDiagnostic {
17-
let method_name_str = name.map_or(String::new(), |name| format!(" `{name}`"));
18-
OxcDiagnostic::warn(format!("Expected method{method_name_str} to have this."))
19-
.with_help(format!("Consider converting method{method_name_str} to a static method."))
16+
fn class_methods_use_this_diagnostic(span: Span, name: &str) -> OxcDiagnostic {
17+
OxcDiagnostic::warn(format!("Expected method `{name}` to have this."))
18+
.with_help(format!("Consider converting method `{name}` to a static method."))
2019
.with_label(span)
2120
}
2221

@@ -219,8 +218,10 @@ impl Rule for ClassMethodsUseThis {
219218
}
220219
let mut finder = ThisFinder::new();
221220
finder.visit_function_body(function_body);
222-
if !finder.has_this {
223-
ctx.diagnostic(class_methods_use_this_diagnostic(name.span(), name.name()));
221+
if !finder.has_this
222+
&& let Some(name_str) = name.name()
223+
{
224+
ctx.diagnostic(class_methods_use_this_diagnostic(name.span(), &name_str));
224225
}
225226
}
226227
}

crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub fn to_precision(mut num: f64, precision: usize) -> String {
370370
let precision_i32 = precision as i32;
371371

372372
// Handle sign
373-
let mut prefix = String::new();
373+
let mut prefix = String::with_capacity(8);
374374
if num < 0.0 {
375375
prefix.push('-');
376376
num = -num;

crates/oxc_linter/src/rules/eslint/sort_imports.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ impl SortImports {
322322
// add a empty string for zip with specifiers
323323
paddings.push("");
324324

325+
let specifiers_len = specifiers.len();
325326
let specifiers = specifiers.iter().sorted_by(|a, b| {
326327
let a = a.local.name.as_str();
327328
let b = b.local.name.as_str();
@@ -334,7 +335,7 @@ impl SortImports {
334335
});
335336

336337
let sorted_text = specifiers.zip(paddings).fold(
337-
String::new(),
338+
String::with_capacity(specifiers_len * 8),
338339
|mut acc, (specifier, padding)| {
339340
let _ = acc.write_str(ctx.source_range(specifier.span));
340341
let _ = acc.write_str(padding);

crates/oxc_linter/src/rules/eslint/sort_keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl Rule for SortKeys {
291291
// trailing comma) to the end, we must remove their trailing comma so
292292
// the resulting object doesn't end up with an extra comma before `}`.
293293
// Also normalize separators between properties to `, ` for clarity.
294-
let mut sorted_text = String::new();
294+
let mut sorted_text = String::with_capacity(8 * indices.len()); // initialize with avg 8 chars per prop
295295
let trim_end_commas = |s: &str| -> String {
296296
let s = s.trim_end();
297297
let mut trimmed = s.to_string();

crates/oxc_linter/src/rules/import/consistent_type_specifier_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Rule for ConsistentTypeSpecifierStyle {
115115
ctx.diagnostic_with_fix(
116116
consistent_type_specifier_style_diagnostic(item.span(), &self.mode),
117117
|fixer| {
118-
let mut import_source = String::new();
118+
let mut import_source = String::with_capacity(128);
119119

120120
if !value_specifiers.is_empty() {
121121
let value_import_declaration =

crates/oxc_linter/src/rules/react/jsx_fragments.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ impl Rule for JsxFragments {
130130
closing_element.span().end,
131131
jsx_elem.span().end,
132132
));
133-
let mut replacement = String::new();
133+
let mut replacement = String::with_capacity(
134+
before_opening_tag.len()
135+
+ between_opening_tag_and_closing_tag.len()
136+
+ after_closing_tag.len()
137+
+ "<></>".len(),
138+
);
134139
replacement.push_str(before_opening_tag);
135140
replacement.push_str("<>");
136141
replacement.push_str(between_opening_tag_and_closing_tag);
@@ -156,7 +161,12 @@ impl Rule for JsxFragments {
156161
jsx_frag.closing_fragment.span().end,
157162
jsx_frag.span().end,
158163
));
159-
let mut replacement = String::new();
164+
let mut replacement = String::with_capacity(
165+
before_opening_tag.len()
166+
+ between_opening_tag_and_closing_tag.len()
167+
+ after_closing_tag.len()
168+
+ "<React.Fragment></React.Fragment>".len(),
169+
);
160170
replacement.push_str(before_opening_tag);
161171
replacement.push_str("<React.Fragment>");
162172
replacement.push_str(between_opening_tag_and_closing_tag);

crates/oxc_linter/src/rules/typescript/consistent_type_definitions.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,18 @@ impl Rule for ConsistentTypeDefinitions {
174174
let body_span = &decl.body.span;
175175
let body = &ctx.source_text()[body_span.start as usize..body_span.end as usize];
176176

177-
let mut extends = String::new();
178-
for exp in &decl.extends {
179-
write!(extends, " & {}", exp.span.source_text(ctx.source_text())).unwrap();
180-
}
181-
182177
ctx.diagnostic_with_fix(
183178
consistent_type_definitions_diagnostic(
184179
"type",
185180
"interface",
186181
Span::sized(decl.span.start, 9),
187182
),
188183
|fixer| {
184+
let mut extends = String::with_capacity(8 * decl.extends.len());
185+
for exp in &decl.extends {
186+
write!(extends, " & {}", exp.span.source_text(ctx.source_text()))
187+
.unwrap();
188+
}
189189
fixer.replace(
190190
exp.span,
191191
format!("type {name} = {body}{extends}\nexport default {name}"),
@@ -220,18 +220,18 @@ impl Rule for ConsistentTypeDefinitions {
220220
let body_span = &decl.body.span;
221221
let body = &ctx.source_text()[body_span.start as usize..body_span.end as usize];
222222

223-
let mut extends = String::new();
224-
for exp in &decl.extends {
225-
write!(extends, " & {}", exp.span.source_text(ctx.source_text())).unwrap();
226-
}
227-
228223
ctx.diagnostic_with_fix(
229224
consistent_type_definitions_diagnostic(
230225
"type",
231226
"interface",
232227
Span::sized(start, 9),
233228
),
234229
|fixer| {
230+
let mut extends = String::with_capacity(8 * decl.extends.len());
231+
for exp in &decl.extends {
232+
write!(extends, " & {}", exp.span.source_text(ctx.source_text()))
233+
.unwrap();
234+
}
235235
fixer.replace(
236236
Span::new(start, decl.span.end),
237237
format!("type {name} = {body}{extends}"),

crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fn is_pow_2_expression(expression: &Expression, ctx: &LintContext<'_>) -> bool {
262262
/// removes any newlines from the string
263263
/// removes any duplicate spaces
264264
fn clean_string(input: &str) -> String {
265-
let mut result = String::new();
265+
let mut result = String::with_capacity(input.len());
266266
let mut prev_char = ' ';
267267

268268
for c in input.chars() {

0 commit comments

Comments
 (0)