mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-15 15:30:00 -04:00
squeeze the last ms of performance out of the rewriter
This commit is contained in:
parent
213f7bfa2b
commit
41acba634d
5 changed files with 31 additions and 152 deletions
|
@ -1,7 +1,8 @@
|
|||
pub mod rewrite;
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::str::{from_utf8, FromStr};
|
||||
|
||||
use js_sys::Uint8Array;
|
||||
use rewrite::rewrite;
|
||||
use url::Url;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
@ -13,12 +14,12 @@ extern "C" {
|
|||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js(js: &str, url: &str) -> String {
|
||||
pub fn rewrite_js(js: &str, url: &str) -> Vec<u8> {
|
||||
rewrite(js, Url::from_str(url).unwrap())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str) -> String {
|
||||
pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str) -> Vec<u8> {
|
||||
// technically slower than the c++ string conversion but it will create *less copies*
|
||||
|
||||
let js = unsafe { std::str::from_utf8_unchecked(js) };
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
#![allow(clippy::print_stdout)]
|
||||
use std::{env, path::Path, str::FromStr};
|
||||
use std::{
|
||||
env,
|
||||
path::Path,
|
||||
str::{from_utf8, FromStr},
|
||||
};
|
||||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::{
|
||||
|
@ -28,10 +32,15 @@ fn main() -> std::io::Result<()> {
|
|||
|
||||
println!(
|
||||
"{}",
|
||||
rewrite(
|
||||
&source_text,
|
||||
Url::from_str("https://google.com/glorngle/si.js").unwrap()
|
||||
from_utf8(
|
||||
rewrite(
|
||||
&source_text,
|
||||
Url::from_str("https://google.com/glorngle/si.js").unwrap()
|
||||
)
|
||||
.as_slice()
|
||||
)
|
||||
.unwrap()
|
||||
.to_string()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -184,7 +184,7 @@ impl Rewriter {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn rewrite(js: &str, url: Url) -> String {
|
||||
pub fn rewrite(js: &str, url: Url) -> Vec<u8> {
|
||||
let allocator = Allocator::default();
|
||||
let source_type = SourceType::default();
|
||||
let ret = Parser::new(&allocator, &js, source_type).parse();
|
||||
|
@ -241,10 +241,8 @@ pub fn rewrite(js: &str, url: Url) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
let mut buffer = String::new();
|
||||
// pre-allocate the space we need. should make copies faster
|
||||
let size_estimate = (original_len as i32 + difference) as usize;
|
||||
buffer.reserve(size_estimate);
|
||||
let mut buffer: Vec<u8> = Vec::with_capacity(size_estimate);
|
||||
|
||||
let mut offset = 0;
|
||||
for change in ast_pass.jschanges {
|
||||
|
@ -254,8 +252,9 @@ pub fn rewrite(js: &str, url: Url) -> String {
|
|||
let start = span.start as usize;
|
||||
let end = span.end as usize;
|
||||
|
||||
buffer.push_str(&js[offset..start]);
|
||||
buffer.push_str(&text);
|
||||
buffer.extend_from_slice(unsafe { js.slice_unchecked(offset, start) }.as_bytes());
|
||||
|
||||
buffer.extend_from_slice(text.as_bytes());
|
||||
offset = end;
|
||||
|
||||
// offset = (offset as i64 + (text.len() as i64 - len as i64)) as usize;
|
||||
|
@ -281,7 +280,7 @@ pub fn rewrite(js: &str, url: Url) -> String {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
buffer.push_str(&js[offset..]);
|
||||
buffer.extend_from_slice(js[offset..].as_bytes());
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue