Skip to content

Commit 7b7b50e

Browse files
authored
perf(es/parser): Add initial capacitity for some vectors (#10361)
1 parent 0f6a8c3 commit 7b7b50e

File tree

7 files changed

+9
-9
lines changed

7 files changed

+9
-9
lines changed

crates/swc_ecma_parser/src/lexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ impl Lexer<'_> {
910910
fn read_unicode_escape(&mut self) -> LexResult<Vec<Char>> {
911911
debug_assert_eq!(self.cur(), Some('u'));
912912

913-
let mut chars = Vec::new();
913+
let mut chars = Vec::with_capacity(4);
914914
let mut is_curly = false;
915915

916916
self.bump(); // 'u'

crates/swc_ecma_parser/src/parser/class_and_fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<I: Tokens> Parser<I> {
124124
let implements = if p.input.syntax().typescript() && eat!(p, "implements") {
125125
p.parse_ts_heritage_clause()?
126126
} else {
127-
Vec::new()
127+
Vec::with_capacity(4)
128128
};
129129

130130
{
@@ -333,7 +333,7 @@ impl<I: Tokens> Parser<I> {
333333
}
334334

335335
fn parse_class_body(&mut self) -> PResult<Vec<ClassMember>> {
336-
let mut elems = Vec::new();
336+
let mut elems = Vec::with_capacity(32);
337337
let mut has_constructor_with_body = false;
338338
while !eof!(self) && !is!(self, '}') {
339339
if eat_exact!(self, ';') {

crates/swc_ecma_parser/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl<I: Tokens> Parser<I> {
499499
let start = cur_pos!(self);
500500

501501
assert_and_bump!(self, '[');
502-
let mut elems = Vec::new();
502+
let mut elems = Vec::with_capacity(8);
503503

504504
while !eof!(self) && !is!(self, ']') {
505505
if is!(self, ',') {

crates/swc_ecma_parser/src/parser/object.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<I: Tokens> Parser<I> {
2222
let mut trailing_comma = None;
2323
assert_and_bump!(p, '{');
2424

25-
let mut props = Vec::new();
25+
let mut props = Vec::with_capacity(8);
2626

2727
while !eat!(p, '}') {
2828
props.push(p.parse_object_prop()?);

crates/swc_ecma_parser/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ impl<'a, I: Tokens> Parser<I> {
904904
}
905905
}
906906

907-
let mut decls = Vec::new();
907+
let mut decls = Vec::with_capacity(4);
908908
let mut first = true;
909909
while first || eat!(self, ',') {
910910
if first {

crates/swc_ecma_parser/src/parser/stmt/module_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<I: Tokens> Parser<I> {
7979

8080
let mut type_only = false;
8181
let mut phase = ImportPhase::Evaluation;
82-
let mut specifiers = Vec::new();
82+
let mut specifiers = Vec::with_capacity(4);
8383

8484
'import_maybe_ident: {
8585
if is!(self, BindingIdent) {

crates/swc_ecma_parser/src/parser/typescript.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<I: Tokens> Parser<I> {
7979
{
8080
debug_assert!(self.input.syntax().typescript());
8181

82-
let mut buf = Vec::new();
82+
let mut buf = Vec::with_capacity(8);
8383
while !self.is_ts_list_terminator(kind)? {
8484
// Skipping "parseListElement" from the TS source since that's just for error
8585
// handling.
@@ -1980,7 +1980,7 @@ impl<I: Tokens> Parser<I> {
19801980
debug_assert!(self.input.syntax().typescript());
19811981

19821982
let params = self.parse_formal_params()?;
1983-
let mut list = Vec::new();
1983+
let mut list = Vec::with_capacity(4);
19841984

19851985
for param in params {
19861986
let item = match param.pat {

0 commit comments

Comments
 (0)