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.
翻译:条件分支对代码优化构成挑战,尤其是在低延迟场景中。为提升性能,处理器依赖专用硬件预测分支结果并推测执行后续指令,这是一种强大的优化手段。现代分支预测器采用复杂算法与启发式方法,利用历史数据和模式进行预测,且通常极为高效。因此,程序员在使用过于简单或可预测性过强的合成数据对代码进行基准测试时,可能无意中低估了误预测的成本。尽管消除分支并非总是可行,但C++20引入了[[likely]]和[[unlikely]]属性,使编译器能够对与可能执行路径相关的汇编代码进行局部优化。我们能否做得比这更好?本研究提出一种新型语言结构——称为半静态条件——允许程序员在执行阶段通过修改底层可执行文件中的汇编代码,动态改变分支方向。随后,我们探索了半静态条件优于传统条件分支的场景,突出其在实时机器学习和高频交易中的潜在应用。在整个开发过程中,我们充分考虑了性能、可移植性、语法和安全性等关键因素。