Skip to content

Commit 3931507

Browse files
committed
perf: Upgrade dprint-core and various perf improvements.
Almost twice as fast mostly due to changes in dprint-core.
1 parent 570fa93 commit 3931507

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ panic = "abort"
2424
wasm = ["serde_json", "dprint-core/wasm"]
2525

2626
[dependencies]
27-
dprint-core = { version = "0.33.1", features = ["formatting"] }
27+
dprint-core = { version = "0.34.0", features = ["formatting"] }
28+
fnv = "1.0.3"
2829
swc_common = "0.10.7"
2930
swc_ecmascript = { version = "0.16.0", features = ["parser"] }
30-
swc_ast_view = { version = "0.3.0", package = "dprint-swc-ecma-ast-view" }
31+
swc_ast_view = { version = "0.3.1", package = "dprint-swc-ecma-ast-view" }
3132
serde = { version = "1.0.118", features = ["derive"] }
3233
serde_json = { version = "1.0", optional = true }
3334

src/parsing/parser.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::HashMap;
21
use std::rc::Rc;
32
use dprint_core::formatting::*;
43
use dprint_core::formatting::{parser_helpers::*,condition_resolvers, conditions::*};
@@ -4848,7 +4847,7 @@ fn parse_statements<'a>(inner_span: Span, stmts: Vec<Node<'a>>, context: &mut Co
48484847

