From b7c32a49e322328ef93837bee9a02a96d19c6b45 Mon Sep 17 00:00:00 2001 From: dktue <2878623+dktue@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:08:56 +0100 Subject: [PATCH] perf: publish the node/way stores' leftover groups from all threads At the end of the nodes and ways passes, SortedNodeStore/SortedWayStore::finalize publish the "orphan" groups (groups split between worker batches) one after the other: sort, encode, allocate. On the planet that took ~10 s after the ways pass with every other thread idle. The groups are independent and publishGroup is what the workers call concurrently during the pass, so finalize now publishes them from threadNum threads. Same store content. Planet (in memory, 96 threads): the ~10 s single-threaded gap after the ways pass is gone (wall -9 s). Germany output identical. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/sorted_node_store.cpp | 42 ++++++++++++++++++++++++++------------- src/sorted_way_store.cpp | 42 ++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/sorted_node_store.cpp b/src/sorted_node_store.cpp index c6d282e8..e9663a02 100644 --- a/src/sorted_node_store.cpp +++ b/src/sorted_node_store.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include #include #include @@ -530,20 +533,31 @@ void SortedNodeStore::finalize(size_t threadNum) { } workerBuffers.clear(); - // Empty the orphanage into the index. - std::vector copy; - for (const auto& entry: orphanage) { - for (const auto& orphan: entry.second) - copy.push_back(orphan); - - // Orphans may come from different workers, and thus be unsorted. - std::sort( - copy.begin(), - copy.end(), - [](auto const &a, auto const &b) { return a.first < b.first; } - ); - publishGroup(copy); - copy.clear(); + // Empty the orphanage into the index. The groups are independent and publishGroup is what the + // workers call during the pass anyway, so publish them from all threads: one after the other, + // the planet's leftover groups took ~10 s with every other thread idle. + std::vector*> orphanGroups; + for (const auto& entry: orphanage) + orphanGroups.push_back(&entry.second); + std::atomic nextGroup(0); + auto publishOrphans = [&]() { + std::vector copy; + for (size_t i = nextGroup++; i < orphanGroups.size(); i = nextGroup++) { + copy.assign(orphanGroups[i]->begin(), orphanGroups[i]->end()); + // Orphans may come from different workers, and thus be unsorted. + std::sort( + copy.begin(), + copy.end(), + [](auto const &a, auto const &b) { return a.first < b.first; } + ); + publishGroup(copy); + } + }; + { + boost::asio::thread_pool pool(std::max(1, threadNum)); + for (unsigned int t = 0; t < std::max(1, threadNum); t++) + boost::asio::post(pool, publishOrphans); + pool.join(); } orphanage.clear(); diff --git a/src/sorted_way_store.cpp b/src/sorted_way_store.cpp index 02f66147..81cd9132 100644 --- a/src/sorted_way_store.cpp +++ b/src/sorted_way_store.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #include #include #include @@ -278,20 +281,31 @@ void SortedWayStore::finalize(unsigned int threadNum) { } workerBuffers.clear(); - // Empty the orphanage into the index. - std::vector>> copy; - for (const auto& entry: orphanage) { - for (const auto& orphan: entry.second) - copy.push_back(orphan); - - // Orphans may come from different workers, and thus be unsorted. - std::sort( - copy.begin(), - copy.end(), - [](auto const &a, auto const &b) { return a.first < b.first; } - ); - publishGroup(copy); - copy.clear(); + // Empty the orphanage into the index. The groups are independent and publishGroup is what the + // workers call during the pass anyway, so publish them from all threads: one after the other, + // the planet's leftover groups took ~10 s with every other thread idle. + std::vector>>*> orphanGroups; + for (const auto& entry: orphanage) + orphanGroups.push_back(&entry.second); + std::atomic nextGroup(0); + auto publishOrphans = [&]() { + std::vector>> copy; + for (size_t i = nextGroup++; i < orphanGroups.size(); i = nextGroup++) { + copy.assign(orphanGroups[i]->begin(), orphanGroups[i]->end()); + // Orphans may come from different workers, and thus be unsorted. + std::sort( + copy.begin(), + copy.end(), + [](auto const &a, auto const &b) { return a.first < b.first; } + ); + publishGroup(copy); + } + }; + { + boost::asio::thread_pool pool(std::max(1, threadNum)); + for (unsigned int t = 0; t < std::max(1, threadNum); t++) + boost::asio::post(pool, publishOrphans); + pool.join(); } orphanage.clear();