事件不是免费的:为什么"事件快、tick 慢"是个误解 Events Are Not Free: Why "Events Fast, Tick Slow" Is a Misconception
把事件拆开看:检测、去重、回调各是什么 · 2026-11-21Taking an event apart: detection, diffing, callback · 2026-11-21
系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →
那个说法 The Claim
有一种说法流传很广:事件触发快,每帧跑一遍慢。照这个说法,"在 tick 里检测碰撞"是新手写法,"用 BeginOverlap / EndOverlap"才叫专业。
A line you hear all the time: events are fast, per-frame work is slow. By that account, "check collisions in tick" is the beginner's move and "use BeginOverlap / EndOverlap" is the professional one.
这个结论只在特定条件下成立,让它成立的东西通常跟"事件"两个字无关。把一次事件拆成判定、去重、处理三段,会发现对"每一刻都得知道状态"的事件,前两段一分都省不掉。重叠就属于这一类。
That conclusion holds only under specific conditions, and what makes it hold usually has nothing to do with the word "event". Take an event apart (deciding, diffing, handling): for events that need the state known every moment, the first two cannot be saved at all. Overlap is one of those.
引擎里的 overlap 事件,本身就是每帧算的 The Engine's Own Overlap Event Is Per-Step Work
UE 的组件 overlap 事件,背后是一段先查询、再比对、最后派发的链路,跟"相交那一刻引擎喊了一声"没什么关系。组件更新重叠时先做一次完整的 overlap 查询,拿到"我现在跟谁重叠"的新列表,再拿它跟上一帧留下的老列表做差:遍历老列表,每一项在新列表里线性查一遍,命中就把两边各删一个;两边剩下的,一边是"该发 begin"的,一边是"该发 end"的。
Behind a component's overlap event sits a query, diff, dispatch chain; nothing in the physics engine shouts at the moment of intersection. Updating overlaps runs a full overlap query for the new "who am I overlapping now" list, then diffs it against the list left over from last time: walk the old list, linearly search the new one for each entry, drop one from each side on a hit. What remains on either side is a begin or an end.
查询和比对都每一步跑:变换没动时查询可以复用上一帧的结果,省掉那一次,比对则一步不落。事件只是比对的产物。"用事件就不用每帧检测"这句话在引擎内部站不住——检测照做,去重照做,只是写在 C++ 里、藏在引擎里,不在你的 tick 图里。
Both run every step: the query every step (a transform that has not moved lets it reuse last step's result and skip that query), the diff without exception. The event is only the diff's product. "With events you do not check every frame" does not hold inside the engine: the check happens, the dedupe happens, just in C++ inside the engine rather than in your tick graph.
MassBattle 的空间检测:函数,不是事件 MassBattle's Spatial Detection: Functions, Not Events
经常有人问:敌人的范围检测有事件吗?没有,而且这是故意的。MassBattle 的空间检测就是一组查询函数——Trace Entity 那一族(球、扇、盒、胶囊、锥、视锥,各自还有扫掠版),加上处理重叠结果的函数。你调它,它当场返回"这一刻形状里有哪些实体";不调,什么都不会发生。旧的 SphereTraceForAgents 那批已经从直接使用的位置退下来,指向新的 Trace Entity 节点,语义还是同一个:一次查询,立刻拿结果。
People ask all the time: is there an event for "who is in range"? No, and that is deliberate. MassBattle's spatial detection is a family of query functions — the Trace Entity set (sphere, sector, box, capsule, cone, view cone, each with a sweep variant) plus the function that processes overlap results. Call one and it returns "which entities are inside the shape right now"; call nothing and nothing happens. The old SphereTraceForAgents batch has stepped back from direct use in favour of the Trace Entity node, semantics unchanged: one query, an immediate answer.
为什么不做成事件?上一节把答案摆出来了:范围变化这件事,判定本身就是主要成本,而且必须每帧重新判定;事件只能把"处理"省下来,省不掉"判定"。与其在框架里藏一套每帧照跑、还要维护 begin/end 差分的机器,不如把查询函数交给你:什么时候查、查多大、查完拿结果做什么,全归你的玩法决定。追击处理器就是这么干的,每 tick 查一次;你的蓝图也可以这么做。
Why not events? The previous section gives the answer: for range changes the decision itself is the main cost, and it has to be re-decided every frame; an event can only save the handling, never the decision. Rather than hide a machine that runs every frame anyway and maintains begin/end diffs, the framework hands you the query: when to run it, how wide, and what to do with the answer are your gameplay's business. The chase processor works exactly this way, once per tick; your Blueprint can too.
这套查询本身也做了优化,最值钱的一条在块这一级。哈希网格按块存放实体,每个块上挂着这块里实体的组成摘要;查询先把形状折算成块坐标范围,拿查询条件跟每个块的摘要先在块这一级比对,对不上的整块跳过——块里几千个格子、再多实体都碰不到。块内再按占用位图跳过空格子,最后才轮到逐实体的几何判定。这就是块级剪枝:一次扔掉一整块,不是一个一个地拒绝。还有一层可选的交叉剪枝(拿"必须没有"的 flag 提前排除整块),默认关着——它多一份每帧成本,只对大规模排除查询才回本,这个取舍在Tags 那篇讲过。
The queries are optimized too, and the most valuable part sits at block level. The hash grid stores entities in blocks, and each block carries a composition summary of what lives in it; a query first folds the shape into a block-coordinate range, compares its conditions against each block's summary, and skips any block that cannot match — the thousands of cells and every entity inside never get touched. Inside a surviving block, occupancy bitmaps skip empty cells, and only then does the per-entity geometric test run. That is block-level pruning: reject a whole block at once instead of refusing entities one by one. One optional deeper prune (using "must not have" flags to exclude whole blocks early) ships off by default — it adds per-frame cost that only pays for large exclusion queries, a trade-off covered in the Tags post.
事件什么时候真的更快 When an Event Really Is Faster
话先说明白:事件确实能把执行频率降下来,那也确实是更快,次数少了、活少了,当然快。"事件不是免费的"因此不能一概而论,它有一个前提:事件省掉的,是不是真正贵的那部分工作。
To be fair: an event really can lower the execution frequency, and that really is faster — fewer invocations, less work. So "events are not free" needs its precondition stated: does the event let you skip the part that is actually expensive?
一次"事件"可以拆成两半:判定,这一刻到底有没有事件;处理,有了之后你要做的事。收益全部来自后一半,没发生就没人处理;前一半一分都省不掉,而且往往是花钱最多的那个。
Split one event in two: deciding whether an event exists this instant, and handling what you do once it does. All the gain sits in the second half — no event, no handler. The first half cannot be saved at all, and it is usually the expensive one.
| 事件类型Kind of event | 判定贵不贵Is deciding cheap | 处理贵不贵Is handling expensive | 换成事件值不值Is the event worth it |
|---|---|---|---|
| 低频的业务事件(一次命令、一次点击、一个计时器到点)Low-frequency business events (one command, one click, a timer expiring) | 便宜:本来就只在那一刻发生Cheap: it only happens at that moment anyway | 通常贵Usually expensive | 值:不做就不付处理费,省下的是真省下的Yes: no event, no handler, and the saving is real |
| 需要"每一刻都知道状态"的事件(重叠开始/结束、进入范围/离开范围)Events that require knowing the state every moment (overlap begin/end, enter/leave range) | 贵:每步都得重新判定,不然不知道什么时候变了Expensive: decided again every step, or the change goes unnoticed | 可能便宜Possibly cheap | 差不多:判定的钱照付,事件只是把结果分了类Much the same: the deciding is paid either way; the event just sorts the result |
重叠就是第二行。它最贵的地方是"要不要触发"这个判定,而这个判定必须每步做,绕不开。做成事件也好,轮询也好,开销差不多:差别只是把"查完直接处理"换成了"查完、比对、只在变化时处理"。省的在后半段,前半段照付。
Overlap is that second row. Its most expensive part is the "should this fire" decision, and that decision is due every step. Event or polling, the cost is about the same: "check, handle" becomes "check, diff, handle only on change". The second half is saved; the first is paid in full.
至于"感觉上事件更快",还有三个原因跟事件本身无关:它写在 C++ 里,同一套逻辑放进引擎 C++ 当然比蓝图连节点快;它只在变化时进你的代码,profiler 里根本看不到它;它只对开了重叠事件的组件跑,没参与的那批把平均值拉低了。
Why events feel faster comes down to three things that have nothing to do with events: it is C++ (the same logic in engine C++ beats a graph of Blueprint nodes); it only enters your code on change, so the profiler never shows it; and it only runs for components that enable overlap events, with everything else pulling the average down.
ECS 这边:遍历不慢,而且能并行 On the ECS Side: Iteration Is Cheap, and It Parallelises
"在 tick 里遍历很慢"这句话在对象世界里成立,到 ECS 里就弱得多。实体按原型分块存放,一次查询只走过命中的那些块,标记那篇讲过这一点:你要比的是几十个标签,不是十万条记录。
"Iterating in tick is slow" holds in an object world and much less so in an ECS. Entities sit in chunks by archetype, a query walks only the chunks that match, and as the tagging article put it, you compare dozens of labels, not a hundred thousand records.
更要紧的是候选集。一次形状查询(球、扇、盒)先把"可能相交"缩到形状之内,之后每个候选只做一次几何判定。中间没有"遍历全场景的 actor,挨个问'你有没有碰撞组件'"这一步。那一步在对象世界是实打实的开销。
The bigger factor is the candidate set: a shape query (sphere, sector, box) narrows "might intersect" to the entities inside the shape, and each candidate then costs one geometric test. Nothing walks every actor in the scene asking whether it has a collision component. In the object world that walk is a real cost.
还有一层更深的差别:这种遍历天生适合并行。数据是一段一段连续的块,块与块之间没有对象图、没有互相引用、也没有"调别人虚函数"这回事。一次查询命中的块可以切成几份分给不同的核,各处理各的,谁也不等谁。面向对象当然也能并行,只是难得多:每加一处并行,都得回答"这些对象会不会互相改、GC 会不会中途把它们搬走、锁由谁负责"。
There is a deeper difference: this iteration parallelises naturally. The data is a run of contiguous chunks with no object graph between them, no cross-references, no calls into some other object's virtual function. A query's matching chunks can be split across cores, each core works its own slice, and no one waits. Object-oriented code can do this too — it is just far harder: every parallel step has to answer whether these objects can mutate each other, whether the GC can move them mid-flight, and who takes the lock.
所以 ECS 在这块的收益,是把空闲的核用起来更容易。同一份逻辑,在 ECS 里写成"每帧遍历一遍"常常划算,在对象世界里大家本能地躲着写。
The gain here is that idle cores are easier to put to work. Same logic, "walk it every frame" is usually a good deal in an ECS, while in an object world people instinctively avoid it.
性能其实只有几条路 There Are Only a Few Levers
把前面这些放到一张图上。能变快的路数其实是有限的:
Put all of the above on one map. The ways to go faster are finite:
| # | 做法Lever | 具体形式In practice |
|---|---|---|
| 1 | 少算一点Compute less | 降低频率、跳过不需要的、去掉重复计算、提前退出、剪枝(事件属于这一条)Lower the frequency, skip what is not needed, remove duplicate work, early-out, prune (events live here) |
| 2 | 多用硬件Use more hardware | 多线程、并行、异步、把闲置的核用上(ECS 的分块遍历属于这一条)Threads, parallelism, async, putting idle cores to work (chunked ECS iteration lives here) |
| 3 | 分一部分给 GPUGive some to the GPU | 把"每个单位都做一遍、彼此独立"的计算搬到 GPU;把插值与表现搬过去尤其划算Move per-unit, mutually independent work onto the GPU — interpolation and presentation are especially good candidates |
| 4 | 把内存喂饱Feed the memory system | 连续布局、少指针跳转、少拷贝、数据紧凑到一次缓存行能装下更多实体Contiguous layout, fewer pointer chases, fewer copies, packing more entities into one cache line |
| 5 | 让每一次算得更便宜Make each operation cheaper | 向量化、去分支、常量折叠、用查表代替分支选择——同样的次数,算得更省Vectorise, remove branches, fold constants, use tables instead of branches — same number of operations, each one cheaper |
| 6 | 把活分摊到时间上Spread work over time | 摊到多帧、隔帧重算、惰性求值、按需降级(远处的单位用更粗的规则)——不必一帧里全部算完Split across frames, recompute on demand, lazy evaluation, coarse rules for distant units — not everything has to finish in one frame |
前四条是主干,后两条是补充:第五条改单次成本,第六条改什么时候付。真开始优化时,值得先问一句:你打算动哪一条?很多争论(事件对 tick 也算)其实是在争第一条里的一个子项该由谁执行,两种机制本身没那么大差别。
The first four are the trunk, the last two are refinements: the fifth changes what one operation costs, the sixth changes when you pay. Before optimising, ask which lever am I pulling? Most arguments about events versus tick are really about who executes one sub-item of lever one; the mechanisms themselves are not that far apart.
我们的去重是哈希,不是线性查找 Our Dedupe Is Hashing, Not a Linear Search
这一条可以对着源码看。拿"这一帧的列表"和"上一帧留下来的列表"做差时,我们用两张哈希集合:两份列表各建一次集合,然后并行给每个元素做一次成员判定,标出新增和消失;最后一趟原地压缩把消失的元素从累积表里剔掉,再把新增的追加进去。
This part can be checked against the source. To diff "this frame's list" against "what survived last frame" we build two hash sets and run a parallel membership test per element, marking what is new and what is gone; one in-place compaction pass then drops the vanished entries from the accumulated array and appends the new ones.
复杂度 O(N+M)。被它换掉的写法是每个元素都在数组里线性查一遍(O(N×M)),再给每个消失的元素做一次链式删除。引擎那边 diff 的形状就是这样:老列表逐项、新列表线性查找、命中就两边各删一个。哈希去重在结构上确实更省:少一趟平方级的查找,少一串删除。
That is O(N+M). What it replaced was a linear search per element against an array (O(N×M)) plus a chained removal for every vanished element — exactly the shape of the engine's diff: walk the old list, linearly search the new one, remove one from each side on a hit. Structurally cheaper: one quadratic search fewer, one chain of removals fewer.
那个"很慢"的示例,慢在哪 Where the "Slow" Example Is Actually Slow
拿一个基于 Actor 的投射物举例:运动交给自带的投射物移动组件,每个 tick 调一次检测函数,命中就调施加伤害的函数,整条链在蓝图里。它确实可能慢,慢在蓝图,跟"在 tick 里"没关系。
Take an actor-based projectile: movement on the stock projectile-movement component, one trace call per tick, and a damage call on a hit, all wired in Blueprint. It can be slow, and the reason is Blueprint, not "in tick".
同样这两个调用,搬进 C++ 的 tick,成本掉一个量级:没有蓝图虚拟机,没有逐节点取参数,没有蓝图数组的拷贝与装箱。这时候它比引擎的 overlap 路线也不多花什么:检测是同一个形状查询,候选集已经裁过;去重靠哈希集合,没有线性查找。
The same two calls in C++ tick drop by an order of magnitude: no Blueprint VM, no per-node argument marshalling, no Blueprint array copies and boxing. It also pays nothing extra next to the engine's overlap route: the same shape query with an already-narrowed candidate set, and a hash dedupe in place of a linear search.
顺带把伤害那一步说清:点伤、径向、光束三条施加伤害的路径都是函数,各有一个能自带查询的版本,调用时不需要任何 Actor 存在。所以"函数 + 每 tick 调"和"引擎事件"之间,真正的分别只有一条:时机和候选集是不是握在你自己手里。
Which leaves the damage step: point, radial and beam damage are all functions, each with a variant that carries its own query, and calling them needs no actor to exist. So between "a function called every tick" and "an engine event" the difference comes down to one thing: whether you own the timing and the candidate set.
那该怎么选 So Which One
| 你的情况Your situation | 用什么Use | 为什么Why |
|---|---|---|
| 变化很稀少,而且你只想在变化时做事(一栋建筑被砸了一次)Changes are rare and you only want to react when they happen (a building takes a hit once) | 事件 / 回调Events / callbacks | 检测照付,但你的处理逻辑只跑真正需要的那几次Detection is still paid, but your handler runs only the few times it matters |
| 你要的是"这一刻谁在范围内",而且单位很多You need "who is in range right now", with many units | 形状查询函数,每 tick 调A shape query function, called per tick | 候选集先被裁一次;遍历在 ECS 里本来就不贵The candidate set is narrowed once, and iteration is cheap in an ECS |
| 联机、要求两端一致Networked, peers must agree | 函数(在模拟里)Functions (inside the simulation) | 引擎 overlap 由物理结算的时序驱动,谁在范围内会随帧序变化,那就是失步Engine overlaps are driven by physics ordering, so "who is in range" varies with frame timing — that is a desync |
| 你要 begin/end 两份数组,还要自己处理去重You want begin / end arrays and your own dedupe | 重叠处理函数(累积表进、begin/end 出)The overlap-processing function (accumulated in, begin/end out) | 哈希判定 + 并行 + 原地压缩,O(N+M)Hash membership, parallel, in-place compaction — O(N+M) |
这些结论是怎么来的 Where These Conclusions Come From
这些结论来自实打实的优化。哈希去重换掉线性查找这一套,是长期的实际优化里遇到问题、改出来、又验过的。方向很确定:对着源码就能指出引擎那边是"每步查询 + 线性查找的 diff",我们这边是"哈希判定 + 原地压缩"。具体快多少随场景变,候选集大小、命中率、并行度、缓存表现都会影响;哪一边更省,不必再讨论。
These conclusions come out of real optimisation: the hash dedupe was found, built and re-verified over long stretches of actual optimisation work. The direction is settled — the source shows the engine doing a per-step query plus a linear-search diff, and us doing hash membership plus in-place compaction. How much faster depends on the scene (candidate-set size, hit rate, parallelism, cache behaviour); which side is cheaper is not an open question.
所以这篇只立一句话:当"判定"本身就是主要成本,事件不会让它消失,只会把它挪个位置;"在 tick 里调用函数"也从来不是慢的原因。慢的原因总是更具体:蓝图、线性查找、没裁剪的候选集,或者每帧都把本该留住的东西重建一遍。找到那一条,才谈得上优化。
So this article stands on one sentence: when the decision itself is the main cost, an event does not remove it, it moves it; and "calling a function in tick" is never the reason for slowness. The real reason is always more specific — Blueprint, a linear search, an untrimmed candidate set, or rebuilding every frame something that should have been kept. Find that one and you can talk about optimisation.