15.2 从 helio 到工业级编辑器的路线图
里程碑带工期与依赖。排序用拓扑序;串行总周数是所有工期之和,关键路径是最长依赖链的工期。两个数字放在一起,才能说明并行能省多少、省不掉多少。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 能力全景 | 本卷 15.1 | 15.1 找缺口;本节把缺口排成可执行的里程碑 |
| 扩展框架 | 本卷 1.1 | 1.1 的拓扑排序用于扩展;本节用于项目计划 |
| 全卷脉络 | 本卷目录 | 本节承接系列收口章 |
从零实现:从 helio 到工业级编辑器的路线图
先按依赖做拓扑排序,同时在遍历路径里检测环路与缺失依赖。再按拓扑序计算每个里程碑的最早完成周数:无依赖从零开始,有依赖取依赖完成时间的最大值再加自身工期。串行总周数直接求和,关键路径取所有最早完成时间的最大值。
关键路径的周数假设资源充分且任务可在依赖完成后立即启动。共享人员、设备和外部审批会延长实际工期。本例输出关键路径的时长。Unreal BuildGraph 提供带依赖的构建自动化图,可对照任务建模。 Epic BuildGraph 文档
flowchart TD
A["输入里程碑、工期和依赖"] --> B["校验 ID、依赖与环路"]
B --> C["按拓扑序计算最早完成时间"]
C --> D["取最大值作为关键路径时长"]
对标 Unity / Unreal / Godot / Cocos:编辑器路线图
| 引擎 | 机制 | 取舍 |
|---|---|---|
| Unity | 产品路线图 | 按版本与平台推进 |
| Unreal | 产品路线图 | 大版本节奏,工具链持续扩展 |
| Godot | 开源路线图 | 按里程碑与提案推进 |
| Cocos Creator | 版本计划 | 按平台与工作流迭代 |
| helio(本节) | plan | 拓扑序 + 串行总和 + 关键路径 |
可运行产物:6 条用例
下面是 src/roadmap.ts,层级 L1,完整可编译。运行命令 node --test test/15-2-helio-to-industrial-roadmap.test.ts:
// file: src/roadmap.ts
// tier: L1
// verify: node --test test/15-2-helio-to-industrial-roadmap.test.ts
//
// 卷四(第十五章):从 helio 到工业级编辑器的路线图。
// 里程碑带工期与依赖。排序用拓扑序;串行总周数是所有工期之和(一个人做
// 的顺序),关键路径是最长依赖链的工期(并行后至少还要这么多周)。两个
// 数字放在一起,才能说明并行能省多少、省不掉多少。
export interface Milestone {
id: string;
weeks: number;
deps: string[];
}
export interface Roadmap {
order: string[];
totalWeeks: number;
criticalPathWeeks: number;
}
export function plan(milestones: Milestone[]): Roadmap {
const byId = new Map(milestones.map((m) => [m.id, m]));
if (byId.size !== milestones.length) throw new Error("Duplicate milestone ID");
const order: string[] = [];
const visited = new Set<string>();
const visit = (id: string, path: string[]): void => {
if (visited.has(id)) return;
if (path.includes(id)) throw new Error(`循环依赖:${id}`);
const milestone = byId.get(id);
if (!milestone) throw new Error(`缺少依赖里程碑 ${id}`);
for (const dep of milestone.deps) visit(dep, [...path, id]);
visited.add(id);
order.push(id);
};
for (const milestone of milestones) visit(milestone.id, []);
const earliest = new Map<string, number>();
for (const id of order) {
const milestone = byId.get(id);
if (!milestone) continue;
const base = milestone.deps.length === 0
? 0
: Math.max(...milestone.deps.map((dep) => earliest.get(dep) ?? 0));
earliest.set(id, base + milestone.weeks);
}
const totalWeeks = milestones.reduce((acc, m) => acc + m.weeks, 0);
const criticalPathWeeks = Math.max(0, ...earliest.values());
return { order, totalWeeks, criticalPathWeeks };
}
串行总周数与关键路径的差值就是并行能压缩的上限:例如串行 10 周、关键路径 7 周,说明理论上最多省 3 周。缺失依赖与环路在计划阶段就报错,比排到一半才发现某个里程碑永远无法开始要好。
下面是 test/15-2-helio-to-industrial-roadmap.test.ts,层级 L1:
// file: test/15-2-helio-to-industrial-roadmap.test.ts
// tier: L1
// verify: node --test test/15-2-helio-to-industrial-roadmap.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { plan } from '../src/roadmap.ts';
const ms = (id: string, weeks: number, deps: string[] = []) => ({ id, weeks, deps });
test('按依赖拓扑排序', () => {
const r = plan([ms('c', 1, ['b']), ms('b', 2, ['a']), ms('a', 3)]);
assert.deepEqual(r.order, ['a', 'b', 'c']);
});
test('串行总周数为工期之和', () => {
const r = plan([ms('a', 2), ms('b', 3), ms('c', 4)]);
assert.equal(r.totalWeeks, 9);
});
test('关键路径取最长依赖链', () => {
const r = plan([
ms('a', 2),
ms('b', 3, ['a']),
ms('c', 4, ['a']),
ms('d', 1, ['b', 'c']),
]);
assert.equal(r.criticalPathWeeks, 7);
assert.equal(r.totalWeeks, 10);
});
test('缺失依赖报错', () => {
assert.throws(() => plan([ms('a', 1, ['ghost'])]), /缺少依赖里程碑 ghost/);
});
test('空路线图全为零', () => {
assert.deepEqual(plan([]), { order: [], totalWeeks: 0, criticalPathWeeks: 0 });
});
test('单里程碑关键路径等于其工期', () => {
const r = plan([ms('only', 6)]);
assert.deepEqual(r.order, ['only']);
assert.equal(r.criticalPathWeeks, 6);
});
本机实测(Node v26.5.0,node --test test/15-2-helio-to-industrial-roadmap.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉拓扑序与串行总和;第三条钉关键路径;第四条钉缺失依赖;后两条钉空图与单里程碑。
小结
- 路线图的两个数字:串行总周数与关键路径。
- 两者之差是并行能压缩的理论上限。
- 关键路径是各里程碑最早完成时间的最大值。
- 缺失依赖与环路在计划阶段就要报错。
阅读导航:上一节:15.1 工业级编辑器全景回顾 · 下一节:15.3 下一步:读源码与自研 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。