From fc61700c955814e478d48c24ddbf472bf48c1855 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 6 Oct 2026 23:10:07 +0200 Subject: [PATCH] Azimuthal geometry cache keeps six geometries instead of one A run alternates between a handful of centres (file, measured, post-refined, probe, the beam-centre ladder's trial centres), and every miss rebuilt the 16.7 Mpx geometry table. The cache now keeps the six most recently used (~170 MB each at 16 Mpx). Tables are bit-identical whether cached or built. p.mtz md5 unchanged on myob/cytc/thau/kdp (GPU build). Timing: no measurable change on myob/cytc/thau at 3 repeats; it removes table rebuilds where geometries alternate. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi --- common/AzimuthalIntegrationMapping.cpp | 19 +++++++++++-------- common/AzimuthalIntegrationMapping.h | 21 ++++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/common/AzimuthalIntegrationMapping.cpp b/common/AzimuthalIntegrationMapping.cpp index d9d3b1aab..efebb322b 100644 --- a/common/AzimuthalIntegrationMapping.cpp +++ b/common/AzimuthalIntegrationMapping.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only #include "JFJochMath.h" +#include #include #include #include @@ -128,16 +129,18 @@ void AzimuthalIntegrationMapping::SetupConvGeomCached(const DiffractionGeometry static_cast(width), static_cast(height)}); std::lock_guard lock(cache.m); - if (cache.key != key) { + auto &entries = cache.entries; + auto it = std::find_if(entries.begin(), entries.end(), [&](const auto &e) { return e.key == key; }); + if (it == entries.end()) { SetupConvGeom(geom, nullptr); - cache.key = key; - cache.pixel_to_bin = pixel_to_bin; - cache.pixel_resolution = pixel_resolution; - cache.corrections = corrections; + if (entries.size() == AzimuthalIntegrationGeometryCache::MAX_ENTRIES) + entries.erase(entries.begin()); + entries.push_back({key, pixel_to_bin, pixel_resolution, corrections}); } else { - pixel_to_bin = cache.pixel_to_bin; - pixel_resolution = cache.pixel_resolution; - corrections = cache.corrections; + std::rotate(it, it + 1, entries.end()); + pixel_to_bin = entries.back().pixel_to_bin; + pixel_resolution = entries.back().pixel_resolution; + corrections = entries.back().corrections; } // What SetupPixel leaves at a masked pixel. diff --git a/common/AzimuthalIntegrationMapping.h b/common/AzimuthalIntegrationMapping.h index 8ae242213..7e91c4f82 100644 --- a/common/AzimuthalIntegrationMapping.h +++ b/common/AzimuthalIntegrationMapping.h @@ -9,19 +9,26 @@ #include "DiffractionExperiment.h" #include "PixelMask.h" -// The per-pixel geometry of the last mapping built through it, before the mask: every pixel's +// The per-pixel geometry of the last few mappings built through it, before the mask: every pixel's // resolution, correction and bin. None of these depends on the mask - a masked pixel is only left // out - so a mapping for the same geometry under another mask takes them from here and blanks its own // masked pixels, instead of evaluating the geometry of every pixel again. Building a mapping is that -// evaluation, and on a 16 Mpx detector it is a tenth of a second on every core. One entry; a mapping -// being built holds it, so two built at once for the same geometry evaluate it once. +// evaluation, and on a 16 Mpx detector it is a tenth of a second on every core. A run alternates +// between a handful of centres - the file's, the measured one, the post-refined one, the beam-centre +// ladder's trial centres, each built once to index and again to score - so several are kept, the +// least recently used going first (~170 MB each at 16 Mpx). A mapping being built holds the cache, +// so two built at once for the same geometry evaluate it once. class AzimuthalIntegrationGeometryCache { friend class AzimuthalIntegrationMapping; + static constexpr size_t MAX_ENTRIES = 6; + struct Entry { + std::vector key; // bit patterns of everything SetupPixel reads + std::vector pixel_to_bin; + std::vector pixel_resolution; + std::vector corrections; + }; std::mutex m; - std::vector key; // bit patterns of everything SetupPixel reads - std::vector pixel_to_bin; - std::vector pixel_resolution; - std::vector corrections; + std::vector entries; // most recently used last }; class AzimuthalIntegrationMapping {