常量折叠:一种编译器(或解释器/优化器)优化技术,在编译期(或优化阶段)把只由常量组成的表达式直接计算成结果,从而减少运行时计算、提升性能并简化代码。此术语在讨论编译器优化、静态分析与中间表示(IR)优化时很常见。
/ˈkɒnstənt ˈfoʊldɪŋ/
The compiler performs constant-folding to replace 2 + 3 with 5.
编译器会进行常量折叠,把 2 + 3 直接替换成 5。
After constant-folding and dead-code elimination, the generated code became smaller and faster.
经过常量折叠和死代码消除后,生成的代码更小、更快。
constant(常量)+ folding(折叠)。这里的“folding”并非“折纸”的意思,而是比喻把一个表达式“折叠/收起”为更简单的形式:把可确定的常量计算提前完成,让表达式在优化后变得更“紧凑”。