8000 refactor(swc_common): Use `BytesStr` instead of `Lrc<String>` by kdy1 · Pull Request #10580 · swc-project/swc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor(swc_common): Use BytesStr instead of Lrc<String> #10580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-ghosts-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_common: major
---

refactor(swc_common): Use `BytesStr` instead of `Lrc<String>`
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
],
"eslint.enable": false,
"rust-analyzer.check.command": "clippy",
"rust-analyzer.cargo.features": [
"plugin"
],
// SWC project is too complex to use rust-analyzer
"rust-analyzer.diagnostics.enable": false
}
21 changes: 19 additions & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ resolver = "2"
bitflags = "2.5.0"
browserslist-rs = "0.18.1"
bumpalo = "3.16.0"
bytes-str = "0.2.3"
cargo_metadata = "0.18.1"
cfg-if = "1.0.0"
changesets = "0.2.2"
Expand Down
6 changes: 3 additions & 3 deletions crates/binding_macros/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ macro_rules! build_minify_sync {
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
};

let fm = c.cm.new_source_file($crate::wasm::FileName::Anon.into(), s.into());
let fm = c.cm.new_source_file($crate::wasm::FileName::Anon.into(), String::from(s));
let program = $crate::wasm::anyhow::Context::context(c.minify(fm, handler, &opts, Default::default()), "failed to minify file")?;

program
Expand Down Expand Up @@ -171,7 +171,7 @@ macro_rules! build_parse_sync {
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to parse options: {}", e))?
};

let fm = c.cm.new_source_file($crate::wasm::FileName::Anon.into(), s.into());
let fm = c.cm.new_source_file($crate::wasm::FileName::Anon.into(), String::from(s));

let cmts = c.comments().clone();
let comments = if opts.comments {
Expand Down Expand Up @@ -379,7 +379,7 @@ macro_rules! build_transform_sync {
} else {
$crate::wasm::FileName::Real(opts.filename.clone().into()).into()
},
s.into(),
String::from(s),
);
let cm = c.cm.clone();
let file = fm.clone();
Expand Down
1 change: 1 addition & 0 deletions crates/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ manual-tokio-runtime = []
[dependencies]
anyhow = { workspace = true }
base64 = { workspace = true }
bytes-str = { workspace = true }
dashmap = { workspace = true }
either = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
Expand Down
5 changes: 3 additions & 2 deletions crates/swc/benches/oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate swc_malloc;

use std::{fs, io::stderr, sync::Arc};

