perf: JsString 缓存 UTF-16 length,字符串 length 访问改 O(1)

This commit is contained in:
爱特每羊队
2026-08-10 16:13:24 +08:00
parent f284f74cb1
commit cd96fa6fcb
2 changed files with 27 additions and 3 deletions
+2 -2
View File
@@ -47,8 +47,8 @@ impl Vm {
if val.is_string() {
let length_si = self.kernel_core.perm_interner().intern("length").0;
if prop_name_si == length_si {
// SAFETY: val 是字符串值。
let len = unsafe { (*val.as_string_ptr()).data.encode_utf16().count() };
// SAFETY: val 是字符串值。utf16_len 构造时缓存,O(1) 读取。
let len = unsafe { (*val.as_string_ptr()).utf16_len() };
return Ok(Some(JsValue::int(len as i32)));
}
let proto_ptr = self.session.builtin_world().string_proto.as_ptr() as *mut JsObject;