Skip to content

Commit acd7bbb

Browse files
authored
refactor: Use BytesStr instead of Arc<str> (#1)
`BytesStr` is a more efficient variant of `Arc<str>` Since swc-project/swc#10580, SWC also uses it to store the source map strings
1 parent 804d663 commit acd7bbb

26 files changed

Lines changed: 195 additions & 177 deletions

.github/workflows/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
name: CI
22

3+
34
on:
5+
merge_group:
6+
pull_request:
7+
types: ["opened", "reopened", "synchronize"]
48
push:
59
branches:
6-
- master
7-
- "release/**"
8-
pull_request:
10+
- main
911

1012
env:
1113
RUSTFLAGS: -Dwarnings

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rust-analyzer.cargo.features": "all",
3+
}

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
2-
name = "sourcemap"
3-
version = "9.2.2"
4-
authors = ["Sentry <hello@sentry.io>"]
2+
name = "swc_sourcemap"
3+
version = "9.3.0"
4+
authors = ["Sentry <hello@sentry.io>", "swc-project <https://swc.rs>"]
55
keywords = ["javascript", "sourcemap", "sourcemaps"]
6-
description = "Basic sourcemap handling for Rust"
7-
homepage = "https://github.com/getsentry/rust-sourcemap"
8-
repository = "https://github.com/getsentry/rust-sourcemap"
6+
description = "Forked from https://github.com/getsentry/rust-sourcemap"
7+
homepage = "https://github.com/swc-project/swc-sourcemap"
8+
repository = "https://github.com/swc-project/swc-sourcemap"
99
license = "BSD-3-Clause"
1010
readme = "README.md"
1111
edition = "2018"
@@ -34,6 +34,7 @@ debugid = {version = "0.8.0", features = ["serde"] }
3434
base64-simd = { version = "0.8" }
3535
bitvec = "1.0.1"
3636
rustc-hash = "2.1.1"
37+
bytes-str = { version = "0.2.4", features = ["serde"] }
3738

