Skip to content

Commit 4843475

Browse files
committed
perf(formatter): do not write elements to the flat buffer if the layout is multline (#16630)
https://github.com/oxc-project/oxc/blob/c17b3d4b471d5404f67b6bb6a55921d173014318/crates/oxc_formatter/src/write/jsx/child_list.rs#L49-L50 If `force_multiline` is true, that means the flat buffer will never be used, so we should pass a flag to disable the flat buffer at the start, rather than disabling it after the first child was written in the flat buffer Disabling here is too late; a child has already been written in the flat buffer, which causes a little bit of a performance hit. https://github.com/oxc-project/oxc/blob/b3581c85f710f79de1a1dfe9af731a7b2edf1b88/crates/oxc_formatter/src/write/jsx/child_list.rs#L328-L331
1 parent 10b4f9f commit 4843475

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

crates/oxc_formatter/src/write/jsx/child_list.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ impl FormatJsxChildList {
4646
MultilineLayout::NoFill
4747
};
4848

49-
let mut flat = FlatBuilder::new(f.context().allocator());
50-
let mut multiline = MultilineBuilder::new(multiline_layout, f.context().allocator());
51-
5249
let mut force_multiline = layout.is_multiline();
50+
let mut flat = FlatBuilder::new(force_multiline, f.context().allocator());
51+
let mut multiline = MultilineBuilder::new(multiline_layout, f.context().allocator());
5352

5453
let mut children = jsx_split_children(children, f.context().comments());
5554

@@ -310,8 +309,11 @@ impl FormatJsxChildList {
310309
} else {
311310
let memoized = non_text.memoized();
312311

313-
force_multiline = memoized.inspect(f).will_break();
314-
flat.write(&format_args!(memoized, format_separator), f);
312+
child_breaks = memoized.inspect(f).will_break();
313+
314+
if !child_breaks {
315+
flat.write(&format_args!(memoized, format_separator), f);
316+
}
315317

316318
if let Some(format_separator) = format_separator {
317319
multiline.write_with_separator(&memoized, &format_separator, f);
@@ -669,8 +671,8 @@ struct FlatBuilder<'a> {
669671
}
670672

671673
impl<'a> FlatBuilder<'a> {
672-
fn new(allocator: &'a Allocator) -> Self {
673-
Self { result: ArenaVec::new_in(allocator), disabled: false }
674+
fn new(disabled: bool, allocator: &'a Allocator) -> Self {
675+
Self { result: ArenaVec::new_in(allocator), disabled }
674676
}
675677

676678
fn write(&mut self, content: &dyn Format<'a>, f: &mut Formatter<'_, 'a>) {

0 commit comments

Comments
 (0)