Skip to content

perf(refine): batch trimer distances -- 13.6% less time - #815

Open
dexhunter wants to merge 1 commit into
soft-matter:masterfrom
dexhunter:perf/batch-trimer-distances
Open

dexhunter wants to merge 1 commit into
soft-matter:masterfrom
dexhunter:perf/batch-trimer-distances

Conversation

@dexhunter

Copy link
Copy Markdown

TL;DR

  • Speed up _trimer_fun during constrained particle refinement.
  • Compute the three edge distances together and reuse the array when squaring.
  • Median runtime for ten 2D trimers: 278.24 ms → 240.65 ms, saving 37.59 ms per refinement.
  • Native least-squares, linking, feature, and legacy-linking tests: 613 passed, 42 skipped.

Refs #814

What & Why

The old code divided, squared, and summed each edge separately. The new code builds one array and runs those operations together.

It keeps the same edge order and arithmetic for every coordinate. This reduces repeated NumPy calls while keeping the fitted results identical.

Validation

Measurements used Python 3.12.13, NumPy 1.26.4, SciPy 1.14.1, and pandas 2.2.3 on Linux, with numerical libraries limited to one thread.

Situation Before After Change
Refine ten Gaussian trimers in 2D 278.24 ms 240.65 ms 37.59 ms saved; 13.5% less time
Native test: ten Gaussian trimers in 3D 766 ms 704 ms 62 ms saved; 8.1% less time
Native test: ten Gaussian dimers in 2D, unchanged code 163 ms 161 ms No expected benefit; 1.2% timing variation, no slowdown observed

The 2D refinement results use 27 measurements of each version across three runs. The additional cases use median pytest durations from five runs per version, alternating versions on the same isolated CPU. Those native test durations include image preparation and assertions. Dimers do not call the changed function.

Statistics for the 2D trimer refinement are:

Statistic Value
Median new/old time ratio 0.864 (13.6% faster)
95% CI of the median (bootstrap, 10,000 resamples) 0.861–0.870 (13.0–13.9% faster)
Mean ± SD of the ratio 0.865 ± 0.007
P95 ratio (slowest 5% of pairs) 0.878
Number of old/new pairs (n) 27 (3 runs)

The elapsed-time medians and median time ratio are calculated separately, so their percentages differ slightly.

Run the representative native benchmarks on each version:

OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 python -m pytest \
  trackpy/tests/test_leastsq.py::TestFit_gauss2D::test_trimer_constrained \
  trackpy/tests/test_leastsq.py::TestFit_gauss3D::test_trimer_constrained \
  trackpy/tests/test_leastsq.py::TestFit_gauss2D::test_dimer_constrained \
  --durations=0

Full native validation command:

python -m pytest trackpy/tests/test_leastsq.py trackpy/tests/test_linking.py \
  trackpy/tests/test_feature.py trackpy/tests/test_legacy_linking.py

Additional comparisons checked 144 combinations of cluster count, dimensions, distance values, numeric types, and reversed arrays. All values, shapes, and types matched exactly, and inputs stayed unchanged. Complete fitting outputs also matched exactly in all three performance runs.

Why this is safe and easy to maintain

The change stays within one small function and uses one shared distance formula. It preserves coordinate arithmetic and the output order. Existing constrained-refinement tests cover 2D and 3D clusters, including anisotropic images.

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/Jb_NT8gNgQk1Pm-gQqVvtzJJ3sj20fOy

Compute the three trimer edge distances together and reuse the squared-distance array. Preserve edge order and coordinate arithmetic.

Co-Authored-By: Aiden <aiden@weco.ai>
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