Skip to content

Commit

Permalink
add measuring of the rewrite() function
Browse files Browse the repository at this point in the history
  • Loading branch information
r58Playz committed Oct 22, 2024
1 parent 66929af commit cd8495b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 38 deletions.
13 changes: 13 additions & 0 deletions rewriter/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 rewriter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ panic = "abort"
[dependencies]
console_error_panic_hook = "0.1.7"
getrandom = { version = "0.2.15", features = ["js"] }
instant = { version = "0.1.13", features = ["wasm-bindgen"] }
js-sys = "0.3.69"
obfstr = "0.4.3"
oxc_allocator = "0.32.0"
Expand Down
35 changes: 21 additions & 14 deletions rewriter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod error;
pub mod rewrite;

use std::{panic, str::FromStr, sync::Arc};
use std::{panic, str::FromStr, sync::Arc, time::Duration};

use error::{Result, RewriterError};
use instant::Instant;
use js_sys::{Function, Object, Reflect};
use oxc_diagnostics::{NamedSource, OxcDiagnostic};
use rewrite::{rewrite, Config, EncodeFn};
Expand All @@ -12,7 +13,7 @@ use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const REWRITER_OUTPUT: &'static str = r#"
type RewriterOutput = { js: Uint8Array, errors: string[] };
type RewriterOutput = { js: Uint8Array, errors: string[], duration: bigint };
"#;

#[wasm_bindgen]
Expand Down Expand Up @@ -110,10 +111,15 @@ fn drmcheck() -> bool {
return vec![obfstr!("http://localhost:1337")].contains(&true_origin.as_str());
}

fn duration_to_millis_f64(duration: Duration) -> f64 {
(duration.as_secs() as f64) * 1_000f64 + (duration.subsec_nanos() as f64) / 1_000_000f64
}

fn create_rewriter_output(
out: (Vec<u8>, Vec<OxcDiagnostic>),
url: String,
src: String,
duration: Duration,
) -> Result<RewriterOutput> {
let src = Arc::new(NamedSource::new(url, src).with_language("javascript"));
let errs: Vec<_> = out
Expand All @@ -123,8 +129,9 @@ fn create_rewriter_output(
.collect();

let obj = Object::new();
set_obj(&obj, "js", &JsValue::from(out.0))?;
set_obj(&obj, "errors", &JsValue::from(errs))?;
set_obj(&obj, "js", &out.0.into())?;
set_obj(&obj, "errors", &errs.into())?;
set_obj(&obj, "duration", &duration_to_millis_f64(duration).into())?;

Ok(RewriterOutput::from(JsValue::from(obj)))
}
Expand All @@ -141,11 +148,11 @@ pub fn rewrite_js(
return Vec::new();
}

create_rewriter_output(
rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?,
script_url,
js.to_string(),
)
let before = Instant::now();
let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?;
let after = Instant::now();

create_rewriter_output(out, script_url, js.to_string(), after - before)
}

#[wasm_bindgen]
Expand All @@ -163,9 +170,9 @@ pub fn rewrite_js_from_arraybuffer(
// we know that this is a valid utf-8 string
let js = unsafe { std::str::from_utf8_unchecked(js) };

create_rewriter_output(
rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?,
script_url,
js.to_string(),
)
let before = Instant::now();
let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?;
let after = Instant::now();

create_rewriter_output(out, script_url, js.to_string(), after - before)
}
65 changes: 41 additions & 24 deletions src/shared/rewriters/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
initSync,
rewrite_js,
rewrite_js_from_arraybuffer,
RewriterOutput,
} from "../../../rewriter/out/rewriter.js";
import { $scramjet, flagEnabled } from "../../scramjet";

Expand All @@ -19,13 +20,51 @@ init();

Error.stackTraceLimit = 50;

function print_errors(errors: string[]) {
const decoder = new TextDecoder();

function rewriteJsWrapper(
input: string | ArrayBuffer,
meta: URLMeta
): string | ArrayBuffer {
let out: RewriterOutput;
if (typeof input === "string") {
out = rewrite_js(
input,
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
} else {
out = rewrite_js_from_arraybuffer(
new Uint8Array(input),
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
}
const { js, errors, duration } = out;

// TODO: maybe make this a scram flag?
if (true) {
for (const error of errors) {
console.error("oxc parse error", error);
}
}

// TODO: maybe make this a scram flag?
if (true) {
let timespan: string;
if (duration < 1n) {
timespan = "BLAZINGLY FAST";
} else if (duration < 500n) {
timespan = "decent speed";
} else {
timespan = "really slow";
}
console.log(`oxc rewrite was ${timespan} (${duration}ms)`);
}

return typeof input === "string" ? decoder.decode(js) : js;
}

export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) {
Expand All @@ -37,29 +76,7 @@ export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) {
return rewriteJsNaiive(text);
}

// const before = performance.now();
if (typeof js === "string") {
let { js: js_out, errors } = rewrite_js(
js,
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
js = new TextDecoder().decode(js_out);
print_errors(errors);
} else {
let { js: js_out, errors } = rewrite_js_from_arraybuffer(
new Uint8Array(js),
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
js = js_out;
print_errors(errors);
}
// const after = performance.now();

// dbg.debug("Rewrite took", Math.floor((after - before) * 10) / 10, "ms");
js = rewriteJsWrapper(js, meta);

return js;
}
Expand Down

0 comments on commit cd8495b

Please sign in to comment.