From 07ef24680d16929bbd900d65f8658b74382997c0 Mon Sep 17 00:00:00 2001 From: Anatoly Serdtcev Date: Tue, 29 Jan 2019 20:03:36 +0300 Subject: [geocoder] Concurrent index houses --- geocoder/index.cpp | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'geocoder') diff --git a/geocoder/index.cpp b/geocoder/index.cpp index 86e89ea0c7..f11e991c0a 100644 --- a/geocoder/index.cpp +++ b/geocoder/index.cpp @@ -8,7 +8,10 @@ #include "base/logging.hpp" #include "base/string_utils.hpp" +#include #include +#include +#include using namespace std; @@ -88,29 +91,47 @@ void Index::AddStreet(DocId const & docId, Index::Doc const & doc) void Index::AddHouses() { - size_t numIndexed = 0; - for (DocId docId = 0; docId < static_cast(m_docs.size()); ++docId) - { - auto const & buildingDoc = GetDoc(docId); + atomic numIndexed{0}; + std::mutex mutex; - if (buildingDoc.m_type != Type::Building) - continue; + vector threads(thread::hardware_concurrency()); - size_t const t = static_cast(Type::Street); + for (size_t t = 0; t < threads.size(); ++t) + { + threads[t] = thread([&, t, this]() { + size_t const size = m_docs.size() / threads.size(); + size_t docId = t * size; + size_t const docIdEnd = (t + 1 == threads.size() ? m_docs.size() : docId + size); - ForEachDocId(buildingDoc.m_address[t], [&](DocId const & streetCandidate) { - auto const & streetDoc = GetDoc(streetCandidate); - if (streetDoc.IsParentTo(buildingDoc)) + for (; docId < docIdEnd; ++docId) { - m_buildingsOnStreet[streetCandidate].emplace_back(docId); + auto const & buildingDoc = GetDoc(docId); - ++numIndexed; - if (numIndexed % kLogBatch == 0) - LOG(LINFO, ("Indexed", numIndexed, "houses")); + if (buildingDoc.m_type != Type::Building) + continue; + + size_t const streetType = static_cast(Type::Street); + + ForEachDocId(buildingDoc.m_address[streetType], [&](DocId const & streetCandidate) { + auto const & streetDoc = GetDoc(streetCandidate); + + if (streetDoc.IsParentTo(buildingDoc)) + { + auto && lock = lock_guard(mutex); + m_buildingsOnStreet[streetCandidate].emplace_back(docId); + } + }); + + auto processedCount = numIndexed.fetch_add(1) + 1; + if (processedCount % kLogBatch == 0) + LOG(LINFO, ("Indexed", processedCount, "houses")); } }); } + for (auto & t : threads) + t.join(); + if (numIndexed % kLogBatch != 0) LOG(LINFO, ("Indexed", numIndexed, "houses")); } -- cgit v1.2.3