Skip to content

NNIDist(): score all pairs in one C++ call - #228

Merged
ms609 merged 2 commits into
mainfrom
claude/nni-all-pairs
Nov 2, 2026
Merged

ms609 merged 2 commits into
mainfrom
claude/nni-all-pairs

Conversation

@ms609-agent

Copy link
Copy Markdown
Collaborator

NNIDist(trees) (one list, all pairs) now renumbers and postorders each tree once and scores every pair in a single C++ call (cpp_nni_distance_all_pairs()), instead of CompareAll() → .NNIDistSingle() per pair. Each tree's splits are computed once; per pair only the split matching and scoring run. cpp_nni_distance() shares the same core (nni_tree(), nni_edge_to_splits(), nni_rf_matching(), nni_score()). Two-argument NNIDist(tree1, tree2) is unchanged.

Identity. Output is identical() to CompareAll(trees, NNIDist) (old path) on all 945 7-leaf trees, unrooted 7-leaf trees, random 60–200-leaf trees with shuffled labels, 3-leaf trees, polytomies, and lists that mix rooted and unrooted trees with equal edge counts (a pair is scored under the first tree's rootedness, as before; the second tree's splits are recomputed in that case).

Benchmark (release installs, min of N, single thread, local R-devel, same session):

Workload main PR
945 × 7 leaves 136.7 s 0.23 s
100 × 50 leaves 1.80 s 0.040 s
40 × 200 leaves 0.86 s 0.110 s

Main's 945-tree time includes the multiPhylo indexing cost fixed in #227; this PR bypasses CompareAll() so doesn't conflict with it. Recorded in .AGENTS/memory/Optimizations.md. At 200 leaves the O(s²) split matching now dominates; sorting canonical splits would allow a linear merge (follow-up).

New R and C++ lines covered by test-tree_distance_nni.R; NEWS entry added.

🤖 Generated with Claude Code

Renumber and postorder each tree once, compute each tree's splits once,
and score every pair in cpp_nni_distance_all_pairs(). Output identical
to CompareAll(trees, NNIDist).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ms609-agent
ms609-agent requested a review from ms609 October 31, 2026 08:50
Comment thread src/nni_distance.cpp Fixed
Comment thread src/nni_distance.cpp Fixed
@github-actions

This comment has been minimized.

@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.77%. Comparing base (2f6776d) to head (4c12013).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #228      +/-   ##
==========================================
+ Coverage   97.74%   97.77%   +0.02%     
==========================================
  Files          60       60              
  Lines        6575     6648      +73     
==========================================
+ Hits         6427     6500      +73     
  Misses        148      148              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Addresses CodeQL "multiplication result converted to larger type".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ms609
ms609 merged commit a59c54e into main Nov 2, 2026
8 checks passed
@ms609
ms609 deleted the claude/nni-all-pairs branch November 2, 2026 09:36
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Performance benchmark results

Call Status Change Time (ms)
ClusteringInfoDistance(tr280) ⚪ NSD -4.35% 9.24, 9.03 →
9.87, 9.47
ClusteringInfoDistance(tr50) ⚪ NSD -7.97% 6.77, 6.72 →
7.42, 7.04
LAPJV(test2000) ⚪ NSD -4.41% 133, 132 →
140, 136
LAPJV(test40) ⚪ NSD -0.91% 0.0187, 0.0186 →
0.0188, 0.0188
LAPJV(test400) ⚪ NSD -1.16% 3.52, 3.51 →
3.58, 3.53
MutualClusteringInfo(tr280) ⚪ NSD -1.49% 18.8, 18.3 →
20.3, 17.7
MutualClusteringInfo(tr50) ⚪ NSD 1.75% 19.6, 17.9 →
17.2, 18
NNIMaxStep(2000, normalize = TRUE) ⚪ NSD 0.19% 0.209, 0.209 →
0.209, 0.209
NNIMaxStep(300, normalize = TRUE) ⚪ NSD -0.36% 0.0309, 0.0309 →
0.0312, 0.0309
NNIMaxStep(300) ⚪ NSD -0.66% 0.00897, 0.00915 →
0.00929, 0.00898
PathDist(postTrees) ⚪ NSD -5.59% 1.64, 1.63 →
1.69, 1.74
PhylogeneticInfoDistance(tr180) ⚪ NSD -1.83% 64, 61 →
63.4, 61.9
PhylogeneticInfoDistance(tr50) ⚪ NSD -2.03% 42.5, 42 →
43.6, 42.7
RobinsonFoulds(tr280) ⚪ NSD -2.87% 4.87, 4.82 →
5.1, 4.8
RobinsonFoulds(tr280) ⚪ NSD -2.2% 4.68, 4.64 →
4.85, 4.64
RobinsonFoulds(tr50) ⚪ NSD -1.1% 4.93, 4.67 →
5, 4.74

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.

3 participants