Skip to content

Speed up find_stacking_interactions() and find_pi_cation_interactions() - #1019

Merged
padix-key merged 1 commit into
biotite-dev:mainfrom
padix-key:perf/info-ring-geometry
Nov 10, 2026
Merged

padix-key merged 1 commit into
biotite-dev:mainfrom
padix-key:perf/info-ring-geometry

Conversation

@padix-key

Copy link
Copy Markdown
Member

Both functions computed the centroid and plane normal of each aromatic ring in a Python loop (mean() + np.cross() per ring). The per-ring NumPy call overhead dominated the runtime for structures with many rings.

Fix: compute all centroids and normals at once in a new _get_ring_geometry() helper (replaces _get_ring_normal()): centroids via np.add.at() on the concatenated ring coordinates, normals via a single np.cross() on the first three atoms of each ring. np.add.at() was chosen over np.add.reduceat(), as the latter is not bitwise identical to mean().

Centroids and normals are bitwise identical to the previous implementation for all rings in tests/structure/data/pdb/*.bcif; the returned interactions are identical for these structures (also with all nitrogen atoms artificially charged to exercise the pi-cation path).

Input Function Before After Speedup
1dix (1748 atoms, 37 rings) find_stacking_interactions 1.11 ms 0.41 ms 2.7x
1igy (12750 atoms, 174 rings) find_stacking_interactions 5.34 ms 2.00 ms 2.7x
3wip (16917 atoms, 260 rings) find_stacking_interactions 8.30 ms 3.36 ms 2.5x
3wip (16917 atoms, 260 rings, 1 cation) find_pi_cation_interactions 6.97 ms 2.05 ms 3.4x

Tests run: tests/structure/test_rings.py, tests/test_doctest.py::test_doctest[biotite.structure].

Authored by Claude

@codspeed

codspeed Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 2 benchmarks

⚡ 16 improved benchmarks
❌ 2 regressed benchmarks
✅ 91 untouched benchmarks
🆕 2 new benchmarks
⏩ 14 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ benchmark_cell_list_compute_contacts[MAPPING] 22.9 ms 26.7 ms -14.3%
❌ benchmark_cell_list_compute_contacts[PAIRS] 22.7 ms 25.8 ms -12.09%
⚡ benchmark_find_rotatable_bonds 177.1 ms 26.7 ms ×6.6
⚡ benchmark_superimpose[superimpose] 3,198.9 µs 646.3 µs ×4.9
⚡ benchmark_hbond 29.7 ms 10.1 ms ×2.9
⚡ benchmark_similar_kmers 483 µs 197.7 µs ×2.4
⚡ benchmark_superimpose_multi_model[superimpose] 14.4 ms 6.4 ms ×2.3
⚡ benchmark_get_assembly 1,594.7 ms 835.8 ms +90.81%
⚡ benchmark_superimpose[superimpose_homologs] 60.6 ms 33.6 ms +80.15%
⚡ benchmark_score_threshold_rule[None] 3.3 s 1.9 s +70.66%
⚡ benchmark_score_threshold_rule[11*11*1*1***111] 3.3 s 1.9 s +70.64%
⚡ benchmark_superimpose_multi_model[superimpose_homologs] 74.2 ms 44.8 ms +65.63%
⚡ benchmark_superimpose[superimpose_without_outliers] 31.9 ms 20.8 ms +53.28%
⚡ benchmark_match_kmer_selection[KmerTable-None] 642.6 µs 527.7 µs +21.77%
⚡ benchmark_set_structure[bcif-True] 39 ms 32.7 ms +19.13%
⚡ benchmark_superimpose[superimpose_structural_homologs] 53.4 ms 44.9 ms +18.99%
⚡ benchmark_match[KmerTable-None] 739.6 µs 625.5 µs +18.23%
⚡ benchmark_set_structure[bcif-False] 17.7 ms 15.1 ms +16.93%
🆕 benchmark_stack[10] N/A 6.7 ms N/A
🆕 benchmark_stack[2] N/A 2 ms N/A

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing padix-key:perf/info-ring-geometry (e27f830) 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/info-ring-geometry branch from e405352 to e27f830 Compare November 10, 2026 17:19
@padix-key
padix-key merged commit 229d0b6 into biotite-dev:main Nov 10, 2026
32 of 33 checks passed

This branch was previously deployed

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