一帧之内:因果链与同步点 Within One Frame: Causal Chains and Sync Points
为什么处理器之间必须有顺序,以及一条因果链怎样在一帧里跑完 · 2026-11-22Why processors need an order, and how a causal chain still completes inside one frame · 2026-11-22
系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →
对象世界里,链条是连着的 In an Object World the Chain Is Continuous
OOP 里写战斗,顺序是触发式的:A 做完自己的事,调函数通知 B;B 接着做,再通知 C。整条链在一帧里从头走到尾,一环都不会漏。下一环是上一环直接叫起来的,因果关系就写在调用栈上。
Combat in an object world is trigger-driven. A does its thing and calls into B; B does its part and calls into C. The chain runs top to bottom inside the frame and nothing goes missing, because the next link is invoked by the previous one. Causality lives in the call stack.
所以在那一边,顺序几乎算不上设计问题。调用别写反,链自己就是顺的,当帧走完。
Over there, ordering is barely a design question. Keep the calls in the right order and the chain stays continuous and finishes within the frame.
ECS 里没有"触发" An ECS Has No Trigger
ECS 不是这样。这里没有谁通知谁:一个处理器把同一件事在整批实体上做完,下一个处理器再做另一件事。它们互不知道对方存在,只知道自己要查哪批实体、要读写哪些片段。
An ECS has no notifying at all. One processor does one job across the whole batch, then the next does a different job. They do not know each other exists; each knows the entities its query matches and the fragments it reads and writes.
没有调用链,才有连续的内存、同一批实体、一趟并行,这也是它快的原因。代价是顺序不再由代码结构保证,得由你安排。写错了没有任何报错,只是算出一个错的结果。
No call chain is what buys the speed: contiguous memory, one batch, one parallel pass. The price is that the order is no longer carried by the shape of the code; you arrange it yourself. Get it wrong and nothing complains, you just get a wrong answer.
可是因果是真的 But Causality Is Real
换了存储方式,战斗里那些先后关系一个都没消失:
The orderings in combat do not go away because the storage changed:
| 链条The chain | 为什么不能并Why it cannot be one step |
|---|---|
| 攻击 → 受击 → 死亡 → 移除attack → hit → death → removal | 受击读这次攻击算出的伤害,死亡读这一帧累积的伤害,移除读"这一帧刚死"的标记The hit reads the damage this attack just produced, death reads what accumulated this frame, removal reads the "died this frame" flag |
| 攻击 → 生成投射物 → 投射物检索与命中 → 施加伤害attack → spawn projectile → projectile traces and hits → damage lands | 投射物得先存在,下一段逻辑才有得处理;而"存在"本身要一次生成解析The projectile has to exist before the next stage can touch it, and existing takes a spawn resolution |
| 移动 → 物理 → 挂载解算move → physics → mount resolve | 子体的坐标系要拿父级移动并解算完之后的变换来建,早一步就是上一帧的位置A child's frame is built from the parent's post-move, post-solve transform. One step early and it is last frame's position. |
所以"处理器之间不需要顺序"这话是错的。真正的问题在别处:顺序摆在那里,怎么让整条链在一帧里跑完。
So "processors do not need an order" is simply wrong. The real question is: the order exists, so how do you make it all happen inside one frame?
最省事的做法,是每环慢一帧 The Easy Way Costs One Frame per Link
让每个处理器只读上一帧写下的数据,一切都简单了:不用排顺序,不用担心并行冲突,谁也不等谁。代价是链上每一环都慢一帧。攻击这一帧判定,受击下一帧,死亡再下一帧,移除再一帧。
Let every processor read only what was written last frame and life gets easy: no ordering to arrange, no parallel conflicts, nobody waits on anybody. The price is that every link in the chain is a frame late. The attack resolves this frame, the hit next frame, death the frame after, removal one more.
有些东西慢一帧看不出来,有几样却刺眼得很。最典型的是子弹:这一帧扣扳机,你就想这一帧看见它飞出去。慢一帧,单位已经往前走了半个身位,子弹却从它原地冒出来;再慢一两帧,原地可能站着别人,看上去就是凭空冒出一颗子弹。这是穿帮,和性能没关系。
Some of that lag nobody sees. A few things are impossible to miss. The clearest is a bullet: pull the trigger this frame and it should leave the barrel this frame. One frame late and the unit has already moved half a body length while the bullet appears where it used to stand; two frames late and someone else may be standing there, so the bullet seems to come out of thin air. That is a visible break, and performance has nothing to do with it.
生成物把这件事放大好几倍。一次攻击可能生成投射物、命中特效、掉落、尸体,这些全是"这一帧该被人看见"的东西。所以延迟得压进同一帧内,光让它看着不那么明显没有用。
Spawning turns the volume up. One attack can produce a projectile, an impact effect, loot and a corpse, and all of them are part of "what this frame is supposed to look like". So the latency has to come down to inside one frame. Making it less noticeable is not a fix.
一趟并行波,就是一个同步点 One Parallel Wave Is One Sync Point
解决这件事的机制我们本来就有,只是平时不这么叫它:一个处理器的一次执行,就是一趟并行波。波里面同步不了,几千个实体同时在跑,谁也停不下来等谁;波一结束就同步了,所有任务收工,这一趟写的字段对下一趟都可见。
The mechanism that solves it is one we already had; we just do not usually call it that. One execution of a processor is one parallel wave. Inside a wave you cannot synchronise: thousands of entities are running and nothing can stop to wait. The end of a wave is a sync point. Every task has finished, and every field written in that wave is visible to the next one.
所以处理器之间的顺序由波的边界保证。我把 Behavior 排在 Projectile 前面,靠的就是它们分属两趟波:前一趟全部写完,后一趟才开始读。这里面没有"调用"这回事。
Order between processors is carried by wave boundaries, not by a call stack. Behaviour comes before Projectile because they are two waves: the first finishes writing before the second starts reading. There is no call anywhere in it.
这和"模拟阶段"是同一回事,只是粒度更细。阶段 = 一组波,阶段边界 = 一个更强的同步点。它除了等线程收工,还做一件波尾做不到的事:把这一组里排队的生成、销毁、命令全部落地。这一条很关键,下一节展开。
This is the same thing as a simulation stage at a finer grain. A stage is a group of waves, and a stage boundary is a stronger sync point. Besides waiting for the threads, it does one thing a wave end cannot: it lands the spawns, destroys and queued commands accumulated in that group. That distinction is what the next section is about.
切分点落在哪 Where the Cuts Fall
如果只为了数据可见,一趟波结束就够了,不用阶段。要阶段的是生成物:新实体在生成解析之前根本不存在,下一趟波再怎么查也查不到。所以切分点正好落在"这一段要把产出当实体交给下一段"的地方。
If visibility of data were the only concern, a wave end would be enough and stages would have nothing to do. What needs a stage is spawning: a new entity does not exist until the spawn resolves, so the next wave cannot find it however it queries. So the cuts land exactly where one section has to hand its output to the next as entities.
把这条判据试一遍,逼你切一刀的只有三种情况:
Put that test to work on a combat loop and only three things force a cut:
| 迫使切一刀的情况What forces a cut | 例子Example |
|---|---|
| 下一段要处理的,正是这一段刚生成出来的实体The next section processes entities this one just spawned | 攻击生成投射物 → 处理它的飞行、检索、命中。它必须看见"这一帧刚出生"的东西An attack spawns a projectile → its flight, trace and hit. It has to see things born this frame |
| 两段只交换数据,都不涉及生成Two sections only exchange data, neither spawns | 移动 → 物理 → 通知:同段串行,按注册顺序走就行,不需要额外边界Move → physics → notify: same stage, registration order is enough |
| 这一段要读别人算完之后的结果A section reads what another has finished computing | 挂载解算要的是父级移动、物理之后的变换,所以它排最后,独占这一段The mount resolve needs the parent's post-move, post-physics transform, so it runs last and alone |
另有一处和分段没关系,代价却一样:网络哈希不能采在中间时刻。我们单独派发它,等所有 post-sim 的队列变异(事件队列、回收、销毁、命令冲刷)做完才采样,采到的才是真正的帧末状态。
One more spot has nothing to do with the cut and costs the same: the network hash must not be sampled mid-frame. We dispatch it on its own, after every post-sim queue mutation — event queues, recycling, destruction, command flush — so what it samples is the true end of the frame.
至于我们把处理器切成几段、每段装哪几个,那是实现细节。注册处(子系统的初始化)一眼能看到,而且随插件源码发布。这篇只留判据。
How many stages we cut and which processors go in each is an implementation detail. The registration site, in the subsystem's initialisation, shows it at a glance, and it ships with the plugin source. This post keeps to the test.
阶段边界到底买到了什么 What a Stage Boundary Actually Buys
这两件事容易混,拆开看就清楚了:数据可见性是波尾给的,实体可见性是阶段边界给的。Move→Physics→Notify 能同段串行,因为它们只交换数据;Behavior→Projectile 必须分段,因为 Projectile 要处理的是刚刚生成出来的实体。
The two are easy to mix up. Separated, they are simple: wave ends give data visibility, stage boundaries give entity visibility. Move → Physics → Notify share a stage because they only exchange data. Behaviour → Projectile cannot, because what Projectile processes are entities that were just created.
所以判断要不要在这里切一刀,问一句就够:下一段要的东西里,有没有上一段刚生出来的实体?有,就切;没有,那就是两趟波,不需要额外的阶段。
So when you wonder whether to cut here, ask: does the next section need anything that did not exist as an entity before this one ran? Yes means cut. No means two waves and no stage.
顺序本身是契约:管线在启动时检查注册顺序,几条 checkf 钉死了"物理在移动之后""notify 在物理之后""Host 最后且独占"。写反了不会给你一个奇怪的结果,启动直接崩。这种错在一局游戏里几乎看不出来,只会在某次回放里变成一个没法解释的瞬间。
Order is a contract, not a convention: the pipeline checks registration at startup, and a few checkfs pin "physics after move", "notify after physics", "Host last and alone". Written backwards it does not give you a strange result, it crashes at startup. An ordering bug is almost never findable by watching a match; it shows up later as one unexplainable moment in a replay.
附着:链内串行,链间并行 Attachment: Serial Within a Chain, Parallel Across Chains
同一条因果链也存在于实体之间,不只在处理器之间。父子链是最硬的一种:子体的位置由父体决定,父体又可能挂在别人身上。这一帧只更新一环是不行的,五代挂接要五帧才到位,中间每一帧都是一个错的位置。
A causal chain also exists between entities. Parent-child is the hardest case: a child's place is decided by its parent, and the parent may hang on something else in turn. Updating one link per frame will not do: a five-deep attachment would take five frames to settle, and every frame in between shows a wrong position.
所以挂载解算是链内串行、链间并行:一条链由自己的根节点独占遍历,自上而下一帧解完;不同的链互不相交,各在自己的块里并行跑。链上的子节点在每实体那趟循环里直接返回,一个字段都不写。并发安全的依据就这一条:一帧之内,子节点的片段只有一个写者。
So the rule is serial within a chain, parallel across chains. Each chain is walked top-down by its own root and settled inside one frame; different chains own disjoint entity sets and run in parallel. A chain child returns immediately in the per-entity loop and writes nothing at all. That is the whole concurrency argument: within a frame, a child's fragments have exactly one writer.
Host 处理器因此排在 Agent 移动之后,并且独占最后一个阶段。它要的是整条移动链走完之后的世界,顺手补个挂载这种说法不成立。同理,它也排在通知之后,这样本帧排队的生成请求,挂载解算还看得见。
That is why the Host processor runs after agent movement and alone in the last stage. It needs the world after the entire movement chain has finished; it is not tidying up mounts on the side. By the same token it runs after notify, so spawn requests queued this frame are still visible to the mount resolve.
代价,以及什么时候不该切 The Cost, and When Not to Cut
同步点不免费。每切一刀就多一趟波:分发任务、等所有线程到齐、再往下走。切得密、负载又小,波尾的空等就看得见了。另一篇里"一趟并行波胜过六次"讲的就是这个。
Sync points cost. Every cut is one more wave: dispatch, wait for every thread, continue. Cut often with small payloads and the idle tails at each wave start to show. That is the same cost as "one wave beats six" in the processor-granularity note.
所以切分要同时满足两件事,优先次序是清楚的:因果要求分开的,必须分开;因果允许合并的,按实体集合与成本合并。前者错了算错,后者错了只是慢一点。两条并不打架,只是常被搅成一句"处理器该怎么切"。
So a cut has to satisfy two things, and the order of priority is clear: where causality requires separation, separate; where causality allows merging, merge by entity set and cost. Getting the first wrong gives a wrong answer; getting the second wrong only costs speed. The two never conflict; they just get collapsed into one sentence about "how to cut processors".
一句话 In One Line
OOP 靠调用栈把因果链摊开,ECS 用"波 + 阶段边界"把同一条链在一帧里重新排出来。一个处理器就是一趟并行波,波尾是同步点,生成物要的是更强的那个。链条能不能一帧走完,就看你有没有把切分点放在因果的接缝上。
An object world unrolls the causal chain through the call stack; an ECS re-forms the same chain inside one frame out of waves and stage boundaries. A processor is one wave, its end is a sync point, and spawned entities need the stronger one. Whether the chain finishes in a frame comes down to whether the cuts sit on the seams of causality.