feat: 环境变量开启 GC 压力模式,每顶层指令边界强制完整收集

This commit is contained in:
2026-10-06 20:20:57 +00:00
parent 107031dcc5
commit ba34c961ba
6 changed files with 99 additions and 24 deletions
+8 -5
View File
@@ -125,9 +125,10 @@ impl Vm {
// 返回顶层后检查恢复,存活值此时已回拷为执行根。账目未超水位时仅
// 少量字段比较。
if self.native_call_depth == 0 {
// 宿主 `$262.gc()` 置位的强制收集在此边界消费:完整收集
// 唯一安全点(无在途 dispatch 局部值),与正常触发路径同一入口。
if self.gc_state.pending_forced_collect {
// 宿主 `$262.gc()` 置位的强制收集与压力模式收集在此边界消费:
// 完整收集唯一安全点(无在途 dispatch 局部值),与正常触发路径
// 同一入口。压力模式开启后每个顶层边界做一次完整收集。
if self.gc_state.pending_forced_collect || self.gc_state.gc_pressure_mode {
self.gc_state.pending_forced_collect = false;
self.collect_session_gc();
}
@@ -141,13 +142,15 @@ impl Vm {
if alloc > self.gc_state.run_alloc_peak {
self.gc_state.run_alloc_peak = alloc;
}
if bytes >= self.gc_state.string_gc_watermark {
// 压力模式开启时跳过两档水位收集:完整收集已涵盖其全部工作,
// 同边界双触发只会多一轮空转 mark。
if bytes >= self.gc_state.string_gc_watermark && !self.gc_state.gc_pressure_mode {
self.maybe_collect_session_strings();
}
// 执行期原地 sweep 收集:O(1) 包络超触发水位 → 收集(无门控:
// 迭代器键引用经根收集标活,原地释放不搬移存活对象;
// 边界契约见 maybe_collect_in_run)。
if alloc >= self.gc_state.gc_watermark {
if alloc >= self.gc_state.gc_watermark && !self.gc_state.gc_pressure_mode {
self.maybe_collect_in_run();
}
}
+4
View File
@@ -57,6 +57,10 @@ pub(crate) struct GcState {
/// 宿主强制收集旗标(`$262.gc()`):native 重入中置位,由下一个顶层
/// 指令边界(完整收集的唯一安全点:无在途 builtin 局部裸指针)消费。
pub(crate) pending_forced_collect: bool,
/// GC 压力模式开关(构造时读环境变量 `OXIDE_GC_PRESSURE`,存在即开):
/// 开启后每个顶层指令边界做一次完整收集,与强制收集共用同一安全点入口,
/// 同时跳过两个水位触发的档(完整收集已涵盖其全部工作)。
pub(crate) gc_pressure_mode: bool,
}
impl GcState {
+11
View File
@@ -15,6 +15,15 @@ use oxide_types::mem::P;
use oxide_types::object::{JsObject, JsString, PropAttributes};
use oxide_types::value::JsValue;
/// 读 GC 压力模式开关:环境变量 `OXIDE_GC_PRESSURE` 存在即开启(取值不解析)。
///
/// 每次 VM 构造读一次,不做进程级缓存——缓存会在测试二进制内先构造的 VM 处
/// 锁死取值,后跑的测试再设环境变量不生效。VM 构造不是热路径(每测试/每池/
/// 每进程一次),读环境变量的开销可忽略。
fn gc_pressure_mode_from_env() -> bool {
std::env::var_os("OXIDE_GC_PRESSURE").is_some()
}
impl Vm {
/// Cons(rope)节点保留的单元阈值:拼接总长(UTF-16 单元数)≤ 该值时
/// 急切扁平。小链的"链接 + 首次消费扁平化"双重分配高于直接拷贝,直接
@@ -138,6 +147,7 @@ impl Vm {
gc_threshold_cached: gc_threshold,
gc_watermark: gc_threshold,
pending_forced_collect: false,
gc_pressure_mode: gc_pressure_mode_from_env(),
},
symbols: SymbolState {
symbol_counter: 0,
@@ -282,6 +292,7 @@ impl Vm {
gc_threshold_cached: gc_threshold,
gc_watermark: gc_threshold,
pending_forced_collect: false,
gc_pressure_mode: gc_pressure_mode_from_env(),
},
symbols: SymbolState {
symbol_counter: 0,