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();