From dd02e656932cef2d8369c6cd72a993966384c1ee Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 26 Sep 2026 22:51:31 +0200 Subject: [PATCH] PostRefine: rank the observation cap on I/sigma worked out once, not per comparison nth_element compared through pointers into gigabytes of partials, a cache miss per comparison on one thread (0.3 s on a 16M sweep). The ratio is now computed beside each pointer on all threads; the comparisons and so the selection and its order are the same. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C --- image_analysis/geom_refinement/PostRefine.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/image_analysis/geom_refinement/PostRefine.cpp b/image_analysis/geom_refinement/PostRefine.cpp index a4cd73d87..8bda19687 100644 --- a/image_analysis/geom_refinement/PostRefine.cpp +++ b/image_analysis/geom_refinement/PostRefine.cpp @@ -487,10 +487,19 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, }); constexpr size_t MAX_OBS = 20000; if (n_obs > MAX_OBS) { - std::nth_element(obs.get(), obs.get() + MAX_OBS, obs.get() + n_obs, - [](const Partial *a, const Partial *b) { - return a->I / std::max(1e-9, static_cast(a->sigma)) - > b->I / std::max(1e-9, static_cast(b->sigma)); }); + // By I/sigma, with the ratio worked out once per observation beside it rather than at + // every comparison through the pointer: the same comparisons, so the same selection in + // the same order. + struct Ranked { double i_over_sigma; const Partial *p; }; + std::vector ranked(n_obs); + ParallelChunks(static_cast(n_obs), nthreads, [&](int lo, int hi) { + for (int i = lo; i < hi; ++i) + ranked[i] = {obs[i]->I / std::max(1e-9, static_cast(obs[i]->sigma)), obs[i]}; + }); + std::nth_element(ranked.begin(), ranked.begin() + MAX_OBS, ranked.end(), + [](const Ranked &a, const Ranked &b) { return a.i_over_sigma > b.i_over_sigma; }); + for (size_t i = 0; i < MAX_OBS; ++i) + obs[i] = ranked[i].p; n_obs = MAX_OBS; } result.obs_used = static_cast(n_obs);