From fffc07b894c010f8514576fbf07aef8a8f5e4d89 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 01:00:04 +0100 Subject: [PATCH] tNCS analysis: sort keys read from arrays of their own The modulation band's sort of every acentric reflection, and the class ladder's two sorts, compared fields read from whole MergedReflections - a cache miss per comparison on a large merge, serial, and the largest single cost left in the pre-promotion analysis on a P1 merge of a large cell. The keys are now copied into arrays first; the comparisons and so the orders are unchanged. 4nwv (I222, offline, --no-refine): pre-promotion analyses wait 1.9-3.4 s -> 0.0-0.1 s, report identical. [tncs] tests pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK --- .../scale_merge/TranslationalNCS.cpp | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/image_analysis/scale_merge/TranslationalNCS.cpp b/image_analysis/scale_merge/TranslationalNCS.cpp index f76e569d9..2acb83aee 100644 --- a/image_analysis/scale_merge/TranslationalNCS.cpp +++ b/image_analysis/scale_merge/TranslationalNCS.cpp @@ -535,8 +535,12 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector(i)); } - std::sort(mod_idx.begin(), mod_idx.end(), - [&](int a, int b) { return merged[a].d != merged[b].d ? merged[a].d > merged[b].d : a < b; }); + // the comparisons read d from an array of its own rather than each from a whole MergedReflection: + // this sort, of every acentric reflection, was most of the analysis' serial time on a large merge + std::vector d(merged.size()); + for (int i : mod_idx) + d[i] = merged[i].d; + std::sort(mod_idx.begin(), mod_idx.end(), [&](int a, int b) { return d[a] != d[b] ? d[a] > d[b] : a < b; }); const int n_beyond = static_cast(std::count_if(mod_idx.begin(), mod_idx.end(), [&](int i) { return merged[i].d >= kModulationDMin; })); mod_idx.resize(std::min(kMaxModulationBand, std::max(n_beyond, std::min(kMinModulationBand, @@ -864,12 +868,16 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector(i)); // Equal-count shells sorted by |h|, so that every stage of the ladder reads a population // that is already normalised and can be taken as a prefix. - std::sort(idx.begin(), idx.end(), [&](int a, int b) { - return std::max({std::abs(merged[a].h), std::abs(merged[a].k), std::abs(merged[a].l)}) - < std::max({std::abs(merged[b].h), std::abs(merged[b].k), std::abs(merged[b].l)}); - }); + // the keys in arrays of their own, as the modulation band's sort above + std::vector order(merged.size()); + std::vector d(merged.size()); + for (int i : idx) { + order[i] = std::max({std::abs(merged[i].h), std::abs(merged[i].k), std::abs(merged[i].l)}); + d[i] = merged[i].d; + } + std::sort(idx.begin(), idx.end(), [&](int a, int b) { return order[a] < order[b]; }); std::vector by_d = idx; - std::sort(by_d.begin(), by_d.end(), [&](int a, int b) { return merged[a].d > merged[b].d; }); + std::sort(by_d.begin(), by_d.end(), [&](int a, int b) { return d[a] > d[b]; }); std::vector e2_of(merged.size(), std::numeric_limits::quiet_NaN()); const size_t n_shells = std::max(1, by_d.size() / 500); for (size_t s = 0; s < n_shells; ++s) {