Scripting languages are widely used to compose external calls such as native libraries and network services. In such scripts, execution time is often dominated by waiting for these external calls, rendering traditional single-language optimizations ineffective. To address this, we propose a novel opportunistic evaluation strategy for scripting languages based on a core lambda calculus that automatically dispatches independent external calls in parallel and streams their results. We prove that our approach is confluent, ensuring that it preserves the programmer's original intent, and that it eventually executes every external call. We implement this approach in a scripting language called Opal. We demonstrate the versatility and performance of Opal, focusing on programs that invoke heavy external computation through the use of large language models (LLMs) and other APIs. Across five scripts, we compare to several state-of-the-art baselines and show that opportunistic evaluation improves total running time (up to $6.2\times$) and latency (up to $12.7\times$) compared to standard sequential Python, while performing very close (between $1.3\%$ and $18.5\%$ running time overhead) to hand-tuned manually optimized asynchronous Rust. For Tree-of-Thoughts, a prominent LLM reasoning approach, we achieve a $6.2\times$ performance improvement over the authors' own implementation.
翻译:脚本语言广泛用于组合外部调用,如原生库和网络服务。在此类脚本中,执行时间通常主要消耗在等待这些外部调用上,使得传统的单语言优化方法效果有限。为解决这一问题,我们提出了一种基于核心Lambda演算的新型机会性求值策略,该策略可自动并行调度独立的外部调用并流式传输其结果。我们证明了该方法具有汇合性,确保其能保留程序员的原始意图,且最终会执行所有外部调用。我们在名为Opal的脚本语言中实现了这一方法。我们展示了Opal的通用性与性能,重点关注通过调用大语言模型(LLMs)及其他API来执行繁重外部计算的程序。通过对五个脚本的测试,我们与多种先进基线方法进行了比较,结果表明:与标准的顺序执行Python相比,机会性求值显著提升了总运行时间(最高达$6.2\times$)和延迟(最高达$12.7\times$),同时其性能非常接近(运行时间开销介于$1.3\%$至$18.5\%$之间)手动调优的异步Rust实现。对于Tree-of-Thoughts这一重要的大语言模型推理方法,我们相比作者原版实现取得了$6.2\times$的性能提升。