From e51a4734418ecff3131419638654df1a126001fb Mon Sep 17 00:00:00 2001 From: dktue <2878623+dktue@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:52:41 +0100 Subject: [PATCH 1/3] fix(store): 12x faster --store under memory pressure, 67x less disk writes Sorting output objects scattered writes into the mmap'd store files, so the kernel rewrote the same dirty pages over and over. Germany with an 8 GB limit: 69+ min (1.3 TB written) -> 5:36 (20 GB written), identical tile content. Scatter into the existing heap buffer and copy back sequentially instead. Also release store file handles after mapping, so many-threaded --store runs no longer abort with 'Failed to open mmap file'. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/mmap_allocator.cpp | 6 +++++- src/tile_sorting.cpp | 24 +++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) 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/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]; }); } From 5ff4422c851d48ab8b621dc931a39365c2c9840c Mon Sep 17 00:00:00 2001 From: dktue <2878623+dktue@users.noreply.github.com> Date: Wed, 28 Oct 2026 18:27:09 +0100 Subject: [PATCH 2/3] perf(store): 3.2% less CPU with --store via batched node lookups Gain: callgrind on Luxembourg --store drops from 125.7 to 121.7 G instructions (-3.2%); output identical (all 273,080 Germany tiles). ShardedNodeStore didn't override the batched at(ids, n, out) from #939, so --store looked up every node of every way on its own plus a contains() check, the top entry in a --store profile. Now the whole batch is looked up in the shard of its first node, with per-node lookups only for ways crossing a shard boundary. Co-Authored-By: Claude Opus 5.5 (1M context) --- include/sharded_node_store.h | 1 + src/sharded_node_store.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) 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/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) From f4eddf9b83d3d3cb3915086a4fc6f13145e0a2b0 Mon Sep 17 00:00:00 2001 From: dktue <2878623+dktue@users.noreply.github.com> Date: Wed, 28 Oct 2026 19:09:00 +0100 Subject: [PATCH 3/3] fix(store): don't drop relations crossing the extract boundary (--store output now matches in-memory) With shards, a relation was read only in the pass for the shard of its first member way; if that way wasn't in the extract, every pass skipped it. Germany --store vs in-memory: 119 missing tiles and ~5,700 missing features (Szczecin Lagoon, Usedom, border nature reserves) -> 0 differences in 273,199 tiles. Decide the shard by the first member way that is in the store; relations with no stored member ways are read in the first pass. Bug predates #939. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/pbf_processor.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) 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 {