Skip to content
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

perf: return a string when transpiling #283

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
38 changes: 14 additions & 24 deletions src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::string::FromUtf8Error;

use base64::Engine;
use thiserror::Error;

Expand Down Expand Up @@ -52,24 +50,6 @@ impl Default for EmitOptions {
}
}

/// Source emitted based on the emit options.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
pub struct EmittedSourceBytes {
/// Emitted text as utf8 bytes.
pub source: Vec<u8>,
/// Source map back to the original file.
pub source_map: Option<Vec<u8>>,
}

impl EmittedSourceBytes {
/// Convenience method to convert the emitted source bytes into a string.
pub fn into_string(self) -> Result<EmittedSourceText, FromUtf8Error> {
let text = String::from_utf8(self.source)?;
let source_map = self.source_map.map(String::from_utf8).transpose()?;
Ok(EmittedSourceText { text, source_map })
}
}

/// Source emitted based on the emit options.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
pub struct EmittedSourceText {
Expand All @@ -96,7 +76,7 @@ pub fn emit(
comments: &dyn crate::swc::common::comments::Comments,
source_map: &SourceMap,
emit_options: &EmitOptions,
) -> Result<EmittedSourceBytes, EmitError> {
) -> Result<EmittedSourceText, EmitError> {
let source_map = source_map.inner();
let mut src_map_buf = vec![];
let mut src_buf = vec![];
Expand Down Expand Up @@ -174,9 +154,19 @@ pub fn emit(
}
}

Ok(EmittedSourceBytes {
source: src_buf,
source_map: map,
debug_assert!(std::str::from_utf8(&src_buf).is_ok(), "valid utf-8");
if let Some(map) = &map {
debug_assert!(std::str::from_utf8(&map).is_ok(), "valid utf-8");
}

// It's better to return a string here because then we can pass this to deno_core/v8
// as a known string, so it doesn't need to spend any time analyzing it.
Ok(EmittedSourceText {
// SAFETY: swc appends UTF-8 bytes to the JsWriter, so we can safely assume
// that the final string is UTF-8 (unchecked for performance reasons)
text: unsafe { String::from_utf8_unchecked(src_buf) },
// SAFETY: see above comment
source_map: map.map(|b| unsafe { String::from_utf8_unchecked(b) }),
})
}

Expand Down
2 changes: 0 additions & 2 deletions src/transpiling/jsx_precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3049,8 +3049,6 @@ const a = _jsxTemplate($$_tpl_1, _jsxAttr("class", "foo"), _jsxAttr("className",
},
)
.unwrap()
.into_string()
.unwrap()
.text
}
}
88 changes: 19 additions & 69 deletions src/transpiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::swc::transforms::typescript;
use crate::swc::visit::FoldWith;
use crate::EmitError;
use crate::EmitOptions;
use crate::EmittedSourceBytes;
use crate::EmittedSourceText;
use crate::Globals;
use crate::Marks;
use crate::ModuleSpecifier;
Expand All @@ -47,13 +47,13 @@ pub enum TranspileResult {
/// The `ParsedSource` needed to be cloned in order to transpile.
///
/// This is a performance issue and you should strive to get an `Owned` result.
Cloned(EmittedSourceBytes),
Cloned(EmittedSourceText),
/// The emit occured consuming the `ParsedSource` without cloning.
Owned(EmittedSourceBytes),
Owned(EmittedSourceText),
}

