cleanup and abstract some stuff

This commit is contained in:
Toshit Chawda 2025-03-20 22:47:25 -07:00
parent eadd1c9e82
commit 9f8a55f70b
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
8 changed files with 116 additions and 438 deletions

309
rewriter/Cargo.lock generated
View file

@ -2,33 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "allocator-api2" name = "allocator-api2"
version = "0.2.21" version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.97" version = "1.0.97"
@ -97,7 +76,7 @@ dependencies = [
"icu_normalizer", "icu_normalizer",
"indexmap", "indexmap",
"intrusive-collections", "intrusive-collections",
"itertools 0.13.0", "itertools",
"num-bigint", "num-bigint",
"num-integer", "num-integer",
"num-traits", "num-traits",
@ -233,12 +212,6 @@ version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]] [[package]]
name = "castaway" name = "castaway"
version = "0.2.3" version = "0.2.3"
@ -254,58 +227,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciborium"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
[[package]]
name = "ciborium-ll"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "4.5.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6088f3ae8c3608d19260cd7445411865a485688711b78b5be70d78cd96136f83"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.5.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22a7ef7f676155edfb82daa97f99441f3ebf4a58d5e32f295a56259f1b6facc8"
dependencies = [
"anstyle",
"clap_lex",
]
[[package]]
name = "clap_lex"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]] [[package]]
name = "compact_str" name = "compact_str"
version = "0.9.0" version = "0.9.0"
@ -326,73 +247,12 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "417bef24afe1460300965a25ff4a24b8b45ad011948302ec221e8a0a81eb2c79" checksum = "417bef24afe1460300965a25ff4a24b8b45ad011948302ec221e8a0a81eb2c79"
[[package]]
name = "criterion"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
dependencies = [
"anes",
"cast",
"ciborium",
"clap",
"criterion-plot",
"is-terminal",
"itertools 0.10.5",
"num-traits",
"once_cell",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools 0.10.5",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.21" version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "crunchy"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]] [[package]]
name = "dashmap" name = "dashmap"
version = "6.1.0" version = "6.1.0"
@ -471,16 +331,6 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "half"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7db2ff139bba50379da6aa0766b52fdcb62cb5b263009b09ed58ba604e14bbd1"
dependencies = [
"cfg-if",
"crunchy",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.5" version = "0.14.5"
@ -498,12 +348,6 @@ dependencies = [
"foldhash", "foldhash",
] ]
[[package]]
name = "hermit-abi"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e"
[[package]] [[package]]
name = "icu_collections" name = "icu_collections"
version = "1.5.0" version = "1.5.0"
@ -674,26 +518,6 @@ dependencies = [
"memoffset", "memoffset",
] ]
[[package]]
name = "is-terminal"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
dependencies = [
"hermit-abi",
"libc",
"windows-sys",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.13.0" version = "0.13.0"
@ -778,7 +602,6 @@ dependencies = [
"anyhow", "anyhow",
"boa_engine", "boa_engine",
"bytes", "bytes",
"criterion",
"js", "js",
"oxc", "oxc",
"url", "url",
@ -862,12 +685,6 @@ version = "1.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc" checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc"
[[package]]
name = "oorandom"
version = "11.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
[[package]] [[package]]
name = "owo-colors" name = "owo-colors"
version = "4.2.0" version = "4.2.0"
@ -1151,34 +968,6 @@ dependencies = [
"siphasher", "siphasher",
] ]
[[package]]
name = "plotters"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
[[package]]
name = "plotters-svg"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
dependencies = [
"plotters-backend",
]
[[package]] [[package]]
name = "pollster" name = "pollster"
version = "0.4.0" version = "0.4.0"
@ -1263,26 +1052,6 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.5.10" version = "0.5.10"
@ -1292,35 +1061,6 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]] [[package]]
name = "regress" name = "regress"
version = "0.10.3" version = "0.10.3"
@ -1355,15 +1095,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd29631678d6fb0903b69223673e122c32e9ae559d0960a38d574695ebc0ea15" checksum = "dd29631678d6fb0903b69223673e122c32e9ae559d0960a38d574695ebc0ea15"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.2.0"
@ -1559,16 +1290,6 @@ dependencies = [
"zerovec", "zerovec",
] ]
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.6.8" version = "0.6.8"
@ -1639,16 +1360,6 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@ -1736,24 +1447,6 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.52.6" version = "0.52.6"

