Files
Endoleap/src/lib/canvas-runtime/import-batches.ts
T

29 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 导入runner内的自适应批大小反馈,不持久化进job,也不改变记录/ID或依赖规则。
* 从一条开始试探,超时缩小、满批且便宜才增长;每次原子发布仍可能有不可抢占固定成本。
*/
/** Runner-owned feedback, not persisted job state. Probe bounded batches to
* distinguish fixed publication cost from the marginal cost of another unit. */
/** runner 提供已验证的正 budget/max,并在尝试后反馈 units/elapsed;本函数不自行读取时钟或发布内容。
* 闭包只保留 limit 和 observed,重启 runner 会重新从 1 探测,不用持久化这些性能估计。
* 参数/样本未在此验证,调用方需传有限且合理的耗时/单位;它不是容量或源完整性校验器。 */
export function createImportBatches(budget:number,max:number){
// 初始未观测时保守为1;observed只表示收到反馈,不是源校验或导入已完成。
let limit=1,observed=false;
return Object.freeze({
/** 下次组选择的单位软预算,依赖SCC仍可能需要一次超过它。 */
get limit(){return limit;},
/** 是否已有至少一次发布成本样本,用于runner的调度决策。 */
get observed(){return observed;},
/** 首次反馈即 observed=true;elapsed > budget 按比例缩小,且尽量至少减 1,最低保留 1。
* 未超时且 units ≥ limit 才按约 1.5 倍增长并封顶 max;等于 budget 也算此分支。
* 不足当前 limit 的尾批不增长,避免将少量记录的固定发布成本误当成更大批仍便宜。 */
record(units:number,elapsed:number){
observed=true;
if(elapsed>budget){limit=Math.max(1,Math.min(limit-1,Math.floor(units*budget/elapsed)));return;}
// A short tail/dependency group does not prove that a larger batch is cheap.
if(units>=limit)limit=Math.min(max,limit+Math.max(1,Math.floor(limit/2)));
},
});
}