perf: 移除 JsString 死哈希字段,消除每次字符串创建的 O(n) FxHash 开销

This commit is contained in:
爱特每羊队
2026-06-30 20:04:37 +08:00
parent 429cb6d9de
commit 2d8e5d861d
2 changed files with 6 additions and 14 deletions
+1 -1
View File
@@ -147,7 +147,7 @@ pub fn string_value_eq(a: JsValue, b: JsValue) -> bool {
}
let sa = unsafe { &*a.as_string_ptr() };
let sb = unsafe { &*b.as_string_ptr() };
sa.hash == sb.hash && sa.data == sb.data
sa.data == sb.data
}
pub fn to_number(val: JsValue) -> f64 {
+5 -13
View File
@@ -1,27 +1,17 @@
use std::hash::{Hash, Hasher};
use rustc_hash::FxHasher;
use crate::value::JsValue;
/// Heap-allocated JS string value.
///
/// String *values* are NaN-boxed as 48-bit pointers to a `JsString` (see
/// `JsValue::string`), replacing the old interner `(index, hash16)` pair. `hash`
/// is a full 64-bit FxHash computed once at construction and used as a fast
/// inequality reject before content comparison.
/// `JsValue::string`).
#[derive(Debug)]
pub struct JsString {
pub data: String,
pub hash: u64,
}
impl JsString {
pub fn new(data: String) -> Self {
let mut h = FxHasher::default();
data.hash(&mut h);
let hash = h.finish();
Self { data, hash }
Self { data }
}
pub fn len(&self) -> usize {
@@ -484,7 +474,9 @@ impl JsObject {
pub fn set_upvalues(&mut self, v: Box<Vec<*mut Cell>>) {
if !self.upvalues.is_null() {
unsafe { drop(Box::from_raw(self.upvalues as *mut Vec<*mut Cell>)); }
unsafe {
drop(Box::from_raw(self.upvalues as *mut Vec<*mut Cell>));
}
}
self.upvalues = Box::into_raw(v) as *mut u8;
}