diff --git a/include/sharded_node_store.h b/include/sharded_node_store.h index 836c34ef..5d62ce23 100644 --- a/include/sharded_node_store.h +++ b/include/sharded_node_store.h @@ -12,6 +12,7 @@ class ShardedNodeStore : public NodeStore { void reopen() override; void finalize(size_t threadNum) override; LatpLon at(NodeID i) const override; + void at(const NodeID* ids, size_t n, LatpLon* out) const override; size_t size() const override; void batchStart() override; void insert(const std::vector& elements) override; diff --git a/src/mmap_allocator.cpp b/src/mmap_allocator.cpp index a68808a7..d7831c29 100644 --- a/src/mmap_allocator.cpp +++ b/src/mmap_allocator.cpp @@ -104,7 +104,11 @@ mmap_file::mmap_file(std::string const &filename, std::size_t offset) , mapping(filename.c_str(), boost::interprocess::read_write) , region(mapping, boost::interprocess::read_write) , buffer(boost::interprocess::create_only, reinterpret_cast(region.get_address()) + offset, region.get_size() - offset) -{ } +{ + // The mapped region stays valid without the file handle. Release it, so large --store runs + // with many threads (one ~1 GB file each, more as they fill) don't exhaust the open-files limit. + mapping = boost::interprocess::file_mapping(); +} mmap_file::~mmap_file() { diff --git a/src/pbf_processor.cpp b/src/pbf_processor.cpp index a602f6e0..93d702bf 100644 --- a/src/pbf_processor.cpp +++ b/src/pbf_processor.cpp @@ -289,6 +289,17 @@ bool PbfProcessor::ReadRelations( TagMap tags; std::vector relations; + // Passes skip shards with no ways, so the first pass is for the first non-empty shard + uint firstWayShard = 0; + while (firstWayShard + 1 < effectiveShards && osmStore.ways.shard(firstWayShard).size() == 0) + firstWayShard++; + auto wayInOtherShard = [&](WayID wayId, uint thisShard) { + for (uint s = 0; s < effectiveShards; s++) + if (s != thisShard && osmStore.ways.contains(s, wayId)) + return true; + return false; + }; + int typeKey = findStringPosition(pb, "type"); int mpKey = findStringPosition(pb, "multipolygon"); int boundaryKey = findStringPosition(pb, "boundary"); @@ -309,7 +320,11 @@ bool PbfProcessor::ReadRelations( WayVec outerWayVec, innerWayVec; bool isInnerOuter = isBoundary || isMultiPolygon; bool skipToNext = false; - bool firstWay = true; + // With shards, a relation is read in the pass for the shard of its first member way + // that is in the store. Member ways missing from the extract don't decide the shard, + // so relations crossing the extract boundary aren't dropped; a relation with no stored + // member ways is read in the first pass. + bool shardDecided = effectiveShards <= 1; for (int n = 0; n < pbfRelation.memids.size(); n++) { uint64_t lastID = pbfRelation.memids[n]; if (pbfRelation.types[n] != PbfReader::Relation::MemberType::WAY) { continue; } @@ -317,16 +332,18 @@ bool PbfProcessor::ReadRelations( if (role==innerKey || role==outerKey) isInnerOuter=true; WayID wayId = static_cast(lastID); - if (firstWay && effectiveShards > 1 && !osmStore.ways.contains(shard, wayId)) { - skipToNext = true; - break; + if (!shardDecided) { + if (osmStore.ways.contains(shard, wayId)) { + shardDecided = true; + } else if (wayInOtherShard(wayId, shard)) { + skipToNext = true; + break; + } } - if (firstWay) - firstWay = false; (role == innerKey ? innerWayVec : outerWayVec).push_back(wayId); } - if (skipToNext) + if (skipToNext || (!shardDecided && shard != firstWayShard)) continue; try { diff --git a/src/sharded_node_store.cpp b/src/sharded_node_store.cpp index 0d915fbd..ac2f13b6 100644 --- a/src/sharded_node_store.cpp +++ b/src/sharded_node_store.cpp @@ -1,4 +1,5 @@ #include "sharded_node_store.h" +#include thread_local size_t lastNodeShard = 0; @@ -35,6 +36,29 @@ LatpLon ShardedNodeStore::at(NodeID id) const { return stores[shards() - 1]->at(id); } +void ShardedNodeStore::at(const NodeID* ids, size_t n, LatpLon* out) const { + if (n == 0) + return; + + // Ways rarely cross shards: look up the whole batch in the shard of its first node, + // and only fall back to per-node lookups if one of the nodes is elsewhere. + for (int i = 0; i < shards(); i++) { + size_t index = (lastNodeShard + i) % shards(); + + if (stores[index]->contains(0, ids[0])) { + lastNodeShard = index; + try { + stores[index]->at(ids, n, out); + return; + } catch (std::out_of_range &) { + break; + } + } + } + + NodeStore::at(ids, n, out); +} + size_t ShardedNodeStore::size() const { size_t rv = 0; for (auto& store : stores) diff --git a/src/tile_sorting.cpp b/src/tile_sorting.cpp index 0d5cb839..64588163 100644 --- a/src/tile_sorting.cpp +++ b/src/tile_sorting.cpp @@ -140,9 +140,10 @@ template void sortOutputObjects( return; } - // Parallel counting sort on the key: each chunk copies its objects - // into a contiguous buffer and counts its keys, then scatters them - // back into their final positions. + // Parallel counting sort on the key: each chunk counts its keys, then + // scatters its objects into their final positions in a buffer, which is + // copied back in order. Writing the objects (which can live in the + // --store files) sequentially means each page is dirtied only once. const size_t chunks = std::max(1, std::min(threadNum, n / countingSortMinSize)); const size_t chunkSize = (n + chunks - 1) / chunks; // OO has no default constructor, so use raw storage for the copies. @@ -165,10 +166,8 @@ template void sortOutputObjects( runChunks([&](size_t c, size_t from, size_t to) { size_t* counts = &offsets[c * keyValues]; auto it = begin + from; - for (size_t i = from; i < to; i++, ++it) { - new (&buffer[i]) OO(*it); - counts[key(buffer[i])]++; - } + for (size_t i = from; i < to; i++, ++it) + counts[key(*it)]++; }); // Turn counts into starting positions: by key, then by chunk. @@ -183,8 +182,15 @@ template void sortOutputObjects( runChunks([&](size_t c, size_t from, size_t to) { size_t* next = &offsets[c * keyValues]; - for (size_t i = from; i < to; i++) - begin[next[key(buffer[i])]++] = buffer[i]; + auto it = begin + from; + for (size_t i = from; i < to; i++, ++it) + new (&buffer[next[key(*it)]++]) OO(*it); + }); + + runChunks([&](size_t c, size_t from, size_t to) { + auto it = begin + from; + for (size_t i = from; i < to; i++, ++it) + *it = buffer[i]; }); }