Skip to content

Commit 3289e55

Browse files
CopilotBrooooooklyn
andcommitted
Replace fallback implementation with optimized oxc-sourcemap approach
Co-authored-by: Brooooooklyn <[email protected]>
1 parent 7db66bd commit 3289e55

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ harness = false
2525

2626
[dependencies]
2727
anyhow = "1"
28+
serde = "1"
29+
serde_json = "1"
2830

2931
[dev-dependencies]
3032
criterion = { version = "0.5", features = ["html_reports"] }
31-
serde_json = "1"
3233

3334
[profile.bench]
3435
lto = true

src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ macro_rules! tri {
9696
#[cfg_attr(target_arch = "aarch64", allow(unused))]
9797
#[inline]
9898
pub fn encode_str_fallback<S: AsRef<str>>(input: S) -> String {
99-
let mut output = String::with_capacity(input.as_ref().len() + 2);
100-
let writer = unsafe { output.as_mut_vec() };
101-
writer.push(b'"');
102-
encode_str_inner(input.as_ref().as_bytes(), writer);
103-
writer.push(b'"');
104-
output
99+
let s = input.as_ref();
100+
let mut escaped_buf = Vec::with_capacity(s.len() * 2 + 2);
101+
// This call is infallible as only error it can return is if the writer errors.
102+
// Writing to a `Vec<u8>` is infallible, so that's not possible here.
103+
serde::Serialize::serialize(s, &mut serde_json::Serializer::new(&mut escaped_buf)).unwrap();
104+
// Safety: `escaped_buf` is valid utf8.
105+
unsafe { String::from_utf8_unchecked(escaped_buf) }
105106
}
106107

107108
#[cfg(not(target_arch = "aarch64"))]

0 commit comments

Comments
 (0)