一句话:提出判别式类型化:乐观推断函数类型并生成优化/未优化双版本,在保持安全性的同时大幅降低渐进类型开销。
渐进类型允许程序员自由混用静态与动态类型,为保证类型可靠性,运行期检查被插入到类型化与非类型化代码的边界上,但在混用密集的程序中会带来显著的运行期开销。本文提出判别式类型化(discriminative typing),在不修改语言底层实现的前提下缓解该开销:它乐观地为函数推断类型,并基于该类型生成一个优化版本;同时依据源码中给出的注解保留一个未优化版本以保证安全。程序随后被翻译为尽量多地调用优化版本、同时保持原有行为的形态。作者在 Reticulated Python 中实现了该技术并与 guarded Reticulated Python 对比:95% 的测试程序性能得到提升,其中 56% 以上获得超过 4 倍加速;与先前的优化方法相比,93% 的程序性能提升,30% 的程序获得 4 至 25 倍加速。此外,判别式类型化显著降低了多种混合类型配置下的渐进类型开销。作者还在 Grift 中实现并评估了该技术,结果表明其性能同样获得显著改善。
Gradual typing has emerged as a popular design point in programming languages, attracting significant interests from both academia and industry. Programmers in gradually typed languages are free to utilize static and dynamic typing as needed. To make such languages sound, runtime checks mediate the boundary of typed and untyped code. Unfortunately, such checks can incur significant runtime overhead on programs that heavily mix static and dynamic typing. To combat this overhead without necessitating changes to the underlying implementations of languages, we present discriminative typing . Discriminative typing works by optimistically inferring types for functions and implementing an optimized version of the function based on this type. To preserve safety it also implements an un-optimized version of the function based purely on the provided annotations. With two versions of each function in hand, discriminative typing translates programs so that the optimized functions are called as frequently as possible while also preserving program behaviors. We have implemented discriminative typing in Reticulated Python and have evaluated its performance compared to guarded Reticulated Python. Our results show that discriminative typing improves the performance across 95% of tested programs, when compared to Reticulated, and achieves more than 4× speedup in more than 56% of these programs. We also compare its performance against a previous optimization approach and find that discriminative typing improved performance across 93% of tested programs, with 30% of these programs receiving speedups between 4 to 25 times. Finally, our evaluation shows that discriminative typing remarkably reduces the overhead of gradual typing on many mixed type configurations of programs. In addition, we have implemented discriminative typing in Grift and evaluated its performance. Our evaluation demonstrations that DT significantly improves performance of Grift.
@article{paperbot2612,
title = {Type-Based Gradual Typing Performance Optimization},
author = {John Peter Campora and Mohammad Wahiduzzaman Khan and Sheng Chen},
journal = {Proceedings of the ACM on Programming Languages},
volume = {8},
number = {POPL},
year = {2024},
doi = {10.1145/3632931}
}