Files
Luke SandbergandGitHub d605cf4c27 perf(atoms): Improve atom! for inlinable strings (#10612)
This ports an optimization from Turbopacks `rcstr` implementation to
`swc`

`Atom` has an optimization to store small strings inline, this optimizes
the `atom!` macro to produce a constant `Atom` struct when possible
instead using a `once_cell::Lazy`. This shifts work from runtime to
compile time and should be a small code size optimization as well.

Many of the _literal_ `Atom`s constructed in `swc` are for things like
keywords, the vast majority of which fit within the 7 byte limit. Even
for the pesky ones that don't (looking at you `function`!), we still
optimize construction by leveraging the `once_cell`.


At the same time replace ~all calls to `"<string-literal>".into()` that
were constructing an `Atom` with a call to the `atom!` macro., which
should improve the impact of the change.
2025-06-18 12:39:47 +09:00

106 lines
3.0 KiB
Rust

extern crate swc_malloc;
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc_atoms::atom;
use swc_common::{input::StringInput, FileName};
use swc_html_ast::{DocumentMode, Element, Namespace};
use swc_html_parser::{lexer::Lexer, parser::Parser};
fn bench_document(b: &mut Bencher, src: &'static str) {
let _ = ::testing::run_test(false, |cm, _| {
let fm = cm.new_source_file(FileName::Anon.into(), src);
b.iter(|| {
let _ = black_box({
let lexer = Lexer::new(StringInput::from(&*fm));
let mut parser = Parser::new(lexer, Default::default());
parser.parse_document()
});
});
Ok(())
});
}
fn bench_document_fragment(b: &mut Bencher, src: &'static str) {
let _ = ::testing::run_test(false, |cm, _| {
let fm = cm.new_source_file(FileName::Anon.into(), src);
b.iter(|| {
let _ = black_box({
let lexer = Lexer::new(StringInput::from(&*fm));
let mut parser = Parser::new(lexer, Default::default());
parser.parse_document_fragment(
Element {
span: Default::default(),
tag_name: atom!("template"),
namespace: Namespace::HTML,
attributes: Vec::new(),
is_self_closing: false,
children: Vec::new(),
content: None,
},
DocumentMode::NoQuirks,
None,
)
});
});
Ok(())
});
}
fn run_document(c: &mut Criterion, id: &str, src: &'static str) {
c.bench_function(&format!("html/parser/{id}"), |b| {
bench_document(b, src);
});
}
fn run_document_fragment(c: &mut Criterion, id: &str, src: &'static str) {
c.bench_function(&format!("html/parser/{id}"), |b| {
bench_document_fragment(b, src);
});
}
fn bench_files(c: &mut Criterion) {
run_document(
c,
"parser_document/css_2021_spec",
include_str!("./files/css_2021_spec.html"),
);
run_document(
c,
"parser_document/github_com_17_05_2022",
include_str!("./files/github_com_17_05_2022.html"),
);
run_document(
c,
"parser_document/stackoverflow_com_17_05_2022",
include_str!("./files/stackoverflow_com_17_05_2022.html"),
);
run_document_fragment(
c,
"parser_document_fragment/css_2021_spec",
include_str!("./files/css_2021_spec.html"),
);
run_document_fragment(
c,
"parser_document_fragment/github_com_17_05_2022",
include_str!("./files/github_com_17_05_2022.html"),
);
run_document_fragment(
c,
"parser_document_fragment/stackoverflow_com_17_05_2022",
include_str!("./files/stackoverflow_com_17_05_2022.html"),
);
}
criterion_group!(benches, bench_files);
criterion_main!(benches);