14.3 流式区块
一个几 GB 的大地形,整块读进内存编辑器直接爆。本节的 StreamingStore 把大资产切成区块,按需加载:
register(id):登记一个可加载的块(空数据占位)。load(id):加载块,标记为最近使用(touch),必要时逐出最久未用的(LRU)。data(id)/loaded(id):读取块数据与判断是否已加载。cachedCount():当前缓存的块数。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 派生缓存 | 8.2 派生资产与缓存失效 | 派生缓存是「有界?」;本节是显式有界 + LRU |
| 后台调度 | 14.2 后台任务调度 | 区块加载是后台任务,别卡 UI |
| 异步导入 | 14.1 异步导入任务 | 大资产先异步导入再分块流式 |
| 运行时流式 | helio 卷三资源流式 | 本节的 LRU 是编辑器预演 |
为什么是 LRU:缓存有界、命中高
流式的核心矛盾:缓存不能无限大,但逐出要尽量命中。LRU(Least Recently Used)是最朴素又最有效的策略:
- 每加载一个块,把它放到「最近使用」列表尾部。
- 缓存超过上限,从头部(最久未用的)逐出。
- 重新加载某个块 = 把它移回尾部——常用块永远活着。
对比「先进先出(FIFO)」:FIFO 逐出最早加载的,哪怕那个块正在被高频使用;LRU 逐出最久没用的,贴近「正在看的地方」。编辑器里用户视角附近的区块要常驻,远处的一次性路过——LRU 正好满足。
注意 load 的语义:加载 = 有数据,占位(register 未 load)不算缓存。data 对未加载的块返回 null——流式读取方要处理「还没加载」,先显示占位再等后台加载完成刷新(14.2 的取消/并发在这里配合)。
对标空间流式与缓存淘汰
Unreal World Partition 使用流式来源和运行时网格加载区块,玩家控制器可以作为流式来源。helio 示例按区块标识访问数据,并按最近使用情况淘汰缓存。流式策略决定何时请求区块,缓存策略决定哪些已加载数据可以回收。实际宿主还需要定义正在使用的区块如何保留。 Unreal World Partition
可运行产物:6 条用例
下面 src/streaming-chunk.ts 层级 L1,完整可编译,命令 node --test test/14-3-streaming-chunk.test.ts:
// file: src/streaming-chunk.ts
// tier: L1
// verify: node --test test/14-3-streaming-chunk.test.ts
//
// 资产管线(第十四章):大资产与流式——流式区块。
// 一个几 GB 的大地形不能整块进内存:把它切成区块,按需加载、最多缓存
// 若干块,超出则逐出最久未用的(LRU)。StreamingStore 三件套:register
// 登记可加载块、load 加载并 touch 最近使用、LRU 逐出腾内存。这是运行时
// 流式(卷三/helio 卷三)在编辑器的预演。
export class StreamingStore {
readonly #chunks = new Map<string, { id: string; data: string }>();
readonly #lru: string[] = []; // 尾部最新
#maxCached: number;
constructor(maxCached = 4) {
this.#maxCached = maxCached;
}
/** 登记一个可加载的块(空数据占位)。 */
register(id: string): void {
if (!this.#chunks.has(id)) this.#chunks.set(id, { id, data: '' });
}
/** 是否已加载(有数据)。 */
loaded(id: string): boolean {
const c = this.#chunks.get(id);
return c !== undefined && c.data.length > 0;
}
/** 加载块:标记最近使用,必要时逐出最久未用的。 */
load(id: string): void {
const c = this.#chunks.get(id);
if (!c) return;
c.data = `data:${id}`;
this.#touch(id);
this.#evict();
}
/** 当前缓存的块数。 */
cachedCount(): number {
let n = 0;
for (const c of this.#chunks.values()) if (c.data.length > 0) n++;
return n;
}
/** 读取块数据;未加载返回 null。 */
data(id: string): string | null {
const c = this.#chunks.get(id);
return c && c.data.length > 0 ? c.data : null;
}
#touch(id: string): void {
const i = this.#lru.indexOf(id);
if (i >= 0) this.#lru.splice(i, 1);
this.#lru.push(id);
}
#evict(): void {
while (this.#lru.length > this.#maxCached) {
const old = this.#lru.shift()!;
const c = this.#chunks.get(old);
if (c) c.data = '';
}
}
}
#touch 把 id 从 LRU 列表挪到尾部(最近使用);#evict 在加载后收紧缓存——超出上限就从头逐出、把数据清空。逐出是「清数据」不是「删登记」:register 占位保留,下次 load 重新填数据——块的身份(register)和内容(load)分离。
可运行产物:6 条用例
下面是 test/14-3-streaming-chunk.test.ts 层级 L1,命令 node --test test/14-3-streaming-chunk.test.ts:
// file: test/14-3-streaming-chunk.test.ts
// tier: L1
// verify: node --test test/14-3-streaming-chunk.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { StreamingStore } from '../src/streaming-chunk.ts';
test('register + load 后可读', () => {
const s = new StreamingStore();
s.register('a');
assert.equal(s.loaded('a'), false);
s.load('a');
assert.equal(s.loaded('a'), true);
assert.equal(s.data('a'), 'data:a');
});
test('未加载 data → null', () => {
const s = new StreamingStore();
s.register('a');
assert.equal(s.data('a'), null);
});
test('未登记 load 忽略', () => {
const s = new StreamingStore();
s.load('ghost');
assert.equal(s.cachedCount(), 0);
assert.equal(s.data('ghost'), null);
});
test('LRU 逐出最久未用的块', () => {
const s = new StreamingStore(2);
s.register('a');
s.register('b');
s.register('c');
s.load('a');
s.load('b');
s.load('c'); // 逐出 a
assert.equal(s.data('a'), null);
assert.equal(s.data('b'), 'data:b');
assert.equal(s.data('c'), 'data:c');
assert.equal(s.cachedCount(), 2);
});
test('重新加载后不逐出(touch)', () => {
const s = new StreamingStore(2);
s.register('a');
s.register('b');
s.register('c');
s.load('a');
s.load('b');
s.load('a'); // 重新 touch a → LRU 序 [b, a]
s.load('c'); // 逐出 b
assert.equal(s.data('a'), 'data:a');
assert.equal(s.data('b'), null);
assert.equal(s.data('c'), 'data:c');
});
test('cachedCount 精确统计', () => {
const s = new StreamingStore(10);
s.register('a');
s.register('b');
s.load('a');
assert.equal(s.cachedCount(), 1);
s.load('b');
assert.equal(s.cachedCount(), 2);
});
本机实测(Node v26.5.0,node --test test/14-3-streaming-chunk.test.ts):tests 6、pass 6、fail 0,耗时约 40 ms。
第四条钉「LRU 逐出」——缓存上限 2,加载 c 时逐出最早加载的 a;第五条钉「touch 保活」——重新加载 a 让它「变新」,再加载 c 逐出的变成 b;第六条钉「计数精确」——cachedCount 只算有数据的块。
小结
- 流式区块:register 占位、load 加载、LRU 有界。
- LRU 逐出最久未用的,常用块永远活着——贴近「正在看的地方」。
- 逐出清数据不清登记:身份与内容分离,可再加载。
data返回 null 提醒读取方处理「还没加载」。- 运行时流式的编辑器预演;实时流式判据(距离/引用计数)留给卷三。
- 下一节把整卷收口:装配——一条可用的资产管线。
阅读导航:上一节:14.2 后台任务调度 · 下一节:15.1 管线装配全景 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。