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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-11-14 01:00:04 +01:00
1 parent a22d8b9b56
commit fffc07b894
1 file changed
+15 -7
@@ -535,8 +535,12 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector<MergedReflectio
if (std::isfinite(r.I) && std::isfinite(r.d) && acentric(r))
mod_idx.push_back(static_cast<int>(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<float> 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<int>(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<int>(kMinModulationBand,
@@ -864,12 +868,16 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector<MergedReflectio
idx.push_back(static_cast<int>(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<int> order(merged.size());
std::vector<float> 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<int> 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<double> e2_of(merged.size(), std::numeric_limits<double>::quiet_NaN());
const size_t n_shells = std::max<size_t>(1, by_d.size() / 500);
for (size_t s = 0; s < n_shells; ++s) {