arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2308.14185cs.PFcs.PL
收藏

面向高频交易的低延迟C++中的半静态条件:优于分支预测提示

Semi-static Conditions in Low-latency C++ for High Frequency Trading: Better than Branch Prediction Hints

Paul Alexander Bilokon, Maximilian Lucuta, Erez Shermer

AI总结:

针对低延迟场景下条件分支预测错误代价易被低估的问题,提出可在运行时修改汇编代码动态调整分支走向的半静态条件结构,在高频交易等场景性能优于传统分支及C++20分支预测提示属性。

摘要:

条件分支给代码优化带来了挑战,尤其是在低延迟场景下。为提升性能,处理器利用专用硬件预测分支结果并推测性执行后续指令,这是一项强大的优化手段。现代分支预测器采用复杂算法和启发式方法,利用历史数据和模式进行预测,通常效果极佳。因此,程序员在用过短或可预测性过强的合成数据对代码做基准测试时,可能会低估预测错误的代价。虽然消除分支并非总能实现,但C++20引入了[[likely]]和[[unlikely]]属性,让编译器能对与大概率执行路径相关的汇编代码进行针对性优化。我们能做得比这更好吗?\n本研究提出了一种新型语言结构的开发方案,名为半静态条件(semi-static condition),它允许程序员在运行时通过修改底层可执行文件中的汇编代码,动态调整分支的走向。随后,我们探究了半静态条件性能优于传统条件分支的场景,凸显了其在实时机器学习和高频交易中的潜在应用价值。在整个开发过程中,我们综合考量了性能、可移植性、语法和安全性等关键因素。

英文摘要:

Conditional branches pose a challenge for code optimisation, particularly in low latency settings. For better performance, processors leverage dedicated hardware to predict the outcome of a branch and execute the following instructions speculatively, a powerful optimisation. Modern branch predictors employ sophisticated algorithms and heuristics that utilise historical data and patterns to make predictions, and often, are extremely effective at doing so. Consequently, programmers may inadvertently underestimate the cost of misprediction when benchmarking code with synthetic data that is either too short or too predictable. While eliminating branches may not always be feasible, C++20 introduced the [[likely]] and [[unlikely]] attributes that enable the compiler to perform spot optimisations on assembly code associated with likely execution paths. Can we do better than this? This work presents the development of a novel language construct, referred to as a semi-static condition, which enables programmers to dynamically modify the direction of a branch at run-time by modifying the assembly code within the underlying executable. Subsequently, we explore scenarios where the use of semi-static conditions outperforms traditional conditional branching, highlighting their potential applications in real-time machine learning and high-frequency trading. Throughout the development process, key considerations of performance, portability, syntax, and security were taken into account.

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

↑