3839
[features]
3940
ram_bundle = ["scroll"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ memory intensive.
2828
Usage:
2929

3030
```rust
31-
use sourcemap::SourceMap;
31+
use swc_sourcemap::SourceMap;
3232
let input: &[_] = b"{
3333
\"version\":3,
3434
\"sources\":[\"coolstuff.js\"],

cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
33
use std::process;
44

55
use argh::FromArgs;
6-
use sourcemap::{DecodedMap, SourceView, Token};
6+
use swc_sourcemap::{DecodedMap, SourceView, Token};
77

88
/// Utility for working with source maps.
99
#[derive(FromArgs, Debug)]

examples/read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::fs;
33
use std::io::Read;
44

5-
use sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};
5+
use swc_sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};
66

77
fn load_from_reader<R: Read>(mut rdr: R) -> SourceMap {
88
match decode(&mut rdr).unwrap() {

examples/rewrite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fs;
33
use std::io::Read;
44
use std::path::Path;
55

6-
use sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};
6+
use swc_sourcemap::{decode, DecodedMap, RewriteOptions, SourceMap};
77

88
fn test(sm: &SourceMap) {
99
for (src_id, source) in sm.sources().enumerate() {
@@ -14,7 +14,7 @@ fn test(sm: &SourceMap) {
1414
if f.read_to_string(&mut contents).ok().is_none() {
1515
continue;
1616
}
17-
if Some(contents.as_str()) != sm.get_source_contents(src_id as u32) {
17+
if Some(contents.as_str()) != sm.get_source_contents(src_id as u32).map(|v| &**v) {
1818
println!(" !!! {source}");
1919
}
2020
}

examples/split_ram_bundle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::fs;
33
use std::fs::File;
44
use std::path::Path;
55

6-
use sourcemap::ram_bundle::{split_ram_bundle, RamBundle, RamBundleType};
7-
use sourcemap::SourceMapIndex;
6+
use swc_sourcemap::ram_bundle::{split_ram_bundle, RamBundle, RamBundleType};
7+
use swc_sourcemap::SourceMapIndex;
88

99
const USAGE: &str = "
1010
Usage:

src/builder.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::env;
55
use std::fs;
66
use std::io::Read;
77
use std::path::{Path, PathBuf};
8-
use std::sync::Arc;
98

9+
use bytes_str::BytesStr;
1010
use debugid::DebugId;
1111
use rustc_hash::FxHashMap;
1212
use url::Url;
@@ -20,14 +20,14 @@ use crate::types::{RawToken, SourceMap, Token};
2020
/// objects is generally not very comfortable. As a general aid this
2121
/// type can help.
2222
pub struct SourceMapBuilder {
23-
file: Option<Arc<str>>,
24-
name_map: FxHashMap<Arc<str>, u32>,
25-
names: Vec<Arc<str>>,
23+
file: Option<BytesStr>,
24+
name_map: FxHashMap<BytesStr, u32>,
25+
names: Vec<BytesStr>,
2626
tokens: Vec<RawToken>,
27-
source_map: FxHashMap<Arc<str>, u32>,
28-
source_root: Option<Arc<str>>,
29-
sources: Vec<Arc<str>>,
30-
source_contents: Vec<Option<Arc<str>>>,
27+
source_map: FxHashMap<BytesStr, u32>,
28+
source_root: Option<BytesStr>,
29+
sources: Vec<BytesStr>,
30+
source_contents: Vec<Option<BytesStr>>,
3131
sources_mapping: Vec<u32>,
3232
ignore_list: BTreeSet<u32>,
3333
debug_id: Option<DebugId>,
@@ -52,9 +52,9 @@ fn resolve_local_reference(base: &Url, reference: &str) -> Option<PathBuf> {
5252

5353
impl SourceMapBuilder {
5454
/// Creates a new source map builder and sets the file.
55-
pub fn new(file: Option<&str>) -> SourceMapBuilder {
55+
pub fn new(file: Option<BytesStr>) -> SourceMapBuilder {
5656
SourceMapBuilder {
57-
file: file.map(Into::into),
57+
file,
5858
name_map: FxHashMap::default(),
5959
names: vec![],
6060
tokens: vec![],
@@ -74,7 +74,7 @@ impl SourceMapBuilder {
7474
}
7575

7676
/// Sets the file for the sourcemap (optional)
77-
pub fn set_file<T: Into<Arc<str>>>(&mut self, value: Option<T>) {
77+
pub fn set_file<T: Into<BytesStr>>(&mut self, value: Option<T>) {
7878
self.file = value.map(Into::into);
7979
}
8080

@@ -84,7 +84,7 @@ impl SourceMapBuilder {
8484
}
8585

8686
/// Sets a new value for the source_root.
87-
pub fn set_source_root<T: Into<Arc<str>>>(&mut self, value: Option<T>) {
87+
pub fn set_source_root<T: Into<BytesStr>>(&mut self, value: Option<T>) {
8888
self.source_root = value.map(Into::into);
8989
}
9090

@@ -94,24 +94,24 @@ impl SourceMapBuilder {
9494
}
9595

9696
/// Registers a new source with the builder and returns the source ID.
97-
pub fn add_source(&mut self, src: &str) -> u32 {
97+
pub fn add_source(&mut self, src: BytesStr) -> u32 {
9898
self.add_source_with_id(src, !0)
9999
}
100100

101-
fn add_source_with_id(&mut self, src: &str, old_id: u32) -> u32 {
101+
fn add_source_with_id(&mut self, src: BytesStr, old_id: u32) -> u32 {
102102
let count = self.sources.len() as u32;
103-
let id = *self.source_map.entry(src.into()).or_insert(count);
103+
let id = *self.source_map.entry(src.clone()).or_insert(count);
104104
if id == count {
105-
self.sources.push(src.into());
105+
self.sources.push(src);
106106
self.sources_mapping.push(old_id);
107107
}
108108
id
109109
}
110110

111111
/// Changes the source name for an already set source.
112-
pub fn set_source(&mut self, src_id: u32, src: &str) {
112+
pub fn set_source(&mut self, src_id: u32, src: BytesStr) {
113113
assert!(src_id != !0, "Cannot set sources for tombstone source id");
114-
self.sources[src_id as usize] = src.into();
114+
self.sources[src_id as usize] = src;
115115
}
116116

117117
/// Looks up a source name for an ID.
@@ -124,12 +124,12 @@ impl SourceMapBuilder {
124124
}
125125

126126
/// Sets the source contents for an already existing source.
127-
pub fn set_source_contents(&mut self, src_id: u32, contents: Option<&str>) {
127+
pub fn set_source_contents(&mut self, src_id: u32, contents: Option<BytesStr>) {
128128
assert!(src_id != !0, "Cannot set sources for tombstone source id");
129129
if self.sources.len() > self.source_contents.len() {
130130
self.source_contents.resize(self.sources.len(), None);
131131
}
132-
self.source_contents[src_id as usize] = contents.map(Into::into);
132+
self.source_contents[src_id as usize] = contents;
133133
}
134134

135135
/// Returns the current source contents for a source.
@@ -169,7 +169,7 @@ impl SourceMapBuilder {
169169
if let Ok(mut f) = fs::File::open(path) {
170170
let mut contents = String::new();
171171
if f.read_to_string(&mut contents).is_ok() {
172-
self.set_source_contents(src_id, Some(&contents));
172+
self.set_source_contents(src_id, Some(contents.into()));
173173
}
174174
}
175175
}
@@ -178,11 +178,11 @@ impl SourceMapBuilder {
178178
}
179179

180180
/// Registers a name with the builder and returns the name ID.
181-
pub fn add_name(&mut self, name: &str) -> u32 {
181+
pub fn add_name(&mut self, name: BytesStr) -> u32 {
182182
let count = self.names.len() as u32;
183-
let id = *self.name_map.entry(name.into()).or_insert(count);
183+
let id = *self.name_map.entry(name.clone()).or_insert(count);
184184
if id == count {
185-
self.names.push(name.into());
185+
self.names.push(name);
186186
}
187187
id
188188
}
@@ -195,8 +195,8 @@ impl SourceMapBuilder {
195195
dst_col: u32,
196196
src_line: u32,
197197
src_col: u32,
198-
source: Option<&str>,
199-
name: Option<&str>,
198+
source: Option<BytesStr>,
199+
name: Option<BytesStr>,
200200
is_range: bool,
201201
) -> RawToken {
202202
self.add_with_id(
@@ -211,9 +211,9 @@ impl SourceMapBuilder {
211211
dst_col: u32,
212212
src_line: u32,
213213
src_col: u32,
214-
source: Option<&str>,
214+
source: Option<BytesStr>,
215215
source_id: u32,
216-
name: Option<&str>,
216+
name: Option<BytesStr>,
217217
is_range: bool,
218218
) -> RawToken {
219219
let src_id = match source {
@@ -273,9 +273,9 @@ impl SourceMapBuilder {
273273
token.get_dst_col(),
274274
token.get_src_line(),
275275
token.get_src_col(),
276-
token.get_source(),
276+
token.get_source().cloned(),
277277
token.get_src_id(),
278-
name,
278+
name.cloned(),
279279
token.is_range(),
280280
)
281281
}
@@ -289,7 +289,7 @@ impl SourceMapBuilder {
289289
prefix.push('/');
290290
}
291291
if source.starts_with(&prefix) {
292-
*source = source[prefix.len()..].into();
292+
source.advance(prefix.len());
293293
break;
294294
}
295295
}

src/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ fn decode_index(rsm: RawSourceMap) -> Result<SourceMapIndex> {
297297

298298
// file sometimes is not a string for unexplicable reasons
299299
let file = rsm.file.map(|val| match val {
300-
Value::String(s) => s,
300+
Value::String(s) => s.into(),
301301
_ => "<invalid>".into(),
302302
});
303303

0 commit comments

Comments
 (0)