arXivDaily arXiv每日学术速递 周一至周五更新
arXiv周末暂无论文更新,休息一下吧,周末愉快~~
arXiv 2602.12973cs.PL
收藏

元多态化特殊化

Meta-Monomorphizing Specializations

Università degli Studi di Milano(米兰大学)

Federico Bruzzone, Walter Cazzola

AI总结:

本文提出元多态化特殊化框架,通过编译时元编程重用多态化实现零成本特殊化,解决条件编译与静态分发的冲突问题,提升类型安全性和代码效率。

摘要:

实现零成本特殊化仍是编程语言和编译器设计中的基本挑战。它往往需要在表达力和类型系统正确性之间做出权衡,因为条件编译与静态分发的交互可能导致意想不到的相干性违反和形式模型复杂性增加。本文介绍元多态化特殊化,一种通过编译时元编程重用多态化实现特殊化的新型框架。我们的方法不修改主机编译器,而是生成元多态化特质和实现,将特殊化约束直接编码到类型结构中,实现确定性、一致性的分发而无需重叠实例。我们为一阶、谓词基于和更高排名多态特殊化正式化,包括生命周期参数的存在。基于Rust实现仅使用现有宏设施的评估表明,元多态化使特殊化模式更具表现力,同时保持与标准优化流水线的完全兼容。我们展示特殊化可以作为受控的元编程层实现,提供一种实用、语言无关的高性能抽象路径。对公共Rust代码库的全面研究进一步验证了我们的方法,揭示了众多元多态化可以消除的变通方法,导致更 idiomatic 和高效的代码。对16个微基准的实证评估证实,编译时特殊化匹配或优于运行时TypeId基于分发,并在生命周期基于分发、更高排名类型、复合谓词和通配符匹配等模式上展示了表达性增益,这些模式运行时分发结构上无法表达。

英文摘要:

Achieving zero-cost specialization remains a fundamental challenge in programming language and compiler design. It often necessitates trade-offs between expressive power and type system soundness, as the interaction between conditional compilation and static dispatch can easily lead to unforeseen coherence violations and increased complexity in the formal model. This paper introduces meta-monomorphizing specializations, a novel framework that achieves specialization by repurposing monomorphization through compile-time metaprogramming. Instead of modifying the host compiler, our approach generates meta-monomorphized traits and implementations that encode specialization constraints directly into the type structure, enabling deterministic, coherent dispatch without overlapping instances. We formalize this method for first-order, predicate-based, and higher-ranked polymorphic specialization, also in presence of lifetime parameters. Our evaluation, based on a Rust implementation using only existing macro facilities, demonstrates that meta-monomorphization enables expressive specialization patterns while maintaining full compatibility with standard optimization pipelines. We show that specialization can be realized as a disciplined metaprogramming layer, offering a practical, language-agnostic path to high-performance abstraction. A comprehensive study of public Rust codebases further validates our approach, revealing numerous workarounds that meta-monomorphization can eliminate, leading to more idiomatic and efficient code. An empirical evaluation on 16 micro-benchmarks confirms that compile-time specialization matches or outperforms runtime TypeId-based dispatch, and demonstrates expressiveness gains on patterns -- such as lifetime-based dispatch, higher-ranked types, compound predicates, and wildcard matching -- that runtime dispatch structurally cannot express.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