From 5513f0e2fb8d94640e17c25993638f5c1c2e3368 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Thu, 13 Aug 2026 14:48:19 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20parse=5Fjs=5Fnumber=20=E5=8E=BB=20lower?= =?UTF-8?q?case=20=E5=88=86=E9=85=8D=20+=20int32=20=E4=BD=8D=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E7=9B=B4=E5=8F=96=EF=BC=88B2/B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_runtime_api/src/lib.rs | 21 +++++++++++---------- crates/oxide_vm/src/vm.rs | 6 ++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/crates/oxide_runtime_api/src/lib.rs b/crates/oxide_runtime_api/src/lib.rs index 66f52c9..3391649 100644 --- a/crates/oxide_runtime_api/src/lib.rs +++ b/crates/oxide_runtime_api/src/lib.rs @@ -245,15 +245,16 @@ fn parse_js_number(s: &str) -> f64 { if t.is_empty() { return 0.0; } - let lower = t.to_ascii_lowercase(); - if let Some(digits) = lower.strip_prefix("0x") { - return parse_radix_int(digits, 16); + let b = t.as_bytes(); + // 前缀探测按字节直接比较(ASCII 大小写不敏感),避免每次转换分配 lowercase。 + if b.len() >= 2 && b[0] == b'0' && (b[1] == b'x' || b[1] == b'X') { + return parse_radix_int(&t[2..], 16); } - if let Some(digits) = lower.strip_prefix("0o") { - return parse_radix_int(digits, 8); + if b.len() >= 2 && b[0] == b'0' && (b[1] == b'o' || b[1] == b'O') { + return parse_radix_int(&t[2..], 8); } - if let Some(digits) = lower.strip_prefix("0b") { - return parse_radix_int(digits, 2); + if b.len() >= 2 && b[0] == b'0' && (b[1] == b'b' || b[1] == b'B') { + return parse_radix_int(&t[2..], 2); } if t == "Infinity" || t == "+Infinity" { return f64::INFINITY; @@ -263,9 +264,9 @@ fn parse_js_number(s: &str) -> f64 { } // 十进制语法仅允许数字、符号、小数点与指数 e;含其它字符的令牌(如 // inf/nan 变体)不是合法 StringNumericLiteral,一律 NaN。 - if !t - .chars() - .all(|c| c.is_ascii_digit() || c == '+' || c == '-' || c == '.' || c == 'e' || c == 'E') + if !b + .iter() + .all(|&c| c.is_ascii_digit() || c == b'+' || c == b'-' || c == b'.' || c == b'e' || c == b'E') { return f64::NAN; } diff --git a/crates/oxide_vm/src/vm.rs b/crates/oxide_vm/src/vm.rs index 36d32d5..e04b046 100644 --- a/crates/oxide_vm/src/vm.rs +++ b/crates/oxide_vm/src/vm.rs @@ -505,6 +505,9 @@ impl Vm { } pub(crate) fn coerce_int32_bounded(&mut self, value: JsValue) -> Result { + if value.is_int() { + return Ok(value.as_int()); + } let n = self.coerce_number_bounded(value)?; if n == 0.0 || !n.is_finite() { return Ok(0); @@ -518,6 +521,9 @@ impl Vm { } pub(crate) fn coerce_uint32_bounded(&mut self, value: JsValue) -> Result { + if value.is_int() { + return Ok(value.as_int() as u32); + } let n = self.coerce_number_bounded(value)?; if n == 0.0 || !n.is_finite() { return Ok(0);