48494848
let nodes_len = stmt_group.nodes.len();
48504849
let mut parsed_nodes = Vec::with_capacity(nodes_len);
4851-
let mut parsed_line_separators = HashMap::with_capacity(if nodes_len == 0 { 0 } else { nodes_len - 1 });
4850+
let mut parsed_line_separators = utils::VecMap::with_capacity(nodes_len);
48524851
let sorter = get_node_sorter(stmt_group.kind, context);
48534852
let sorted_indexes = match sorter {
48544853
Some(sorter) => Some(get_sorted_indexes(stmt_group.nodes.iter().map(|n| Some(n)), sorter, context)),
@@ -4897,7 +4896,7 @@ fn parse_statements<'a>(inner_span: Span, stmts: Vec<Node<'a>>, context: &mut Co
48974896

48984897
// Now combine everything
48994898
for (i, parsed_node) in parsed_nodes.into_iter().enumerate() {
4900-
if let Some(parsed_separator) = parsed_line_separators.remove(&i) {
4899+
if let Some(parsed_separator) = parsed_line_separators.remove(i) {
49014900
items.extend(parsed_separator);
49024901
}
49034902
items.extend(parsed_node);
@@ -5360,7 +5359,7 @@ fn parse_separated_values_with_result<'a>(
53605359

53615360
for (i, value) in nodes.into_iter().enumerate() {
53625361
let node_index = match &sorted_indexes {
5363-
Some(old_to_new_index) => *old_to_new_index.get(&i).unwrap(),
5362+
Some(old_to_new_index) => *old_to_new_index.get(i).unwrap(),
53645363
None => i,
53655364
};
53665365
let (allow_inline_multi_line, allow_inline_single_line) = if let Some(value) = &value {
@@ -5412,10 +5411,10 @@ fn get_sorted_indexes<'a: 'b, 'b>(
54125411
nodes: impl Iterator<Item=Option<&'b Node<'a>>>,
54135412
sorter: Box<dyn Fn((usize, Option<&Node<'a>>), (usize, Option<&Node<'a>>), &Module<'a>) -> std::cmp::Ordering>,
54145413
context: &mut Context<'a>,
5415-
) -> HashMap<usize, usize> {
5414+
) -> utils::VecMap<usize> {
54165415
let mut nodes_with_indexes = nodes.enumerate().collect::<Vec<_>>();
54175416
nodes_with_indexes.sort_unstable_by(|a, b| sorter((a.0, a.1), (b.0, b.1), context.module));
5418-
let mut old_to_new_index = HashMap::new();
5417+
let mut old_to_new_index = utils::VecMap::with_capacity(nodes_with_indexes.len());
54195418

54205419
for (new_index, old_index) in nodes_with_indexes.into_iter().map(|(index, _)| index).enumerate() {
54215420
old_to_new_index.insert(old_index, new_index);
@@ -5424,14 +5423,14 @@ fn get_sorted_indexes<'a: 'b, 'b>(
54245423
old_to_new_index
54255424
}
54265425

5427-
fn sort_by_sorted_indexes<T>(items: Vec<T>, sorted_indexes: HashMap<usize, usize>) -> Vec<T> {
5426+
fn sort_by_sorted_indexes<T>(items: Vec<T>, sorted_indexes: utils::VecMap<usize>) -> Vec<T> {
54285427
let mut sorted_items = Vec::with_capacity(items.len());
54295428
for _ in 0..items.len() {
54305429
sorted_items.push(None);
54315430
}
54325431

54335432
for (i, parsed_node) in items.into_iter().enumerate() {
5434-
sorted_items[*sorted_indexes.get(&i).unwrap_or(&i)] = Some(parsed_node);
5433+
sorted_items[*sorted_indexes.get(i).unwrap_or(&i)] = Some(parsed_node);
54355434
}
54365435

54375436
sorted_items.into_iter().map(|x| x.unwrap()).collect()

src/parsing/parser_types.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::collections::{HashSet, HashMap};
21
use dprint_core::formatting::{Info, ConditionReference};
32
use swc_common::SourceFile;
43
use swc_ast_view::*;
4+
use fnv::{FnvHashMap, FnvHashSet};
55

66
use super::*;
77
use crate::configuration::*;
@@ -14,10 +14,10 @@ pub struct Context<'a> {
1414
pub token_finder: TokenFinder<'a>,
1515
pub current_node: Node<'a>,
1616
pub parent_stack: Stack<Node<'a>>,
17-
handled_comments: HashSet<BytePos>,
17+
handled_comments: FnvHashSet<BytePos>,
1818
pub info: &'a SourceFile,
19-
stored_infos: HashMap<(BytePos, BytePos), Info>,
20-
stored_info_ranges: HashMap<(BytePos, BytePos), (Info, Info)>,
19+
stored_infos: FnvHashMap<(BytePos, BytePos), Info>,
20+
stored_info_ranges: FnvHashMap<(BytePos, BytePos), (Info, Info)>,
2121
pub end_statement_or_member_infos: Stack<Info>,
2222
before_comments_start_info_stack: Stack<(Span, Info)>,
2323
if_stmt_last_brace_condition_ref: Option<ConditionReference>,
@@ -41,10 +41,10 @@ impl<'a> Context<'a> {
4141
token_finder: TokenFinder::new(module),
4242
current_node,
4343
parent_stack: Stack::new(),
44-
handled_comments: HashSet::new(),
44+
handled_comments: FnvHashSet::default(),
4545
info,
46-
stored_infos: HashMap::new(),
47-
stored_info_ranges: HashMap::new(),
46+
stored_infos: FnvHashMap::default(),
47+
stored_info_ranges: FnvHashMap::default(),
4848
end_statement_or_member_infos: Stack::new(),
4949
before_comments_start_info_stack: Stack::new(),
5050
if_stmt_last_brace_condition_ref: None,

src/utils/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ mod char_iterator;
22
mod is_prefix_semi_colon_insertion_char;
33
mod stack;
44
mod string_utils;
5+
mod vec_map;
56

67
pub use char_iterator::*;
78
pub use is_prefix_semi_colon_insertion_char::*;
89
pub use stack::*;
910
pub use string_utils::*;
11+
pub use vec_map::*;

src/utils/vec_map.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// A faster map for when the mapping is being done over keys going from 0..n
2+
pub struct VecMap<T> {
3+
inner: Vec<Option<T>>,
4+
}
5+
6+
impl<T> VecMap<T> {
7+
pub fn with_capacity(capacity: usize) -> VecMap<T> {
8+
let mut inner = Vec::with_capacity(capacity);
9+
for _ in 0..capacity {
10+
inner.push(None);
11+
}
12+
13+
VecMap {
14+
inner
15+
}
16+
}
17+
18+
pub fn insert(&mut self, index: usize, value: T) {
19+
self.inner[index] = Some(value);
20+
}
21+
22+
pub fn get(&self, index: usize) -> Option<&T> {
23+
self.inner.get(index).map(|value| value.as_ref()).flatten()
24+
}
25+
26+
pub fn remove(&mut self, index: usize) -> Option<T> {
27+
self.inner.get_mut(index).map(|value| value.take()).flatten()
28+
}
29+
}

0 commit comments

Comments
 (0)