As the ECMAScript specification evolves, industrial-scale JavaScript compilers face the challenge of supporting modern language syntax while maintaining compatibility for diverse execution environments. Traditionally, compilers solve this by running transpilation passes in a monolithic pipeline, where the transpilation passes are chosen to execute strictly based on a target language level. This results in significant computational waste, as compilers perform expensive Abstract Syntax Tree (AST) traversals to lower features that may not exist in the actual input source code. We present a compiler improvement that conditionally executes transpiler passes based on accurately tracking and dynamically maintaining the exact set of language features present in the compilation unit throughout the transpilation process. It is implemented in the production Google Closure Compiler. By populating and maintaining a FeatureSet at every JavaScript script-level, it dynamically skips running unnecessary lowering passes. We detail the architectural safeguards -- including strategic pass ordering and dynamic validation of the transpiled code for feature-correctness. Evaluation of this improvement on large-scale production monorepos produced a considerable reduction in compilation time and saved compute and memory usage.
翻译:随着ECMAScript规范的演进,工业级JavaScript编译器面临支持现代语言语法同时保持对不同执行环境兼容性的挑战。传统上,编译器通过在单一管道中运行转译通道来解决这一问题,其中转译通道的选择严格基于目标语言级别执行。这导致了显著的计算浪费,因为编译器会执行昂贵的抽象语法树遍历来降低实际输入源代码中可能不存在的特性。我们提出了一种编译器改进方案,该方案基于在整个转译过程中准确追踪并动态维护编译单元中存在的精确语言特征集,从而条件性地执行转译通道。该方案已在谷歌闭包编译器(Google Closure Compiler)的生产环境中实现。通过在每个JavaScript脚本级别填充并维护特征集(FeatureSet),系统动态跳过不必要的降级通道执行。我们详细介绍了架构保障措施——包括策略性通道排序和对转译代码进行特征正确性的动态验证。在大型生产级单体仓库上的评估表明,该改进方案显著减少了编译时间,并节省了计算和内存资源的使用。