(编译器/优化)常量折叠:在编译或优化阶段,把只由常量组成的表达式提前计算并替换为结果常量,以减少运行时开销、简化代码。也常见于解释器、JIT、静态分析与构建工具中。
/ˈkɑːnstənt ˈfoʊldɪŋ/(AmE), /ˈkɒnstənt ˈfəʊldɪŋ/(BrE)
The compiler performs constant folding, so 2 + 3 becomes 5 before the program runs.
编译器会进行常量折叠,所以 2 + 3 在程序运行前就会变成 5。
With constant folding and other optimizations enabled, the generated code can be smaller and faster, especially in tight loops.
启用常量折叠等优化后,生成的代码可能更小、更快,尤其是在频繁执行的循环中。
constant 源自拉丁语 constans(“坚定的、固定的”),在计算机语境中指“值固定不变的常量”;folding 来自动词 fold(“折叠、合并”),引申为把多个步骤“合并/压缩”为一个结果。合起来就是“把常量表达式折叠成一个常量结果”的优化术语。