Skip to content

Add C++20 likely/unlikely hints on quick-tricks hot paths#252

Merged
tameware merged 5 commits into
dds-bridge:developfrom
tameware:wopdevries-performance-2
Jul 22, 2026
Merged

Add C++20 likely/unlikely hints on quick-tricks hot paths#252
tameware merged 5 commits into
dds-bridge:developfrom
tameware:wopdevries-performance-2

Conversation

@tameware

@tameware tameware commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Mark rare void / no-opponents suit cases in QuickTricks as [[unlikely]] and the common else arms as [[likely]].
  • No behavioral change; attributes only guide code layout and branch prediction.
  • Roughly sub-1% but consistent performance improvement on batches; instrumentation confirms the quick-tricks hints match observed hit rates.

Test plan

  • bazel test //library/tests:unit_tests //library/tests/system:context_equivalence_test
  • CI green on this PR
  • Optional: compare benchmark.sh before/after on a small hand list to spot-check timing

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.cpp with [[likely]] / [[unlikely]].
  • Marks rare suit/void/trump-edge cases in QuickTricks as [[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.

Comment thread library/src/ab_search.cpp Outdated
}

if (posPoint->tricks_max >= target)
if (posPoint->tricks_max >= target) [[likely]]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero difference over a large sample when inverted. I'll also benchmark removing from this if entirely - presumably I'll find the same.

@zzcgumn

zzcgumn commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

How was the data collected to determine how likely a path is?

@wopdevries

Copy link
Copy Markdown

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 zzcgumn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to merge this as we do see a performance improvement.

@zzcgumn

zzcgumn commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

@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.

@tameware

tameware commented Jul 21, 2026 via email

Copy link
Copy Markdown
Collaborator Author

@tameware

Copy link
Copy Markdown
Collaborator Author

Performance improvement is below 1%, but consistent.

Instrumentation shows that the hints in quick_tricks.cpp are correct:

$ ../dtest_instrumented  --file hands/list100.txt -s calc
DDS DLL
-------
System                      Apple
Word size                 64 bits
Compiler                    clang
Constructor                      
Version                     3.0.0
Memory max (MB)             25463
Threads (MB)       20-30 / 95-160
Number of cores                18
Number of threads               1
Thread sizes             0 S, 1 L
Threading                 None(*)

dtest worker threads: auto
test program
-------------
System                      Apple
Word size                 64 bits
Compiler                    clang

Hand no.                     Time
      40 (  40.0%)           1365
      80 (  80.0%)           1907
     100 ( 100.0%)            854

Timer name             Hand stats
Number of hands               100
User time (ms)               4126
Avg user time (ms)          41.26
Sys time (ms)               66233
Avg sys time (ms)          662.33
Ratio                       16.05
QuickTricks branch hint stats
branch                      hint        hits          pair%     note
own_only                    unlikely    11139887        9.3%     
not_own_only                likely      108206095      90.7%     
own_empty                   unlikely    7148955       -         
own_long_no_otrump          unlikely    314630         14.1%     
own_long_has_otrump         likely      1920904        85.9%     
own_long_nt_or_trump        likely      1755398       -         
part_trump_no_opps          unlikely    1632538       -         
part_side_no_opps           unlikely    6537206       -         
comm_part_only              unlikely    922663          2.3%     
comm_not_part_only          likely      38735095       97.7%     
comm_part_no_otrump         unlikely    67468          13.3%     
comm_part_has_otrump        likely      440590         86.7%     
comm_part_nt_or_trump       likely      414605        -         
comm_part_trump_no_opps     unlikely    366186        -         
comm_part_side_no_opps      unlikely    1081507       -         
winner_rank_zero            unlikely    0             -        

@wopdevries

Copy link
Copy Markdown

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.

@tameware

Copy link
Copy Markdown
Collaborator Author

Tomorrow I'll revert ab_search.cpp to remove it from the PR and submit with just the quick_tricks.cpp hints.

@tameware tameware changed the title Add C++20 likely/unlikely hints on AB and quick-tricks hot paths Add C++20 likely/unlikely hints on quick-tricks hot paths Jul 22, 2026
tameware and others added 3 commits July 21, 2026 20:30
Keep only the quick_tricks.cpp annotations that instrumentation confirmed help.

Co-authored-by: Cursor <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 2 changed files in this pull request and generated no new comments.

@tameware
tameware merged commit 7c96cc6 into dds-bridge:develop Jul 22, 2026
7 checks passed
@tameware
tameware deleted the wopdevries-performance-2 branch July 22, 2026 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants