mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 19:21:11 +08:00
perf: 移除 JsString 死哈希字段,消除每次字符串创建的 O(n) FxHash 开销
This commit is contained in:
@@ -147,7 +147,7 @@ pub fn string_value_eq(a: JsValue, b: JsValue) -> bool {
|
|||||||
}
|
}
|
||||||
let sa = unsafe { &*a.as_string_ptr() };
|
let sa = unsafe { &*a.as_string_ptr() };
|
||||||
let sb = unsafe { &*b.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 {
|
pub fn to_number(val: JsValue) -> f64 {
|
||||||
|
|||||||
@@ -1,27 +1,17 @@
|
|||||||
use std::hash::{Hash, Hasher};
|
|
||||||
|
|
||||||
use rustc_hash::FxHasher;
|
|
||||||
|
|
||||||
use crate::value::JsValue;
|
use crate::value::JsValue;
|
||||||
|
|
||||||
/// Heap-allocated JS string value.
|
/// Heap-allocated JS string value.
|
||||||
///
|
///
|
||||||
/// String *values* are NaN-boxed as 48-bit pointers to a `JsString` (see
|
/// String *values* are NaN-boxed as 48-bit pointers to a `JsString` (see
|
||||||
/// `JsValue::string`), replacing the old interner `(index, hash16)` pair. `hash`
|
/// `JsValue::string`).
|
||||||
/// is a full 64-bit FxHash computed once at construction and used as a fast
|
|
||||||
/// inequality reject before content comparison.
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct JsString {
|
pub struct JsString {
|
||||||
pub data: String,
|
pub data: String,
|
||||||
pub hash: u64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JsString {
|
impl JsString {
|
||||||
pub fn new(data: String) -> Self {
|
pub fn new(data: String) -> Self {
|
||||||
let mut h = FxHasher::default();
|
Self { data }
|
||||||
data.hash(&mut h);
|
|
||||||
let hash = h.finish();
|
|
||||||
Self { data, hash }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
@@ -484,7 +474,9 @@ impl JsObject {
|
|||||||
|
|
||||||
pub fn set_upvalues(&mut self, v: Box<Vec<*mut Cell>>) {
|
pub fn set_upvalues(&mut self, v: Box<Vec<*mut Cell>>) {
|
||||||
if !self.upvalues.is_null() {
|
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;
|
self.upvalues = Box::into_raw(v) as *mut u8;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user