benches and generic infection

This commit is contained in:
Toshit Chawda 2024-12-11 14:52:59 -08:00
parent 2f7d2fa043
commit 633d0f17fc
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
11 changed files with 1113 additions and 204 deletions

320
rewriter/Cargo.lock generated
View file

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "ahash"
@ -14,12 +14,33 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "anyhow"
version = "1.0.94"
@ -88,7 +109,7 @@ dependencies = [
"icu_normalizer",
"indexmap",
"intrusive-collections",
"itertools",
"itertools 0.13.0",
"num-bigint",
"num-integer",
"num-traits",
@ -224,6 +245,12 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "castaway"
version = "0.2.3"
@ -239,6 +266,58 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838"
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]]
name = "compact_str"
version = "0.8.0"
@ -259,12 +338,73 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
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.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
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]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "dashmap"
version = "6.1.0"
@ -343,6 +483,16 @@ dependencies = [
"wasi",
]
[[package]]
name = "half"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888"
dependencies = [
"cfg-if",
"crunchy",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
@ -364,6 +514,12 @@ dependencies = [
"foldhash",
]
[[package]]
name = "hermit-abi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc"
[[package]]
name = "icu_collections"
version = "1.5.0"
@ -534,6 +690,26 @@ dependencies = [
"memoffset",
]
[[package]]
name = "is-terminal"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.13.0"
@ -608,6 +784,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"boa_engine",
"criterion",
"oxc",
"rewriter",
"url",
@ -691,6 +868,12 @@ version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "oorandom"
version = "11.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9"
[[package]]
name = "owo-colors"
version = "4.1.0"
@ -950,6 +1133,34 @@ dependencies = [
"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]]
name = "pollster"
version = "0.4.0"
@ -1034,6 +1245,26 @@ dependencies = [
"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]]
name = "redox_syscall"
version = "0.5.8"
@ -1043,6 +1274,35 @@ dependencies = [
"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]]
name = "regress"
version = "0.10.1"
@ -1087,6 +1347,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad97d4ce1560a5e27cec89519dc8300d1aa6035b099821261c651486a19e44d5"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
@ -1296,6 +1565,16 @@ dependencies = [
"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]]
name = "toml_datetime"
version = "0.6.8"
@ -1378,6 +1657,16 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[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]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
@ -1462,6 +1751,33 @@ dependencies = [
"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 0.59.0",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"

View file

@ -12,3 +12,8 @@ urlencoding = "2.1.3"
[dev-dependencies]
boa_engine = "0.20.0"
criterion = "0.5.1"
[[bench]]
name = "samples"
harness = false

View file

@ -0,0 +1,62 @@
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use rewriter::{cfg::Config, rewrite};
use std::str::FromStr;
use url::Url;
use urlencoding::encode;
fn encode_string(str: String) -> String {
encode(&str).to_string()
}
pub fn bench(c: &mut Criterion) {
let discord = include_str!("../sample/discord.js");
let google = include_str!("../sample/google.js");
let cfg = Config {
prefix: "/scrammedjet/".to_string(),
encoder: Box::new(encode_string),
base: Url::from_str("https://google.com/glorngle/si.js").expect("invalid base"),
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);

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 8.6 MiB

View file

@ -37,17 +37,24 @@ fn dorewrite(data: &str) -> Result<RewriteResult> {
.context("failed to rewrite file")
}
fn dobench(data: String) {
loop {
let _ = dorewrite(&data);
}
}
fn main() -> Result<()> {
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 bench = env::args().nth(2).is_some();
if bench {
loop {
dorewrite(&data)?;
for _ in 0..15 {
let data = data.clone();
std::thread::spawn(move || dobench(data));
}
}
dobench(data);
} else {
let res = dorewrite(&data)?;
let source = Arc::new(
@ -64,6 +71,7 @@ fn main() -> Result<()> {
"rewritten:\n{}",
String::from_utf8(res.js).context("failed to parse rewritten js")?
);
}
Ok(())
}

View file

@ -1,6 +1,11 @@
use url::Url;
pub struct Config {
#[derive(Clone)]
pub struct Config<E>
where
E: Fn(String) -> String,
E: Clone,
{
pub prefix: String,
pub sourcetag: String,
pub base: Url,
@ -13,12 +18,10 @@ pub struct Config {
pub metafn: String,
pub pushsourcemapfn: String,
pub encoder: EncodeFn,
pub encoder: E,
pub capture_errors: bool,
pub scramitize: bool,
pub do_sourcemaps: bool,
pub strict_rewrites: bool,
}
pub type EncodeFn = Box<dyn Fn(String) -> String>;

View file

@ -112,7 +112,11 @@ impl JsChange {
// returns (bunch of stuff to add before, option<bunch of stuff to add after>)
// bunch of stuff to add after should only be some if it's not a replace op
fn to_inner<'a>(&'a self, cfg: &'a Config) -> JsChangeInner<'a> {
fn to_inner<'a, E>(&'a self, cfg: &'a Config<E>) -> JsChangeInner<'a>
where
E: Fn(String) -> String,
E: Clone,
{
match self {
Self::WrapFn { ident, wrapped, .. } => JsChangeInner::Replace(if *wrapped {
smallvec!["(", cfg.wrapfn.as_str(), "(", ident.as_str(), ")", ")"]
@ -220,7 +224,11 @@ impl JsChanges {
self.inner.push(change);
}
pub fn perform(&mut self, js: &str, cfg: &Config) -> JsChangeResult {
pub fn perform<E>(&mut self, js: &str, cfg: &Config<E>) -> JsChangeResult
where
E: Fn(String) -> String,
E: Clone,
{
let mut offset = 0;
let mut buffer = Vec::with_capacity(((js.len() as u64 * 120) / 100) as usize);

View file

@ -28,7 +28,11 @@ pub struct RewriteResult {
pub changes: Vec<JsChange>,
}
pub fn rewrite(js: &str, config: Config) -> Result<RewriteResult, RewriterError> {
pub fn rewrite<E>(js: &str, config: Config<E>) -> Result<RewriteResult, RewriterError>
where
E: Fn(String) -> String,
E: Clone,
{
let allocator = Allocator::default();
let source_type = SourceType::default();
let ret = Parser::new(&allocator, js, source_type)

View file

@ -34,12 +34,20 @@ const UNSAFE_GLOBALS: &[&str] = &[
"frames",
];
pub struct Visitor {
pub struct Visitor<E>
where
E: Fn(String) -> String,
E: Clone,
{
pub jschanges: JsChanges,
pub config: Config,
pub config: Config<E>,
}
impl Visitor {
impl<E> Visitor<E>
where
E: Fn(String) -> String,
E: Clone,
{
fn rewrite_url(&mut self, url: String) -> String {
let url = self.config.base.join(&url).unwrap();
@ -80,7 +88,11 @@ impl Visitor {
}
}
impl<'a> Visit<'a> for Visitor {
impl<'a, E> Visit<'a> for Visitor<E>
where
E: Fn(String) -> String,
E: Clone,
{
fn visit_identifier_reference(&mut self, it: &IdentifierReference) {
// if self.config.capture_errors {
// self.jschanges.insert(JsChange::GenericChange {