Add C++20 likely/unlikely hints on quick-tricks hot paths#252
Conversation
Guide the compiler toward fall-through search and common QT suit cases so rare void/cutoff branches mispredict less often. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Pull request overview
This PR adds C++20 [[likely]] / [[unlikely]] branch prediction hints to hot-path conditionals in the alpha-beta search and quick-tricks logic, aiming to improve generated code layout and branch prediction without changing behavior.
Changes:
- Annotates early cutoff / leaf checks in
ab_search.cppwith[[likely]]/[[unlikely]]. - Marks rare suit/void/trump-edge cases in
QuickTricksas[[unlikely]]and common paths as[[likely]].
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| library/src/ab_search.cpp | Adds [[likely]]/[[unlikely]] hints to cutoff and QT/LT result branches in AB search. |
| library/src/quick_tricks.cpp | Adds [[likely]]/[[unlikely]] hints around rare void/partner-only/opponent-less suit cases in quick-tricks. |
| } | ||
|
|
||
| if (posPoint->tricks_max >= target) | ||
| if (posPoint->tricks_max >= target) [[likely]] |
There was a problem hiding this comment.
Zero difference over a large sample when inverted. I'll also benchmark removing from this if entirely - presumably I'll find the same.
|
How was the data collected to determine how likely a path is? |
|
The likelihood assumptions follow from alpha-beta search theory: most nodes hit an early cutoff (target reached or tricks exhausted) before reaching move generation. The "fall-through to move search" path is therefore the rare case, making cutoff branches [[unlikely]]. This is standard practice in chess/bridge engine optimization. The benchmark results across 11 runs on different hand set sizes support the annotation being correct in practice. |
zzcgumn
left a comment
There was a problem hiding this comment.
Happy to merge this as we do see a performance improvement.
|
@wopdevries, call my old-fashioned but I prefer data supporting claims like "most nodes hit an early cutoff" over this is expected based on what usually happens in alpha-beta searches with pruning. We should merge this as it does improve the performance, but I am still wondering if we can do even better by collecting search statistics. |
|
Per my comments on the related Issue, not all of these help. I think I’ve
isolated the ones that do. I’ll confirm via further benchmarking before I
merge them.
…On Tue, Jul 21, 2026 at 6:25 PM Martin Nygren ***@***.***> wrote:
*zzcgumn* left a comment (dds-bridge/dds#252)
<#252 (comment)>
@wopdevries <https://github.com/wopdevries>, call my old-fashioned but I
prefer data supporting claims like "most nodes hit an early cutoff" over
this is expected based on what usually happens in alpha-beta searches with
pruning. We should merge this as it does improve the performance, but I am
still wondering if we can do even better by collecting search statistics.
—
Reply to this email directly, view it on GitHub
<#252?email_source=notifications&email_token=ABC4PYCYKUG6MJLTPRJ3ZID5F6KOXA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMBTGY2DKMBZGE32M4TFMFZW63VGMFZXG2LHN2SWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-5036450917>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABC4PYG4HWVFVJUZCMKYJNL5F6KOXAVCNFSNUABEKJSXA33TNF2G64TZHMZDMOJYHAZDANR3JFZXG5LFHM2DSMRVG42TQOJUG2QXMAQ>
.
You are receiving this because you were assigned.Message ID:
***@***.***>
|
|
Performance improvement is below 1%, but consistent. Instrumentation shows that the hints in quick_tricks.cpp are correct: |
|
The instrumentation data beautifully answers @zzcgumn's question. All [[likely]]/[[unlikely]] annotations in quick_tricks.cpp are confirmed correct by actual hit counts. The dominant paths (not_own_only 90.7%, comm_not_part_only 97.7%) match the hints exactly. I also measured on Linux (Intel i7-4770, Z87) with perf stat, 1000 hands single-threaded: Branch miss rate: 4.32% (1.6B misses / 37.2B branches) Ready to merge — the data supports it. |
|
Tomorrow I'll revert |
Keep only the quick_tricks.cpp annotations that instrumentation confirmed help. Co-authored-by: Cursor <cursoragent@cursor.com>
Summary
QuickTricksas[[unlikely]]and the commonelsearms as[[likely]].Test plan
bazel test //library/tests:unit_tests //library/tests/system:context_equivalence_testbenchmark.shbefore/after on a small hand list to spot-check timing