View file

@ -7,7 +7,7 @@ use oxc::{
}; };
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use crate::{cfg::Config, RewriterError}; use crate::{cfg::Config, changeset::ChangeSet, RewriterError};
// const STRICTCHECKER: &str = "(function(a){arguments[0]=false;return a})(true)"; // const STRICTCHECKER: &str = "(function(a){arguments[0]=false;return a})(true)";
const STRICTCHECKER: &str = "(function(){return !this;})()"; const STRICTCHECKER: &str = "(function(){return !this;})()";
@ -161,54 +161,12 @@ impl<'data> Rewrite<'data> {
} }
} }
enum Change<'a> {
Str(&'a str),
U32(u32),
}
impl<'a> From<&'a str> for Change<'a> {
fn from(value: &'a str) -> Self {
Self::Str(value)
}
}
impl<'a> From<&'a String<'_>> for Change<'a> {
fn from(value: &'a String<'_>) -> Self {
Self::Str(value.as_str())
}
}
impl<'a> From<&'a Atom<'_>> for Change<'a> {
fn from(value: &'a Atom<'_>) -> Self {
Self::Str(value.as_str())
}
}
impl<'a> From<&'a AssignmentOperator> for Change<'a> {
fn from(value: &'a AssignmentOperator) -> Self {
Self::Str(value.as_str())
}
}
impl From<u32> for Change<'static> {
fn from(value: u32) -> Self {
Self::U32(value)
}
}
macro_rules! changes { macro_rules! changes {
[$($change:expr),+] => { [$($change:expr),+] => {
smallvec![$(Change::from($change)),+] smallvec![$(Change::from($change)),+]
}; };
} }
type Changes<'a> = SmallVec<[Change<'a>; 8]>;
enum JsChangeInner<'a> {
Insert { loc: u32, str: Changes<'a> },
Replace { str: Changes<'a> },
}
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
enum JsChange<'data> { enum JsChange<'data> {
/// insert `${cfg.wrapfn}(` /// insert `${cfg.wrapfn}(`
@ -386,6 +344,48 @@ impl Ord for JsChange<'_> {
} }
} }
enum Change<'a> {
Str(&'a str),
U32(u32),
}
impl<'a> From<&'a str> for Change<'a> {
fn from(value: &'a str) -> Self {
Self::Str(value)
}
}
impl<'a> From<&'a String<'_>> for Change<'a> {
fn from(value: &'a String<'_>) -> Self {
Self::Str(value.as_str())
}
}
impl<'a> From<&'a Atom<'_>> for Change<'a> {
fn from(value: &'a Atom<'_>) -> Self {
Self::Str(value.as_str())
}
}
impl<'a> From<&'a AssignmentOperator> for Change<'a> {
fn from(value: &'a AssignmentOperator) -> Self {
Self::Str(value.as_str())
}
}
impl From<u32> for Change<'static> {
fn from(value: u32) -> Self {
Self::U32(value)
}
}
type Changes<'a> = SmallVec<[Change<'a>; 8]>;
enum JsChangeInner<'a> {
Insert { loc: u32, str: Changes<'a> },
Replace { str: Changes<'a> },
}
pub(crate) struct JsChangeResult<'alloc> { pub(crate) struct JsChangeResult<'alloc> {
pub js: Vec<'alloc, u8>, pub js: Vec<'alloc, u8>,
pub sourcemap: Vec<'alloc, u8>, pub sourcemap: Vec<'alloc, u8>,
@ -393,20 +393,20 @@ pub(crate) struct JsChangeResult<'alloc> {
pub(crate) struct JsChanges<'alloc: 'data, 'data> { pub(crate) struct JsChanges<'alloc: 'data, 'data> {
alloc: &'alloc Allocator, alloc: &'alloc Allocator,
inner: Vec<'alloc, JsChange<'data>>, inner: ChangeSet<'alloc, JsChange<'data>>,
} }
impl<'alloc: 'data, 'data> JsChanges<'alloc, 'data> { impl<'alloc: 'data, 'data> JsChanges<'alloc, 'data> {
pub fn new(alloc: &'alloc Allocator, capacity: usize) -> Self { pub fn new(alloc: &'alloc Allocator, capacity: usize) -> Self {
Self { Self {
inner: Vec::with_capacity_in(capacity, alloc), inner: ChangeSet::new(alloc, capacity),
alloc, alloc,
} }
} }
pub fn add(&mut self, rewrite: Rewrite<'data>) { pub fn add(&mut self, rewrite: Rewrite<'data>) {
for change in rewrite.into_inner() { for change in rewrite.into_inner() {
self.inner.push(change); self.inner.add(change);
} }
} }
@ -418,8 +418,6 @@ impl<'alloc: 'data, 'data> JsChanges<'alloc, 'data> {
where where
E: Fn(&str, &'alloc Allocator) -> String<'alloc>, E: Fn(&str, &'alloc Allocator) -> String<'alloc>,
{ {
// using wrapping adds for perf, 4gb large files is really an edge case
let mut cursor = 0; let mut cursor = 0;
let mut offset = 0i32; let mut offset = 0i32;
let mut buffer = Vec::with_capacity_in(js.len() * 2, self.alloc); let mut buffer = Vec::with_capacity_in(js.len() * 2, self.alloc);
@ -453,7 +451,7 @@ impl<'alloc: 'data, 'data> JsChanges<'alloc, 'data> {
self.inner.sort(); self.inner.sort();
for change in &self.inner { for change in self.inner.iter() {
let span = change.get_span(); let span = change.get_span();
let start = span.start; let start = span.start;
let end = span.end; let end = span.end;

View file

@ -0,0 +1,33 @@
#![allow(dead_code, clippy::unused_self)]
use oxc::allocator::{Allocator, Vec};
pub struct VecChangeSet<'alloc, T: Ord> {
inner: Vec<'alloc, T>,
}
impl<'alloc, T: Ord> VecChangeSet<'alloc, T> {
pub fn new(alloc: &'alloc Allocator, capacity: usize) -> Self {
Self {
inner: Vec::with_capacity_in(capacity, alloc),
}
}
pub fn add(&mut self, t: T) {
self.inner.push(t);
}
pub fn sort(&mut self) {
self.inner.sort();
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.inner.iter()
}
}
pub type ChangeSet<'alloc, T> = VecChangeSet<'alloc, T>;

View file

@ -8,8 +8,9 @@ use oxc::{
use thiserror::Error; use thiserror::Error;
pub mod cfg; pub mod cfg;
pub mod changes; mod changes;
mod visitor; mod visitor;
mod changeset;
use cfg::Config; use cfg::Config;
use changes::{JsChangeResult, JsChanges}; use changes::{JsChangeResult, JsChanges};

View file

@ -10,14 +10,12 @@ workspace = true
anyhow = "1.0.97" anyhow = "1.0.97"
bytes = "1.10.1" bytes = "1.10.1"
oxc = { workspace = true } oxc = { workspace = true }
js = { version = "0.1.0", path = "../js" } js = { version = "0.1.0", path = "../js", default-features = false }
url = "2.5.4" url = "2.5.4"
urlencoding = "2.1.3" urlencoding = "2.1.3"
[features]
debug = ["js/debug"]
[dev-dependencies] [dev-dependencies]
boa_engine = "0.20.0" boa_engine = "0.20.0"
criterion = "0.5.1"
[[bench]]
name = "samples"
harness = false

View file

@ -1,61 +0,0 @@
use std::str::FromStr;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use js::{cfg::Config, rewrite};
use url::Url;
use urlencoding::encode;
pub fn bench(c: &mut Criterion) {
let discord = include_str!("../sample/discord.js");
let google = include_str!("../sample/google.js");
let url = Url::from_str("https://google.com/glorngle/si.js").expect("failed to make url");
let cfg = Config {
prefix: "/scrammedjet/".to_string(),
base: url.to_string(),
urlrewriter: Box::new(move |x: String| encode(url.join(&x).unwrap().as_str()).to_string()),
sourcetag: "glongle1".to_string(),
wrapfn: "$wrap".to_string(),
wrapthisfn: "$gwrap".to_string(),
importfn: "$import".to_string(),
rewritefn: "$rewrite".to_string(),
metafn: "$meta".to_string(),
setrealmfn: "$setrealm".to_string(),
pushsourcemapfn: "$pushsourcemap".to_string(),
capture_errors: true,
do_sourcemaps: true,
scramitize: false,
strict_rewrites: true,
};
c.bench_with_input(
BenchmarkId::new("rewrite/samples", "discord"),
&(discord, cfg.clone()),
|b, input| {
b.iter_batched(
|| input.clone(),
|x| rewrite(x.0, x.1),
BatchSize::SmallInput,
)
},
);
c.bench_with_input(
BenchmarkId::new("rewrite/samples", "google"),
&(google, cfg.clone()),
|b, input| {
b.iter_batched(
|| input.clone(),
|x| rewrite(x.0, x.1),
BatchSize::SmallInput,
)
},
);
}
criterion_group!(samples, bench);
criterion_main!(samples);

View file

@ -1,12 +1,17 @@
use std::{env, fs, str::FromStr, sync::Arc}; use std::{
env, fs,
str::FromStr,
sync::Arc,
time::{Duration, Instant},
};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use bytes::{Buf, Bytes, BytesMut}; use bytes::{Buf, Bytes, BytesMut};
use js::{cfg::Config, rewrite, RewriteResult};
use oxc::{ use oxc::{
allocator::{Allocator, String}, allocator::{Allocator, String},
diagnostics::NamedSource, diagnostics::NamedSource,
}; };
use js::{cfg::Config, rewrite, RewriteResult};
use url::Url; use url::Url;
use urlencoding::encode; use urlencoding::encode;
@ -18,9 +23,9 @@ fn dorewrite<'alloc>(alloc: &'alloc Allocator, data: &str) -> Result<RewriteResu
Config { Config {
prefix: "/scrammedjet/", prefix: "/scrammedjet/",
base: "https://google.com/glorngle/si.js", base: "https://google.com/glorngle/si.js",
urlrewriter: Box::new(move |x: &str, alloc: &'alloc Allocator| { urlrewriter: move |x: &str, alloc: &'alloc Allocator| {
String::from_str_in(encode(url.join(x).unwrap().as_str()).as_ref(), alloc) String::from_str_in(encode(url.join(x).unwrap().as_str()).as_ref(), alloc)
}), },
sourcetag: "glongle1", sourcetag: "glongle1",
@ -104,20 +109,31 @@ fn dounrewrite(res: &RewriteResult) -> Vec<u8> {
fn main() -> Result<()> { fn main() -> Result<()> {
let file = env::args().nth(1).unwrap_or_else(|| "test.js".to_string()); let file = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
let data = fs::read_to_string(file).context("failed to read file")?; let data = fs::read_to_string(file).context("failed to read file")?;
let bench = env::args().nth(2).is_some(); let bench = env::args().nth(2).map(|x| usize::from_str(&x));
let mut alloc = Allocator::default(); let mut alloc = Allocator::default();
if bench { if let Some(cnt) = bench.transpose().context("invalid bench size")? {
let mut i = 0; let mut duration = Duration::from_secs(0);
loop {
let cnt = cnt * 100;
for x in 1..=cnt {
let before = Instant::now();
let _ = dorewrite(&alloc, &data); let _ = dorewrite(&alloc, &data);
let after = Instant::now();
duration += after - before;
alloc.reset(); alloc.reset();
i += 1; if x % 100 == 0 {
if i % 100 == 0 { println!("{x}...");
println!("{i}...");
} }
} }
println!("iterations: {cnt}");
println!("total time: {duration:?}");
println!("avg time: {:?}", duration / cnt as u32);
} else { } else {
println!("orig:\n{data}"); println!("orig:\n{data}");

View file

@ -14,8 +14,8 @@ if ! [[ "$(wasm-bindgen -V)" =~ ^"$WBG" ]]; then
fi fi
if ! [ "${RELEASE:-0}" = "1" ]; then if ! [ "${RELEASE:-0}" = "1" ]; then
: "${WASMOPTFLAGS:=-g}" WASMOPTFLAGS="${WASMOPTFLAGS:-} -g"
: "${FEATURES:=debug}" FEATURES="debug,${FEATURES:-}"
else else
: "${WASMOPTFLAGS:=}" : "${WASMOPTFLAGS:=}"
: "${FEATURES:=}" : "${FEATURES:=}"