From 6b9371ed4a38cb7d54f41470f883e2a13b3f1ca1 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Thu, 13 Aug 2026 15:03:48 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=AD=97=E7=AC=A6=E4=B8=B2=20for-of=20?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=94=B9=E5=AD=97=E8=8A=82=E6=B8=B8=E6=A0=87?= =?UTF-8?q?=E5=A2=9E=E9=87=8F=E6=8E=A8=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_builtins/src/iterator.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/oxide_builtins/src/iterator.rs b/crates/oxide_builtins/src/iterator.rs index 8b9e3cef..bcc02c93 100644 --- a/crates/oxide_builtins/src/iterator.rs +++ b/crates/oxide_builtins/src/iterator.rs @@ -7,6 +7,8 @@ use oxide_runtime_api::{to_object, NativeResult, VmHost}; const INNER_PROP: &str = "__inner__"; const INDEX_PROP: &str = "__index__"; +/// 字符串迭代的字节游标(`next` 增量推进的当前位置),避免每步整串复制 + 从头重扫。 +const BYTEOFF_PROP: &str = "__byteoff__"; /// 占位构造函数:`Iterator` 不是构造函数,任何调用都抛 TypeError。 pub fn iterator_constructor(vm: &mut H, _args: &[u8]) -> NativeResult { @@ -304,9 +306,12 @@ fn next_array_like( if inner.is_string() { let index = current_index(vm, wrapper, index_si); - let source = oxide_runtime_api::to_string(inner); - let mut chars = source.chars(); - if let Some(ch) = chars.nth(index) { + let byteoff_si = vm.kernel_core().perm_interner().intern(BYTEOFF_PROP).0; + let byteoff = current_index(vm, wrapper, byteoff_si); + let source = unsafe { &*inner.as_string_ptr() }.as_str(); + let rest = &source[byteoff..]; + if let Some(ch) = rest.chars().next() { + vm.set_or_create_prop_value(wrapper, byteoff_si, JsValue::int((byteoff + ch.len_utf8()) as i32)); vm.set_or_create_prop_value(wrapper, index_si, JsValue::int((index + 1) as i32)); let value = vm.new_string(&ch.to_string()); return Ok(Some(make_iter_result(vm, value, false)));