Skip to content

perf(structure): index_distance() -- 2.3x faster on 1gya - #1112

Draft
dexhunter wants to merge 1 commit into
biotite-dev:mainfrom
dexhunter:perf/index-distance-layout-v2
Draft

dexhunter wants to merge 1 commit into
biotite-dev:mainfrom
dexhunter:perf/index-distance-layout-v2

Conversation

@dexhunter

Copy link
Copy Markdown

TL;DR

  • This draft adds a benchmark for index_distance() first, as the contribution guide requires.
  • The following commit will group the x, y, and z components before summing their squares.
  • On our mixed timing the median fell from 351.20 to 153.02 ms, about 2.3 times faster on 1gya contact pairs.
  • All 76 tests passed with pytest -q tests/structure/test_geometry.py tests/structure/test_compare.py.
  • After the repository benchmark job records the current speed, that optimization will be pushed to this pull request.

What & Why

The old calculation read three adjacent values per contact pair. NumPy can sum across many pairs faster when each component occupies its own contiguous block.

The layout check keeps the order of the model and pair axes. The same dot product adds the same three terms for each distance.

Results

Situation Before After Change
One model, one pair 7.08 µs 7.49 µs 5.9% more time
One model, 100 pairs 11.09 µs 10.34 µs 6.7% less time
One model, 1,000 pairs 43.98 µs 30.81 µs 30.0% less time
One model, 10,000 pairs 372.33 µs 231.27 µs 37.9% less time
18 models, 149,613 pairs 58.67 ms 23.98 ms 59.1% less time

Calls with one or ten pairs took longer. The first measured gain was at 100 pairs. A direct two-atom distance took 1.79 µs before and 1.88 µs after.
The full lddt() calculation took 150.62 ms before and 119.90 ms after.

Three runs of the mixed workload gave these results on x86-64.

Statistic Value
Median new/old time ratio 0.436 (56.4% faster)
95% CI of the median (bootstrap, 10,000 resamples) 0.427–0.442 (55.8–57.3% faster)
Mean ± SD of the ratio 0.434 ± 0.022
P95 ratio (slowest 5% of pairs) 0.474
Number of old/new pairs (n) 27 (3 runs)

With default thread settings, the four-call loop took 82.32 ms before and 40.95 ms after (ratio 0.497). These figures are from our measurements. The repository benchmark job will compare the same function before and after the optimization commit.

Validation

The public timing command reports milliseconds per four-call loop.

  • Metric: run_time
  • Direction: lower_is_better
  • Measurement command: python -m timeit -n 5 -r 9 -s "import numpy as np, biotite.structure as struc, biotite.structure.io.pdbx as pdbx; a = pdbx.get_structure(pdbx.BinaryCIFFile.read('tests/structure/data/pdb/1gya.bcif')); a = a[..., struc.filter_heavy(a)]; f = a[0]; p = np.argwhere(np.triu(struc.CellList(f, 15.0).create_adjacency_matrix(15.0), k=1)); b = np.diag(np.ptp(f.coord, axis=0) + 30.0).astype(np.float32)" "struc.index_distance(f, p); struc.index_distance(a, p); struc.index_distance(f.coord, p); struc.index_distance(f, p, periodic=True, box=b)"
  • Baseline value: 81.9
  • New value: 40.3
  • Run time before (ms): 351.19732
  • Run time after (ms): 153.02104
  • Peak memory before (KiB): 451232
  • Peak memory after (KiB): 439192
  • Files changed: 2
  • Language: Python
  • Licence: BSD-3-Clause

Run the native tests from the repository root.

uv venv .venv && uv pip install --python .venv/bin/python ".[test]" && .venv/bin/python -m biotite.setup_ccd && .venv/bin/python -m pytest -q tests/structure/test_geometry.py tests/structure/test_compare.py

Why this is safe and easy to maintain

The small layout check keeps the shared distance formula. The output stays unchanged for masked arrays and ndarray subclasses. The regression tests cover partial masks, full masks, and ndarray subclass dispatch through both public functions.

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/9bvUA5sg01NabkvFn4am1_S_GfdywmDu

Time contact-pair distances on the 1gya structure, including the
periodic-box case, so later timing changes can be compared.

Co-Authored-By: Aiden <aiden@weco.ai>

This branch has not been deployed

No deployments
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