Dan Luu 写系统问题时,习惯先测量、再对照、最后才下结论。把「Branch prediction」放到智能体、评测和线上系统里,真正要问的是:默认做法会不会系统性失败。下面用中文整理成可执行的工程笔记,去掉原站导航和无关链接。
Predict taken
This is a pseudo-transcript for a talk on branch prediction given at Two Sigma on 8/22/2017 to kick off "localhost", a talk series organized by RC .
How many of you use branches in your code? Could you please raise your hand if you use if statements or pattern matching?
Backwards taken forwards not taken (BTFNT)
I won’t ask you to raise your hands for this next part, but my guess is that if I asked, how many of you feel like you have a good understanding of what your CPU does when it executes a branch and what the performance implications are, and how many of you feel like you could understand a modern paper on branch prediction, fewer people would raise their hands.
The purpose of this talk is to explain how and why CPUs do “branch prediction” and then explain enough about classic branch prediction algorithms that you could read a modern paper on branch prediction and basically know what’s going on.
One-bit
Before we talk about branch prediction, let’s talk about why CPUs do branch prediction. To do that, we’ll need to know a bit about how CPUs work.
For the purposes of this talk, you can think of your computer as a CPU plus some memory. The instructions live in memory and the CPU executes a sequence of instructions from memory, where instructions are things like “add two numbers”, “move a chunk of data from memory to the processor”. Normally, after executing one instruction, the CPU will execute the instruction that’s at the next sequential address. However, there are instructions called “branches” th
Two-bit
Here’s an abstract diagram of a CPU executing some instructions. The x-axis is time and the y-axis distinguishes different instructions.
Here, we execute instruction A , followed by instruction B , followed by instruction C , followed by instruction D .
Two-level adaptive, global (1991)
One way you might design a CPU is to have the CPU do all of the work for one instruction, then move on to the next instruction, do all of the work for the next instruction, and so on. There’s nothing wrong with this; a lot of older CPUs did this, and some modern very low-cost CPUs still do this. But if you want to make a faster CPU, you might make a CPU that works like an assembly line. That is, you break the CPU up into two parts, so that half the CPU can
If you do this, the execution might look something like the above. After the first half of instruction A is complete, the CPU can work on the second half of instruction A while the first half of instruction B runs. And when the second half of A finishes, the CPU can start on both the second half of B and the first half of C. In this diagram, you can see that the pipelined CPU can execute twice as many instructions per unit time as the unpipelined CPU above
Two-level adaptive, local [1992]
There’s no reason that a CPU can only be broken up into two parts. We could break the CPU into three parts, and get a 3x speedup, or four parts and get a 4x speedup. This isn’t strictly true, and we generally get less than a 3x speedup for a three-stage pipeline or 4x speedup for a 4-stage pipeline because there’s overhead in breaking the CPU up into more parts and having a deeper pipeline.
One source of overhead is how branches are handled. One of the first things the CPU has to do for an instruction is to get the instruction; to do that, it has to know where the instruction is. For example, consider the following code:
值得单独记下的观察
- PPC 601(1993): also uses compiler generated branch hints
- Burroughs B4900 (1982): state stored in instruction stream; hardware would over-write instruction to update branch state
- PA 8000 (1996): actually a 3-bit shift register with majority vote
- Pentium MMX (1996): 4-bit global branch history
- Pentium Pro (1996): 4 bit local branch history, low bits of PC used for index . Note that is under some dispute and Agner Fog claims that the PPro and follow-on processors use 4-bit global history
- Pentium II (1997): same as PPro
- Pentium III (1999): same as PPro
- MIPS R12000 (1998): 2K entries, 11 bits of PC, 8 bits of history
落地时建议先做的 5 件事
- 用自己的真实负载测,而不是只用公开榜或厂商数字。
- 把评测设计成能抓到失败模式:平均分好看但尾部崩溃,仍然算失败。
- 智能体默认不会好好用测试;要写进流程,而不是写在口头规范里。
- 性能和正确性都要有基线,改模型或改语言前后必须能对比。
- 结论写成可回滚的决策:哪一版配置、哪一版评测集、谁签字。
和智能体产品怎么接
龙虾PRO做 OpenClaw 落地时,同样吃「先测量再扩面」这条纪律:技能、数字员工和网关都要有可复现评测,而不是只看一次演示通过。
本文侧重全链路风控方法论。落地时请用自身业务单据做回放验证,不要把示例阈值直接当生产策略。 相关:风控体检 · 方案资源
常见问题 FAQ
什么是AI智能系统?
「AI智能系统」可概括为:This is a pseudo-transcript for a talk on branch prediction given at Two Sigma on 8/22/2017 to kick off "localhost", a talk series organized by RC . 本文从定义、方法与实践要点展开说明。
为什么要关注AI智能系统?
关注AI智能系统,是因为它直接影响效率、风险与可复制性。文中指出:This is a pseudo-transcript for a talk on branch prediction given at Two Sigma on 8/22/2017 to kick off "localhost", a talk series organized by RC .
如何落地AI智能系统?有哪些关键步骤?
建议按以下路径推进AI智能系统:1) PPC 601(1993): also uses compiler generated branch hints;2) Burroughs B4900 (1982): state stored in instruction stream; hardware would over…;3) PA 8000 (1996): actually a 3-bit shift register with majority vote;4) Pentium MMX (1996): 4-bit global branch history;5) Pentium II (1997): same as PPro。细节见…
AI智能系统适合哪些人或团队?
AI智能系统更适合:产品/技术负责人、运营与增长团队、需要落地智能体或自动化的中小团队、关注「AI智能系统」方向的读者。若你只需要单次聊天式问答,可先读概念;若要上生产,请重点看步骤、权限与风控相关段落。
关于「Predict taken」,本文给出了什么结论?
在「Predict taken」部分,要点是:your code? Could you please raise your hand if you use if statements or pattern matching? Backwards taken forwards not taken (BTFNT) I won’t ask you to raise your hands for this next part, but my guess is that if I asked
关于「Backwards taken forwards not taken (BTFNT)」,本文给出了什么结论?
在「Backwards taken forwards not taken (BTFNT)」部分,要点是:es a branch and what the performance implications are, and how many of you feel like you could understand a modern paper on branch prediction, fewer people would raise their hands. The purpose of this talk is to explain