We introduce a just-in-time runtime program transformation based on repeated recursion unfolding. Our online polyvariant program specialization generates several versions of a recursion differentiated by the minimal number of recursive steps covered. The base case of the recursion is ignored in our technique. Our method is presented here on the basis of linear direct recursive rules. When a recursive call is encountered at runtime, first an unfolder creates specializations of the associated recursive rule on-the-fly and then an interpreter applies these rules to the call. Our approach reduces the number of recursive rule applications to its logarithm at the expense of introducing a logarithmic number of unfolded rules. Each rule is applied at most once. We prove correctness of our technique and determine its worst-case time complexity. For recursions that solve tractable problems and which have enough unfoldings that can sufficiently simplified, we prove a super-linear speedup theorem, i.e. speedup by more than a constant factor. The simplification is problem-specific and has to be provided at compile-time. In the best case, the complexity of the given recursion is reduced to that of its first recursive step. We have implemented the necessary unfolder and meta-interpreter for runtime repeated recursion unfolding in Constraint Handling Rules (CHR) with just five rules. We illustrate the feasibility of our approach with complexity results and benchmarks for several classical algorithms. The runtime improvement quickly reaches several orders of magnitude.
翻译:我们提出一种基于重复递归展开的即时运行时程序变换技术。我们的在线多态程序特化会生成递归的多个版本,这些版本由所覆盖的最小递归步数区分,而递归的基本情形在我们的技术中被忽略。本方法基于线性直接递归规则进行阐述。当在运行时遇到递归调用时,首先展开器会动态创建关联递归规则的特化版本,随后解释器将这些规则应用于该调用。我们的方法将递归规则的应用次数减少至其对数级别,代价是引入对数数量的展开规则,且每条规则至多应用一次。我们证明了该技术的正确性,并确定了其最坏情况下的时间复杂度。对于解决可处理问题且具有足够可简化的展开次数的递归,我们证明了超线性加速定理,即加速比超过常数因子。这种简化是问题相关的,且需在编译时提供。在最佳情况下,给定递归的复杂度可降至其首个递归步骤的复杂度。我们仅用五条约束处理规则(CHR)实现了运行时重复递归展开所需的展开器与元解释器。通过若干经典算法的复杂度分析与基准测试,我们验证了该方法的可行性,运行时性能提升可快速达到数个数量级。