impl TranspileResult {
pub fn into_source(self) -> EmittedSourceBytes {
pub fn into_source(self) -> EmittedSourceText {
match self {
TranspileResult::Owned(source) => source,
TranspileResult::Cloned(source) => source,
Expand Down Expand Up @@ -223,7 +223,7 @@ impl ParsedSource {
&self,
transpile_options: &TranspileOptions,
emit_options: &EmitOptions,
) -> Result<EmittedSourceBytes, TranspileError> {
) -> Result<EmittedSourceText, TranspileError> {
let program = (*self.program()).clone();
transpile(
self.specifier().clone(),
Expand All @@ -246,7 +246,7 @@ impl ParsedSource {
self,
transpile_options: &TranspileOptions,
emit_options: &EmitOptions,
) -> Result<Result<EmittedSourceBytes, TranspileError>, ParsedSource> {
) -> Result<Result<EmittedSourceText, TranspileError>, ParsedSource> {
let inner = match Arc::try_unwrap(self.0) {
Ok(inner) => inner,
Err(inner) => return Err(ParsedSource(inner)),
Expand Down Expand Up @@ -328,7 +328,7 @@ fn transpile(
transpile_options: &TranspileOptions,
emit_options: &EmitOptions,
diagnostics: &[ParseDiagnostic],
) -> Result<EmittedSourceBytes, TranspileError> {
) -> Result<EmittedSourceText, TranspileError> {
if transpile_options.use_decorators_proposal
&& transpile_options.use_ts_decorators
{
Expand Down Expand Up @@ -676,9 +676,7 @@ export class A {
let transpiled_source = module
.transpile(&TranspileOptions::default(), &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
let expected_text = r#"var D;
(function(D) {
D[D["A"] = 0] = "A";
Expand Down Expand Up @@ -735,9 +733,7 @@ export class A {
let transpiled_source = module
.transpile(&TranspileOptions::default(), &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
let expected_text = r#"function _using_ctx() {
var _disposeSuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed) {
var err = new Error();
Expand Down Expand Up @@ -838,9 +834,7 @@ try {
let transpiled_source = module
.transpile(&TranspileOptions::default(), &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
assert!(transpiled_source
.text
.contains("React.createElement(\"div\", null"));
Expand Down Expand Up @@ -869,9 +863,7 @@ try {
let transpiled_source = module
.transpile(&TranspileOptions::default(), &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
assert!(transpiled_source
.text
.contains("React.createElement(\"my:tag\", null"));
Expand Down Expand Up @@ -911,8 +903,6 @@ function App() {
)
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"import { h, Fragment } from "https://deno.land/x/mod.ts";
function App() {
Expand Down Expand Up @@ -952,8 +942,6 @@ function App() {
)
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"/** @jsxImportSource jsx_lib */ import { jsx as _jsx, Fragment as _Fragment } from "jsx_lib/jsx-runtime";
function App() {
Expand Down Expand Up @@ -998,8 +986,6 @@ function App() {
)
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"import { jsx as _jsx, Fragment as _Fragment } from "jsx_lib/jsx-runtime";
function App() {
Expand Down Expand Up @@ -1045,8 +1031,6 @@ function App() {
)
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "jsx_lib/jsx-dev-runtime";
function App() {
Expand Down Expand Up @@ -1092,8 +1076,6 @@ function App() {
.transpile(&transpile_options, &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"/** @jsxImportSource jsx_lib */ const { "jsx": _jsx, "Fragment": _Fragment } = await import("jsx_lib/jsx-runtime");
const example = await import("example");
Expand Down Expand Up @@ -1146,8 +1128,6 @@ function App() {
)
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
Expand Down Expand Up @@ -1213,8 +1193,6 @@ _ts_decorate([
)
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected =
include_str!("./testdata/tc39_decorator_proposal_output.txt");
Expand Down Expand Up @@ -1283,8 +1261,6 @@ _ts_decorate([
.transpile(&TranspileOptions::default(), &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"function enumerable(value) {
return function(_target, _propertyKey, descriptor) {
Expand Down Expand Up @@ -1334,8 +1310,6 @@ export function g() {
.transpile(&transpile_options, &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"export function g() {
let algorithm;
Expand Down Expand Up @@ -1366,8 +1340,6 @@ for (let i = 0; i < testVariable >> 1; i++) callCount++;
.transpile(&Default::default(), &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected = r#"for(let i = 0; i < testVariable >> 1; i++)callCount++;"#;
assert_eq!(&code[..expected.len()], expected);
Expand Down Expand Up @@ -1508,9 +1480,7 @@ for (let i = 0; i < testVariable >> 1; i++) callCount++;
let transpiled = p
.transpile(&Default::default(), &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
let lines: Vec<&str> = transpiled.text.split('\n').collect();
let last_line = lines.last().unwrap();
let input = last_line
Expand Down Expand Up @@ -1544,8 +1514,6 @@ for (let i = 0; i < testVariable >> 1; i++) callCount++;
.transpile(&transpile_options, &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected1 = r#"import { jsx as _jsx, jsxTemplate as _jsxTemplate, jsxAttr as _jsxAttr } from "react/jsx-runtime";
const $$_tpl_2 = [
Expand Down Expand Up @@ -1589,8 +1557,6 @@ const a = _jsx(Foo, {
.transpile(&transpile_options, &EmitOptions::default())
.unwrap()
.into_source()
.into_string()
.unwrap()
.text;
let expected1 = r#"import bar from "./bar.ts";
import baz from "./baz.ts";
Expand Down Expand Up @@ -1620,9 +1586,7 @@ const b = 1;
let emit_result = module
.transpile(&TranspileOptions::default(), &emit_options)
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
let expected1 = r#"{
const foo = "bar";
}
Expand Down Expand Up @@ -1652,9 +1616,7 @@ const b = 1;
let emit_result = module
.transpile(&TranspileOptions::default(), &emit_options)
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
assert_eq!(
&emit_result.text,
r#"{
Expand Down Expand Up @@ -1692,9 +1654,7 @@ const b = 1;
let emit_result = module
.transpile(&TranspileOptions::default(), &emit_options)
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
assert_eq!(
&emit_result.text,
r#"{
Expand Down Expand Up @@ -1776,9 +1736,7 @@ const b = 1;
let emit_result = module
.transpile(&TranspileOptions::default(), &emit_options)
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
assert_eq!(
&emit_result.text,
r#"{
Expand Down Expand Up @@ -1814,9 +1772,7 @@ const b = 1;
let emit_result = module
.transpile(&TranspileOptions::default(), &emit_options)
.unwrap()
.into_source()
.into_string()
.unwrap();
.into_source();
assert_eq!(
&emit_result.text,
r#"{
Expand Down Expand Up @@ -1849,8 +1805,6 @@ const b = 1;
},
)
.unwrap()
.unwrap()
.into_string()
.unwrap();
assert_eq!(&emit_result.text, "const foo = \"bar\";\n");
}
Expand Down Expand Up @@ -1894,8 +1848,6 @@ const b = 1;
},
)
.unwrap()
.unwrap()
.into_string()
.unwrap();
assert_eq!(&emit_result.text, "const foo = \"bar\";\n");
}
Expand Down Expand Up @@ -1928,9 +1880,7 @@ const b = 1;
.unwrap();
let emit_result = match emit_result {
TranspileResult::Owned(_) => unreachable!(),
TranspileResult::Cloned(emit_result) => {
emit_result.into_string().unwrap()
}
TranspileResult::Cloned(emit_result) => emit_result,
};
assert_eq!(&emit_result.text, "const foo = \"bar\";\n");

Expand All @@ -1946,7 +1896,7 @@ const b = 1;
.unwrap();
let emit_result = match emit_result {
TranspileResult::Cloned(_) => unreachable!(),
TranspileResult::Owned(emit_result) => emit_result.into_string().unwrap(),
TranspileResult::Owned(emit_result) => emit_result,
};
assert_eq!(&emit_result.text, "const foo = \"bar\";\n");
}
Expand Down
Loading