Skip to content

Speed up to_3di() by avoiding masked arrays in partner search - #1018

Merged
padix-key merged 1 commit into
biotite-dev:mainfrom
padix-key:perf/ana-3di-partners
Nov 10, 2026
Merged

padix-key merged 1 commit into
biotite-dev:mainfrom
padix-key:perf/ana-3di-partners

Conversation

@padix-key

Copy link
Copy Markdown
Member

PartnerIndexEncoder._find_residue_partners() built the O(n²) pairwise distance matrix with numpy.ma operations (ma.dot, masked arithmetic, nan_to_num on a masked array). Those dominated to_3di().

Now the matrix is computed on plain arrays: masked residues are set to NaN (which becomes inf, like the masked entries that argmin() filled with inf before), and nan_to_num(nan=inf) is replaced by an equivalent clip() + NaN assignment. The partner search itself becomes ~3.8x faster (0.54 -> 0.14 ms, 213 residues).

to_3di(), model 1, amino acids only:

Input Longest chain Before After Speedup
4ZXB (5 chains) 800 res 13.0 ms 5.8 ms 2.2x
5H73 363 res 2.13 ms 0.96 ms 2.2x
5UGO 326 res 1.82 ms 0.89 ms 2.1x
1IGY (4 chains) 434 res 7.67 ms 3.72 ms 2.1x
1DIX 208 res 1.03 ms 0.58 ms 1.8x
1AKI 129 res 0.68 ms 0.45 ms 1.5x

The speedup grows with chain length (the quadratic part); for short chains the remaining per-chain overhead dominates.

Output checked identical to main (codes and PartnerIndexEncoder.encode() results) for all structures in tests/structure/data/pdb, including variants with randomly removed atoms to produce masked residues (also at the first position).

Tests: tests/structure/alphabet

Authored by Claude

@codspeed

codspeed Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 91.22%

1 warning

⚠️ 17 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 17 improved benchmarks
✅ 92 untouched benchmarks
🆕 2 new benchmarks
⏩ 14 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ benchmark_find_rotatable_bonds 177.1 ms 26.8 ms ×6.6
⚡ benchmark_superimpose[superimpose] 3,198.9 µs 643.9 µs ×5
⚡ benchmark_hbond 29.7 ms 10.1 ms ×2.9
⚡ benchmark_similar_kmers 483 µs 197.4 µs ×2.4
⚡ benchmark_superimpose_multi_model[superimpose] 14.4 ms 6.4 ms ×2.3
⚡ benchmark_get_assembly 1,594.7 ms 835.6 ms +90.85%
⚡ benchmark_superimpose[superimpose_homologs] 60.6 ms 33.5 ms +80.57%
⚡ benchmark_score_threshold_rule[11*11*1*1***111] 3.3 s 1.9 s +70.9%
⚡ benchmark_score_threshold_rule[None] 3.3 s 1.9 s +70.78%
⚡ benchmark_superimpose_multi_model[superimpose_homologs] 74.2 ms 44.8 ms +65.7%
⚡ benchmark_superimpose[superimpose_without_outliers] 31.9 ms 20.8 ms +52.91%
⚡ benchmark_structural_alphabet_methods[to_3di] 21.1 ms 14.1 ms +49.96%
⚡ benchmark_superimpose[superimpose_structural_homologs] 53.4 ms 35.8 ms +49.32%
⚡ benchmark_match_kmer_selection[KmerTable-None] 642.6 µs 527.6 µs +21.8%
⚡ benchmark_match[KmerTable-None] 739.6 µs 621.8 µs +18.94%
⚡ benchmark_set_structure[bcif-True] 39 ms 32.8 ms +18.79%
⚡ benchmark_set_structure[bcif-False] 17.7 ms 15.1 ms +16.84%
🆕 benchmark_stack[10] N/A 6.7 ms N/A
🆕 benchmark_stack[2] N/A 2 ms N/A

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing padix-key:perf/ana-3di-partners (588a1b9) with main (81df912)2

Open in CodSpeed

Footnotes

  1. 14 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (b0d32ca) during the generation of this report, so 81df912 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@padix-key
padix-key force-pushed the perf/ana-3di-partners branch from eed3978 to 588a1b9 Compare November 10, 2026 17:19
@padix-key
padix-key merged commit 444d2e0 into biotite-dev:main Nov 10, 2026
62 of 63 checks passed

This branch was previously deployed

1 inactive deployment
publish — 588a1b98 Deployed Nov 10, 2026 by padix-key via Upload package to GitHub Releases & PyPI #1557
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.

1 participant