use bytes_str::BytesStr;
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc::config::{Config, JscConfig, Options, TransformConfig};
use swc_common::{errors::Handler, FileName, FilePathMapping, SourceMap, GLOBALS};
Expand All @@ -26,13 +27,13 @@ fn mk() -> swc::Compiler {
fn bench_full(b: &mut Bencher, filename: &str, opts: &Options) {
let c = mk();

let source = Arc::new(fs::read_to_string(filename).unwrap());
let source: BytesStr = fs::read_to_string(filename).unwrap().into();

b.iter(|| {
GLOBALS.set(&Default::default(), || {
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));

let fm = c.cm.new_source_file_from(
let fm = c.cm.new_source_file(
FileName::Real(filename.to_string().into()).into(),
black_box(source.clone()),
);
Expand Down
6 changes: 2 additions & 4 deletions crates/swc/examples/transform_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ fn main() {
skip_filename: false,
},
|handler| {
let fm = cm.new_source_file(
FileName::Custom("foo.js".into()).into(),
"this ?= foo".into(),
);
let fm =
cm.new_source_file(FileName::Custom("foo.js".into()).into(), "this ?= foo");

c.process_js_file(fm, handler, &Default::default())
.context("failed to process file")
Expand Down
3 changes: 2 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use anyhow::{bail, Context, Error};
use bytes_str::BytesStr;
use dashmap::DashMap;
use either::Either;
use indexmap::IndexMap;
Expand Down Expand Up @@ -1569,7 +1570,7 @@ pub struct HiddenTransformConfig {
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct ConstModulesConfig {
#[serde(default)]
pub globals: FxHashMap<Atom, FxHashMap<Atom, String>>,
pub globals: FxHashMap<Atom, FxHashMap<Atom, BytesStr>>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)]
Expand Down
6 changes: 2 additions & 4 deletions crates/swc/tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,7 @@ fn should_visit() {
"
import React from 'react';
const comp = () => <amp-something className='something' />;
"
.into(),
",
);
let comments = SingleThreadedComments::default();
let config = c
Expand Down Expand Up @@ -910,8 +909,7 @@ fn issue_1984() {
? new Set()
: new Set(derivedHalfSelectedKeys);
}
"
.into(),
",
);

c.minify(
Expand Down
15 changes: 5 additions & 10 deletions crates/swc/tests/rust_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fn test_visit_mut() {
FileName::Anon.into(),
"
console.log(5 as const)
"
.into(),
",
);

let res = c.process_js_with_custom_pass(
Expand Down Expand Up @@ -74,8 +73,7 @@ fn shopify_1_check_filename() {
const [i18n] = useI18n();
return <h1>{i18n.translate('foo')}</h1>
}
"
.into(),
",
);

let res = c.process_js_with_custom_pass(
Expand Down Expand Up @@ -173,8 +171,7 @@ fn shopify_2_same_opt() {
const [i18n] = useI18n();
return <h1>{i18n.translate('foo')}</h1>
}
"
.into(),
",
);

let res = c.process_js_with_custom_pass(
Expand Down Expand Up @@ -241,8 +238,7 @@ fn shopify_3_reduce_defaults() {
const [i18n] = useI18n();
return <h1>{i18n.translate('foo')}</h1>
}
"
.into(),
",
);

let res = c.process_js_with_custom_pass(
Expand Down Expand Up @@ -304,8 +300,7 @@ fn shopify_4_reduce_more() {
const [i18n] = useI18n();
return <h1>{i18n.translate('foo')}</h1>
}
"
.into(),
",
);

let res = c.process_js_with_custom_pass(
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn compile(src: &str, options: Options) -> String {
.print_errors(|cm, handler| {
let c = Compiler::new(cm.clone());

let fm = cm.new_source_file(FileName::Real("input.js".into()).into(), src.into());
let fm = cm.new_source_file(FileName::Real("input.js".into()).into(), src.to_string());
let s = c.process_js_file(fm, &handler, &options);

match s {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/bundler/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) struct Helpers {

fn parse(code: &'static str, name: &'static str) -> Vec<ModuleItem> {
let cm = SourceMap::new(FilePathMapping::empty());
let fm = cm.new_source_file(FileName::Custom(name.into()).into(), code.into());
let fm = cm.new_source_file(FileName::Custom(name.into()).into(), code);
parse_file_as_module(
&fm,
Default::default(),
Expand Down
7 changes: 4 additions & 3 deletions crates/swc_bundler/src/bundler/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ impl Tester<'_> {

#[allow(dead_code)]
pub fn parse(&self, s: &str) -> Module {
let fm = self
.cm
.new_source_file(FileName::Real(PathBuf::from("input.js")).into(), s.into());
let fm = self.cm.new_source_file(
FileName::Real(PathBuf::from("input.js")).into(),
s.to_string(),
);

let lexer = Lexer::new(
Default::default(),
Expand Down
12 changes: 10 additions & 2 deletions crates/swc_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ plugin_transform_schema_vtest = []

tty-emitter = ["termcolor"]

__rkyv = []
rkyv-impl = ["__rkyv", "rkyv", "swc_atoms/rkyv-impl", "bytecheck", "rancor"]
__rkyv = []
rkyv-impl = [
"__rkyv",
"rkyv",
"swc_atoms/rkyv-impl",
"bytes-str/rkyv",
"bytecheck",
"rancor",
]

shrink-to-fit = ["dep:shrink-to-fit", "swc_atoms/shrink-to-fit"]

Expand All @@ -42,6 +49,7 @@ shrink-to-fit = ["dep:shrink-to-fit", "swc_atoms/shrink-to-fit"]
anyhow = { workspace = true }
arbitrary = { workspace = true, features = ["derive"], optional = true }
bytecheck = { workspace = true, optional = true }
bytes-str = { workspace = true, features = ["serde"] }
cfg-if = { workspace = true }
either = { workspace = true }
new_debug_unreachable = { workspace = true }
Expand Down
10 changes: 4 additions & 6 deletions crates/swc_common/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,15 @@ pub trait Input<'a>: Clone {

#[cfg(test)]
mod tests {
use std::sync::Arc;

use super::*;
use crate::{FileName, FilePathMapping, SourceMap};
use crate::{sync::Lrc, FileName, FilePathMapping, SourceMap};

fn with_test_sess<F>(src: &str, f: F)
fn with_test_sess<F>(src: &'static str, f: F)
where
F: FnOnce(StringInput<'_>),
{
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
let fm = cm.new_source_file(FileName::Real("testing".into()).into(), src.into());
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let fm = cm.new_source_file(FileName::Real("testing".into()).into(), src);

f((&*fm).into())
}
Expand Down
Loading
Loading
0