refactor: 扁平化对象属性存储并优化常量池与 IC 热路径

This commit is contained in:
爱特每羊队
2026-06-13 23:59:47 +08:00
parent dcc7db2575
commit bf74adf7f5
26 changed files with 307 additions and 87 deletions
+2 -2
View File
@@ -118,12 +118,12 @@ pub fn error_to_string(vm: &mut Vm, args: &[u8]) -> NativeResult {
let name_val = obj
.hash_props_vec()
.and_then(|v| v.first())
.map(|b| **b)
.copied()
.unwrap_or(JsValue::undefined());
let msg_val = obj
.hash_props_vec()
.and_then(|v| v.get(1))
.map(|b| **b)
.copied()
.unwrap_or(JsValue::undefined());
(
vm.lookup_str(name_val).unwrap_or_else(|| "Error".to_string()),
+5 -5
View File
@@ -19,11 +19,11 @@ fn bind_dispatcher(vm: &mut Vm, args: &[u8]) -> NativeResult {
let wrapper = unsafe { &*wrapper_val.as_js_object_ptr() };
let bound_target = wrapper
.hash_props_vec()
.and_then(|v| v.first().map(|b| **b))
.and_then(|v| v.first().copied())
.unwrap_or(JsValue::undefined());
let bound_this = wrapper
.hash_props_vec()
.and_then(|v| v.get(1).map(|b| **b))
.and_then(|v| v.get(1).copied())
.unwrap_or(JsValue::undefined());
// Forward bound call arguments (skip args[0], the bound-wrapper receiver).
@@ -63,7 +63,7 @@ pub fn function_apply(vm: &mut Vm, args: &[u8]) -> NativeResult {
for i in 0..n {
let vec = arr.hash_props_vec();
if let Some(v) = vec.and_then(|v| v.get(i)) {
vm.set_reg(base + i as u8, **v);
vm.set_reg(base + i as u8, *v);
}
}
} else {
@@ -97,8 +97,8 @@ pub fn function_bind(vm: &mut Vm, args: &[u8]) -> NativeResult {
// SAFETY: bind_dispatcher is a NativeFn fn-item; valid to store as NativeFnPtr.
(*wrapper).set_native_fn(Some(NativeFnPtr::from_raw(bind_dispatcher as *const ())));
(*wrapper).set_native_arg_count(0);
(*wrapper).ensure_hash_props().push(Box::new(target_val));
(*wrapper).ensure_hash_props().push(Box::new(bound_this));
(*wrapper).ensure_hash_props().push(target_val);
(*wrapper).ensure_hash_props().push(bound_this);
}
NativeResult::Ok(JsValue::from_js_object(wrapper))
+1 -1
View File
@@ -70,7 +70,7 @@ fn value_to_jsvalue(vm: &mut Vm, val: &serde_json::Value) -> JsValue {
let jsv = value_to_jsvalue(vm, val);
let new_shape = vm.kernel().shape_forge().make_shape(obj.shape_id(), si);
obj.set_shape_id(new_shape);
obj.ensure_hash_props().push(Box::new(jsv));
obj.ensure_hash_props().push(jsv);
}
let obj_ptr = vm.epoch().alloc(obj);
JsValue::from_js_object(obj_ptr)
+6 -6
View File
@@ -43,13 +43,13 @@ fn set_prop(obj: &mut JsObject, name: &str, val: JsValue, vm: &Vm) {
let si = vm.kernel().string_forge().intern(name).0;
let shape_id = vm.kernel().shape_forge().make_shape(obj.shape_id(), si);
obj.set_shape_id(shape_id);
obj.ensure_hash_props().push(Box::new(val));
obj.ensure_hash_props().push(val);
}
fn get_prop(obj: &JsObject, idx: usize) -> JsValue {
obj.hash_props_vec()
.and_then(|v| v.get(idx))
.map(|b| **b)
.copied()
.unwrap_or(JsValue::undefined())
}
@@ -57,9 +57,9 @@ fn set_prop_at(obj: *mut JsObject, idx: usize, val: JsValue) {
unsafe {
let re = &mut *obj;
if let Some(vec) = re.hash_props_vec() {
let ptr = vec.as_ptr() as *mut Box<JsValue>;
let ptr = vec.as_ptr() as *mut JsValue;
let v = &mut *ptr.add(idx);
**v = val;
*v = val;
}
}
}
@@ -201,14 +201,14 @@ pub fn regexp_exec(vm: &mut Vm, args: &[u8]) -> NativeResult {
let full_match = &haystack[m.start()..m.end()];
let full_si = vm.kernel().string_forge().intern(full_match).0;
match_obj.ensure_hash_props().push(Box::new(JsValue::string(full_si, 0)));
match_obj.ensure_hash_props().push(JsValue::string(full_si, 0));
let captures = regex.captures(&haystack[m.start()..m.end()]);
if let Some(caps) = &captures {
for i in 1..caps.len() {
let cap_str = caps.get(i).map(|cm| cm.as_str()).unwrap_or("");
let si = vm.kernel().string_forge().intern(cap_str).0;
match_obj.ensure_hash_props().push(Box::new(JsValue::string(si, 0)));
match_obj.ensure_hash_props().push(JsValue::string(si, 0));
}
}