Skip to content

Commit

Permalink
perf(es/codegen): Use scoped allocator (#9248)
Browse files Browse the repository at this point in the history
**Description:**

Main:
<img width="711" alt="스크린샷 2024-07-15 오후 8 05 50"
src="https://github.com/user-attachments/assets/508a29e8-744f-4017-a819-b7576e61d287">

new:

<img width="711" alt="스크린샷 2024-07-15 오후 8 05 19"
src="https://github.com/user-attachments/assets/bcf3107d-e843-4fab-8b8a-fc6376705273">
  • Loading branch information
kdy1 committed Jul 16, 2024
1 parent 5cc6449 commit 970f323
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 14 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/swc_compiler_base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sourcemap = { workspace = true }
swc_allocator = { version = "0.1.5", path = "../swc_allocator", default-features = false }

swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
swc_common = { version = "0.35.0", path = "../swc_common", features = [
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_compiler_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ where
{
let _timer = timer!("Compiler::print");

let mut src_map_buf = vec![];
let mut src_map_buf = swc_allocator::vec::Vec::new();

let src = {
let mut buf = vec![];
let mut buf = Vec::new();
{
let mut w = swc_ecma_codegen::text_writer::JsWriter::new(
cm.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_ast/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Stmt {
}
}

// Memory layout depedns on the version of rustc.
// Memory layout depends on the version of rustc.
// #[cfg(target_pointer_width = "64")]
// assert_eq_size!(Stmt, [u8; 56]);

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde = { workspace = true }
sourcemap = { workspace = true }
tracing = { workspace = true }

swc_allocator = { version = "0.1.5", path = "../swc_allocator" }
swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
swc_common = { version = "0.35.0", path = "../swc_common" }
swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" }
Expand Down
8 changes: 6 additions & 2 deletions crates/swc_ecma_codegen/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate swc_malloc;

use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc_allocator::{vec::Vec, Allocator};
use swc_common::FileName;
use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{Parser, StringInput, Syntax};
Expand Down Expand Up @@ -84,7 +85,6 @@ fn bench_emitter(b: &mut Bencher, s: &str) {
let fm = cm.new_source_file(FileName::Anon.into(), s.into());
let mut parser = Parser::new(Syntax::default(), StringInput::from(&*fm), None);

let mut src_map_buf = vec![];
let module = parser
.parse_module()
.map_err(|e| e.into_diagnostic(handler).emit())
Expand All @@ -95,7 +95,11 @@ fn bench_emitter(b: &mut Bencher, s: &str) {
}

b.iter(|| {
let mut buf = vec![];
let alloc = Allocator::default();
let _guard = unsafe { alloc.guard() };
let mut src_map_buf = Vec::new();

let mut buf = Vec::new();
{
let mut emitter = Emitter {
cfg: Default::default(),
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_codegen/benches/with_parse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate swc_malloc;

use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc_allocator::vec::Vec;
use swc_common::FileName;
use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{Parser, StringInput, Syntax};
Expand Down Expand Up @@ -84,7 +85,7 @@ fn bench_emitter(b: &mut Bencher, s: &str) {
b.iter(|| {
let fm = cm.new_source_file(FileName::Anon.into(), s.into());
let mut parser = Parser::new(Syntax::default(), StringInput::from(&*fm), None);
let mut src_map_buf = vec![];
let mut src_map_buf = Vec::new();
let module = parser
.parse_module()
.map_err(|e| e.into_diagnostic(handler).emit())
Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_codegen/examples/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
time::Instant,
};

use swc_allocator::vec::Vec;
use swc_common::input::SourceFileInput;
use swc_ecma_ast::*;
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
Expand All @@ -30,7 +31,7 @@ fn parse_and_gen(entry: &Path) {
.expect("failed to parse input as a module");

let mut code = vec![];
let mut srcmap = vec![];
let mut srcmap = Vec::new();

{
let mut emitter = Emitter {
Expand All @@ -56,7 +57,7 @@ fn parse_and_gen(entry: &Path) {
.expect("failed to process a module");
}

/// Usage: ./scripts/instruements path/to/input/file
/// Usage: ./scripts/instruments path/to/input/file
fn main() {
let main_file = env::args().nth(1).unwrap();

Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_codegen/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;

use swc_allocator::vec::Vec;
use swc_common::{comments::SingleThreadedComments, FileName, SourceMap};
use swc_ecma_parser;
use swc_ecma_testing::{exec_node_js, JsExecOptions};
Expand Down Expand Up @@ -44,11 +45,11 @@ impl Builder {
where
F: for<'aa> FnOnce(&mut Emitter<'aa, Box<(dyn WriteJs + 'aa)>, SourceMap>),
{
let mut buf = vec![];
let mut buf = Vec::new();

self.with(src, &mut buf, op);

String::from_utf8(buf).unwrap()
String::from_utf8_lossy(&buf).into_owned()
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_codegen/src/text_writer/basic_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Write;

use rustc_hash::FxHashSet;
use swc_allocator::vec::Vec;
use swc_common::{sync::Lrc, BytePos, LineCol, SourceMap, Span};

use super::{Result, WriteJs};
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_codegen/tests/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{fs::read_to_string, path::PathBuf};
use base64::prelude::{Engine, BASE64_STANDARD};
use rustc_hash::FxHashSet;
use sourcemap::SourceMap;
use swc_allocator::vec::Vec;
use swc_common::{comments::SingleThreadedComments, source_map::SourceMapGenConfig};
use swc_ecma_ast::EsVersion;
use swc_ecma_codegen::{text_writer::WriteJs, Emitter};
Expand Down Expand Up @@ -315,7 +316,7 @@ fn identity(entry: PathBuf) {
Some(&comments),
);
let mut parser: Parser<Lexer> = Parser::new_from(lexer);
let mut src_map = vec![];
let mut src_map = Vec::new();

{
let mut wr = Box::new(swc_ecma_codegen::text_writer::JsWriter::new(
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" }
swc_ecma_parser = { version = "0.147.0", path = "../swc_ecma_parser" }
swc_ecma_utils = { version = "0.131.0", path = "../swc_ecma_utils" }
swc_ecma_visit = { version = "0.102.0", path = "../swc_ecma_visit" }
swc_allocator = { version = "0.1.5", path = "../swc_allocator", default-features = false }

[dev-dependencies]
codspeed-criterion-compat = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_react/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde = { workspace = true, features = ["derive"], optional = true }
sha1 = { workspace = true }

string_enum = { version = "0.4.4", path = "../string_enum" }
swc_allocator = { version = "0.1.5", path = "../swc_allocator", default-features = false }
swc_atoms = { version = "0.6.5", path = "../swc_atoms" }
swc_common = { version = "0.35.0", path = "../swc_common" }
swc_config = { version = "0.1.13", path = "../swc_config" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use swc_allocator::vec::Vec;
use swc_common::{comments::SingleThreadedComments, sync::Lrc, FileName, Mark, SourceMap};
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
use swc_ecma_parser::{Parser, StringInput};
Expand Down Expand Up @@ -40,8 +41,8 @@ fn emit(
comments: Lrc<SingleThreadedComments>,
program: &Module,
) -> String {
let mut src_map_buf = vec![];
let mut buf = vec![];
let mut src_map_buf = Vec::new();
let mut buf = std::vec::Vec::new();
{
let writer = Box::new(JsWriter::new(
source_map.clone(),
Expand Down
1 change: 1 addition & 0 deletions crates/swc_fast_ts_strip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ version = "0.2.2"
[dependencies]
anyhow = { workspace = true }
serde = { workspace = true, features = ["derive"] }
swc_allocator = { version = "0.1.5", path = "../swc_allocator", default-features = false }

swc_common = { version = "0.35.0", path = "../swc_common", features = [
"sourcemap",
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub fn operate(

let mut src = vec![];
let mut src_map_buf = if options.source_map {
Some(vec![])
Some(swc_allocator::vec::Vec::new())
} else {
None
};
Expand Down

0 comments on commit 970f323

Please sign in to comment.