Repository navigation
Conversation
systemed#983 (combined snapshot) For testing only, not for merging. One commit with the combined tree that built the planet in 14 min 03 s (in memory, 96 threads) for systemed#977: master 2789651 + systemed#933, systemed#959, systemed#966, systemed#968, systemed#970, systemed#971, systemed#972, systemed#973, systemed#974, systemed#975, systemed#976, systemed#978, systemed#979 (open performance PRs), systemed#981 (deterministic tile content), systemed#942 (deterministic POI order) and systemed#983 (ordered .pmtiles/.mbtiles writer, 64 MB per thread). Each change is reviewed in its own PR. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Same change as systemed#986. With it, the sort before the tile phase takes ~13 s instead of 2-5 minutes at 96 threads, so planet times no longer vary by minutes between runs (14:12 / 14:08 in memory at 96 threads). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
end() woke every thread waiting in waitForRoom() after each batch it wrote, although most of the time the held tiles were still over the limit. At 96 threads that meant dozens of threads waking, finding no room and sleeping again for every batch: 125M context switches and 7,700 s of system time on the planet. Now they are only woken once the held tiles are back under the limit. Planet in memory, 96 threads, all open performance PRs: 14:05 -> 12:45 (-9.5%), system time 7,708 -> 2,868 s, context switches 125M -> 35M. Output unchanged (same batches, same order). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Every tile batch that needs a large polygon copied it from the store and clipped it from scratch, at every zoom. Now polygons with at least 10,000 points are clipped once per z8 tile, before the tiles are written (all threads, largest polygon first), and tiles below clip from that piece. After that the pieces are only read, so there is no lock while tiles are written; a z6 tile's pieces are freed once no tile in it is still to come. A piece only depends on the polygon and the z8 tile, so the output stays the same for any thread count. A piece whose clip needed the repair path is not used: once one came out empty and a lake vanished from all tiles below. Planet in memory, 96 threads, all open performance PRs + systemed#983's wake-up fix: 12:45 -> 10:55 (-14.4%), user CPU -27%, peak RSS +7.5 GB. Germany byte-identical at 32 and 8 threads; planet sample: same feature counts, area within 0.003%. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
At 96 threads the one thread that writes batches in order became the bottleneck of the tile phase: busy 260 s of a 323 s tile phase, with the others waiting for room for 5,100 thread-seconds. Writing the bytes was more than half of its work (ofstream::write, and a tellp() per tile). - The writer keeps its own tile data offset instead of calling tellp() for every tile, and the stream gets a 4 MB buffer. - OrderedBatches can take a second step, flushItem: also in batch order and one thread at a time, but run by whichever thread is free, so it doesn't hold up the first step. PMTiles places tiles (offset, repeated tiny tiles, index) in the first step and writes their bytes in the second. The file is byte-identical. Planet in memory, 96 threads, all open performance PRs: 10:41 -> 9:54 (-7.3%); tile phase 323 -> 245 s, threads busy 76 -> 94 of 96 on average. New test: the second step runs in order, never twice at once, and always after the first (also clean under TSan). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The pieces used to be computed in a pass of their own before the tile phase, which ended with one thread on the largest piece (~6 s on the planet) while all others waited. Now the tile threads compute them before their first tile, largest polygon first, and go on to tiles once no piece is left to start; a tile that needs a piece still being computed waits for just that one (shared_future). A piece is known before the tiles start, so the map doesn't change and tiles look pieces up without a lock as before. Output byte-identical (Germany at 32 and 8 threads). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This was referenced Nov 15, 2026
- polylabel.h: rename edgeDistBound's parameter `near` to `nearCell`. tile_data.h includes boost/asio since systemed#972, which pulls in windows.h on Windows, and windows.h defines `near` as an empty macro. - test/shp_mem_tiles.test.cpp (systemed#979): include shp_mem_tiles.h before minunit.h. minunit.h sets _POSIX_C_SOURCE, which on macOS hides ip_mreq, and boost/asio (through tile_data.h) needs it. Renames and include order only; output unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dktue
force-pushed
the
perf/planet-3min-08-memory-exit
branch
from
November 15, 2026 13:38
393a6d3 to
bc4b9de
Compare
…uctors have run (macOS) On macOS the main thread's thread-local destructors run after the static destructors. Two of them touched objects that were already gone, and the unit tests aborted at exit with "mutex lock failed: Invalid argument": - mmap_allocator.cpp: thread_regions_t's destructor locks mmap_allocator_mutex and appends to free_mmap_files / free_mmap_shm_regions. These three are now allocated once and never destroyed. ~mmap_dir_t clears free_mmap_files first, so --store files are still released and deleted as before (that list used to be destroyed just before mmap_dir). - LeasedStore (tile_data.cpp): a thread gives its store leases back to the TileDataSource when it ends; the main thread does that at exit, after the sources (locals of main(), or of a unit test such as test_shp_mem_tiles) are destroyed, locking a destroyed storeMutex. The sources are now registered while alive (an immortal registry), and a lease goes back only to a live source. Sources are also numbered: a new source at the address of a destroyed one no longer gets that one's lease. No change in output. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dktue
force-pushed
the
perf/planet-3min-08-memory-exit
branch
from
November 15, 2026 15:21
bc4b9de to
95f20ad
Compare
cldellow
added a commit
to cldellow/tilemaker
that referenced
this pull request
Nov 15, 2026
…s-b36pcl dktue's 3-minute planet series (systemed#993–systemed#1000) on relslow-patched; Lua 5.4 and jemalloc in the image
dktue
force-pushed
the
perf/planet-3min-08-memory-exit
branch
from
November 16, 2026 10:46
95f20ad to
2da233a
Compare
…+ systemed#990) systemed#989 and systemed#990 adapted to perf/shared-pieces, as the base of this series. - SortedNodeStore/SortedWayStore::finalize publish the leftover groups (groups split between worker batches) from all threads instead of one after the other. On the planet that was ~10 s after the ways pass with every other thread idle. Same store content. (systemed#989, unchanged) - PMTiles::close collects the directory entries and cuts them into leaves exactly as before, then serializes and compresses the leaves on all cores and writes them in order: ~9 s -> 3 s on the planet, byte-identical file. (systemed#990; on perf/shared-pieces the two-step writer already holds the file, so the leaf writes need no lock and fileMutex goes.) Planet (in memory, 96 threads): -9 s and -6 s. Output byte-identical to perf/shared-pieces. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…atorArea Most ways run exactly the same Lua as many ways before them: the same tag reads give the same results and the same calls. With `way_memo = true` in the profile, tilemaker records, per thread, a tree of what way_function read (Find, Holds, IsClosed, NextRelation, ...) and which tilemaker functions it then called (Layer, Attribute..., MinZoom), and replays the calls for a way whose reads give the same results, without entering Lua. Ways with anything else (geometry queries, Id, relation tags, globals) run through Lua as before. - Opt-in declarations, documented in CONFIGURATION.md: way_memo_values (tags only tested for being empty and copied, e.g. name; replayed with the way's own value, optionally its length), way_memo_area (MercatorArea only compared with given numbers), way_memo_relations (NextRelation only tested, relation tags only via FindInRelation), and node_memo* for node_function. Multipolygon relations get a record of their own. - The walk reads the way's tags once into small key slots (memo_keys.h); TagMap builds its hash only when Lua asks for a tag. - Bounded: 2^18 nodes per thread, at most 256 children per node, full trees are compacted; a value-heavy branch (a name, a house number) falls back to Lua. - New Lua functions MercatorArea() (planar Web Mercator area) and MinZoomByArea(limit, thresholds): the minimum-zoom-by-area rule worked out in C++, so that way_function stays replayable. - The bundled OpenMapTiles profile uses all of this, and keeps tilemaker's functions in locals. - The Makefile compiles as C++17, like CMake since systemed#953 (memo_keys.h uses std::string_view). Works with Lua 5.1/LuaJIT too (lua_objlen where luaL_len is missing). Output: the only change in this series. The old profile computed MercatorArea in Lua as Area() / cos^2(latitude of the centroid); the native function is the planar area on the Web Mercator plane. Areas close to a minimum-zoom threshold can land on the other side of it, so some landuse, water and building polygons start one zoom earlier or later. Everything else is identical; the memo itself does not change the output. Planet (96 threads): way_function replayed for ~1.17 billion of 1.22 billion ways, node_function for ~211 million nodes; together with the native area about -21% wall. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…erts) Exact speed-ups of reading the .pbf and filling the stores and indexes; output byte-identical. - PBF: packed varints decoded in one SWAR pass; latitude to latp through a polynomial with an exact fallback (checked against the old function for every E7 latitude). - Node/way stores: chunks decoded lazily with a fused SSSE3 decoder, uncompressed chunks kept in the per-thread cache, ways decoded in one pass, a small linestring cache; leftover way groups are moved, not copied, at finalize. Node/way block batches handed out guided. - Relations: UsedWays collects the member ways per thread (a global lock per member way was the main kernel spin-lock cost) and is finalized into a bitmap on all threads (atomic OR; _InterlockedOr64 on MSVC); a thread's list is found by the UsedWays' own number, not its address (a new UsedWays at the address of a destroyed one picked up the old list - seen in the unit test). RelationScanStore finds a way's relations by a hash index, then by sorted shard arrays with a per-reader cursor. Heavy relation blocks go first and the last blocks are split, so the pass doesn't end on one thread. Multipolygon rings are joined in one buffer (no deque, no std::map of done ways), reading each way's coordinates once. - Tags and attributes: TagMap compares keys by an 8-byte prefix; DequeMap gets an open-addressing hash index, hashed once per lookup; the hot attribute-pair shard is read without its lock once it is full. - Output index: small-index adds are staged per thread and appended per shard in bulk (one lock per z6 tile run instead of one per tile); the tiles of a linestring are found faster. - Memory: way pieces are handed to the orphanage in a vector of their own size; the thread's buffer keeps its capacity for the next pieces. Planet (96 threads): about -18% wall over the rounds (e.g. UsedWays -21 s, node decode -18 s, attribute store -9 s). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…e_valid Boost's is_valid and the repair (make_valid) were the largest geometry cost left, both while reading (relations, ways) and in the tile phase (clipped polygons). Output byte-identical. - Precheck (geom.cpp), rounds 1-4: a conservative test that answers "certainly valid" or "unsure" (then Boost decides, as before): bucketed segment sweeps instead of sorting, one-pass ring checks, monotone chunks, hole placement by crossing parity, orientation shortcuts. An "AfterClip" variant for rect_clip results uses Boost's own side test (rounding bound, no distance tolerance) and decides near-spikes the way Boost does. Verified with a build that also ran Boost on every "certainly valid": 1.5 billion planet calls, 0 mismatches. - The precheck now also runs before Boost in writeMultiPolygon and in the repair's acceptance test; a linestring with two distinct finite points is known valid without is_valid. - ring_point_index.h: point-in-ring through a y-band segment index for big rings, used inside Boost's validity and relate algorithms (Lake Huron r1205151: 16.6 s -> 1.7 s). - make_valid (correct.hpp): self-intersections found with an index (rtree fallback), adjacent pairs handled without Boost, rings clearly apart never intersected, results moved instead of copied; correct.hpp now includes geom.h, and the unit test links geom.o. Works with Boost before and after 1.87, which renamed segment_intersection_info's robust_ra/robust_rb to ra/rb. - Repair: an orientation-only fault is fixed by reversing in place, also for polygons with holes (exact for integer tile coordinates). - Relations with >= 100k points that still need is_valid are checked in the background until the tiles start; a relation that needs no correction is checked in the cache and not copied. Planet (96 threads): about -16% wall over the rounds (e.g. ring index -9 s, precheck round 1 -12 s, deferred validity -11 s). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Exact speed-ups of the per-tile geometry work; output byte-identical. - Clipping: fast_clip computes the outcodes with SSE2 and reuses scratch buffers; a copy-free path when nothing is cut; the end-zoom box; scaleRing with packed divisions; rect_clip finds the holes a cut crosses through a y-index. Lines: split_linestring_for_tile skips work for segments clearly inside, clip_line.h clips and scales in one go (scaleLine), and the linestring clip code shared by OSM and shapefile objects moved there. - Covered z12 subtrees: when every object of a z12 tile, including those with minzoom 13/14, is a polygon that clips to exactly the tile box, the tiles below it are all alike, so the first computed tile of each zoom is reused for the rest (9.4M z12 tiles on the planet). - Visvalingam: the heap rewritten (packed keys, in place); the tie order is kept exactly, since it decides the output. - polylabel: a small-polygon path, a block tree of the edges and an AVX2 distance kernel chosen at run time (no FMA, so results are unchanged); the AVX2 code now sits under the same guard as the SSE2 code, which fixes builds with -march=x86-64-v3/native. The kernels are GCC/Clang only (target attributes, __builtin_cpu_supports); MSVC takes the scalar path, with the same results. Planet (96 threads): about -8% wall over the rounds (clipping -9 s, polylabel -5 s, covered subtrees -5 s). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Exact speed-ups of building and encoding each tile; output byte-identical. - tile_encode.h: features are encoded straight into the layer (vtzero builder extensions), empty layers are skipped, layer builders are recycled between tiles, and attribute pairs are resolved once per set. - Per-tile sorting: 128-bit sort keys; above 64 objects, attribute sets are sorted by rank instead of comparing their pairs; a canonical pair order is cached. - Object lookup: the large-object rtree is queried once per z12 tile and reused for its children, the small index is binary-searched, and covered subtrees are found by a cheap pre-scan. Planet (96 threads): about -7% wall (-14 s and -6 s in two rounds), user CPU -5.5% in the first. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Fewer idle cores before and during the tile phase; output byte-identical (the batches, their order and the clip cache scope are unchanged). - Tile list: filtered in parallel column stripes (set bits found with a small portable helper, _BitScanForward64 on MSVC), sorted as 64-bit keys with sample_sort on all threads (systemed#986 v2; called from tile_sorting.cpp, as boost/sort in tilemaker.cpp clashes with Boost.Range on MSVC), its storage touched in parallel; low zooms collected in the counting pass; batch starts computed in parallel. - finalizeObjects: big shards run concurrently in the pool with threads in proportion to their share, sort buffers reused per thread, budget raised to half the objects. - Hand-out: batches of z<=8 first; consecutive batches handed out as a group (OrderedBatches begin/end per group, group size adapted to the batch count); the groups expected to be slowest (z12 object counts gathered while collecting) handed out up to 5% ahead; idle threads take over the units of a running group (tile stealing). - No deadlock with few threads: groups handed out ahead of their place (z0-z8, slow groups) could fill the held-tile limit while the group to be written next had not been handed out, so every thread waited for room (seen on the planet at 16 threads). OrderedBatches now knows the first group not handed out yet (setFirstUnhanded), and when that group holds the next unit to write, waitForRoom lets one thread go on and take it, even over the limit. - Held-tile limit by memory: threads may run ahead by 1/16 of the memory (physical RAM, or the cgroup v2/v1 limit if lower), at least 64 MB per thread as before and at most 4 GB; 64 MB per thread where the memory isn't known (e.g. Windows). With 64 MB per thread only, a 16-thread planet with --store on 64 GB spent minutes with one thread on a slow multipolygon and the others waiting for room: 54 min -> 25 min. - Shapefiles/GeoJSON are read alongside the .pbf when no layer is indexed from them. - compress_string returns a string of exactly the compressed size (it kept the compression bound as capacity), so held tiles take what is counted. - Memory: the tile list is allocated and filled after the key sort, no longer alongside the list parts, the keys and the sort buffer. - Memory: the tile threads give back (MADV_DONTNEED, Linux) the pages of the tile list before the first batch not written yet, in 64 MB steps (OrderedBatches::nextToWrite); the progress line reads its tile first. - Memory: releaseSharedPieces returns the bytes it dropped, and once 256 MB has been released the tile threads call malloc_trim(0) outside batchMutex (glibc), as the freed pieces sat in the middle of the arenas. - Memory: finalize releases a sort buffer of 64 MB or more after its shard, rather than every thread keeping one of the biggest shards' size. Planet (96 threads): about -11% wall over the rounds (e.g. batch groups -12 s, parallel tile list -19 s). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- On Linux the tile data is written with O_DIRECT from 32 MB aligned buffers by 4 writer threads (64 buffers); a single thread writing through the page cache managed ~1.2 GB/s, all of it kernel time, and was the tail of the tile phase. The unaligned rest, the header and the directories are written as before. Falls back to normal writes where O_DIRECT isn't available, or when TM_NO_DIRECT is set. - close(): the dense index is turned into directory entries in parallel, and the leaves are built in parallel with the same split rule. Byte-identical file. - Memory: close() frees the dense index and the chunks on a thread of their own as soon as they are used up; the writer reuses the buffer freed last, so idle buffers aren't touched. - Memory: the dense index is kept in blocks of 16K ids, allocated when their first tile is placed, rather than one vector up to the highest id so far; close() skips missing blocks. - Memory: beyond 4 spare buffers, a writer buffer's pages are given back (MADV_DONTNEED) after its pwrite, so a burst doesn't stay resident. - New setting compression_level (1 = fastest .. 12 = smallest, default 6 = unchanged output). On the planet, 1 gave a 1.8% larger file and a ~3% shorter run. Planet (96 threads): direct I/O -13 s (3:59 was the first run under 4 minutes), parallel leaves -2 s. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The last seconds of a big run were spent freeing memory, and a large share of system time on page faults. - Once the output is complete (and without --store, whose files are still deleted by the destructors), tilemaker no longer runs the destructors. A process sharing its memory (clone with CLONE_VM) holds on to it, so tilemaker's own exit doesn't wait for the kernel; that process closes the inherited files, waits until tilemaker is gone and exits, and the kernel frees the memory in the background. Where that isn't possible, the big anonymous mappings are released with MADV_DONTNEED from several threads before _Exit. --no-early-exit keeps the release in the foreground. Linux only; elsewhere tilemaker exits with _Exit after the output is written. - The node and way stores' regions get madvise(MADV_HUGEPAGE), so they use transparent huge pages where the system allows it (THP "madvise" or "always"); TM_NO_HUGEPAGES turns it off. - glibc only: malloc_trim(0) after the reading phase and after the tiles are built, so the freed arena pages go back before the next phase. - RUNNING.md describes both, and running with jemalloc (LD_PRELOAD + MALLOC_CONF=thp:always,metadata_thp:always). Planet (96 threads): -12 s (no destructors), -4 s (early exit), -1.5 s and ~25% less system time (huge pages, with jemalloc's THP settings). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dktue
force-pushed
the
perf/planet-3min-08-memory-exit
branch
from
November 16, 2026 12:27
2da233a to
b6f49ad
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Gain: planet (96 threads) about -5% wall for this PR: no destructors at exit 9:34.5 -> 9:23 (-11.5 s), early exit -3.7 s, huge pages -1.5 s with ~25% less system time (run with jemalloc's THP settings; measured on the full stack). Memory, with the memory changes of #994, #998, #999 and this PR: planet with
--storeon 8 threads peaks at 15.9 GB instead of 24.4 GB (-35%), Europe at 6.0 instead of 10.1 GB.Series total: planet (in memory, 96 threads) about 9:50 for perf/shared-pieces + #989/#990 (Nov 8; glibc, Boost 1.74, GCC 11, Lua 5.3) -> 3:08 for the last commit (Nov 14; jemalloc with THP settings, Boost 1.83, GCC 14, Lua 5.4), -68%. Compounding the per-round A/B measurements, the code of this series accounts for about -63%; the newer toolchain (about -8%) and jemalloc (about -8%) for the rest.
Stacked on
perf/shared-pieces(#1001) and the previous PR of this series (perf/planet-3min-07-pmtiles-writer), so the diff of this PR on GitHub also shows the commits below it; review the last commit. The whole series is onperf/planet-3min.What
The last seconds of a big run were spent freeing memory, and a large share of system time on page faults.
Output
Byte-identical output: it only changes what happens after the file is complete, how memory is backed and when it is given back.
Verification
make test, 18 suites, 0 failures). CI passes on Linux (Makefile with LuaJIT, CMake), macOS (macos-latest) and Windows; macos-14 fails on every PR since GitHub retired that runner (ci: replace the retired macos-14 runner with macos-15 #1002); locally also checked with LuaJIT 2.1 and Boost 1.87 headers.--store, 8 threads, 32 GB cgroup, peak anonymous RSS): planet 24.37 GB before the memory changes -> 19.75 GB -> 15.94 GB with all of them, the last two runs giving files of the same size to the byte (76,632,120,334); Europe 10.14 -> 6.00 GB and Netherlands 1.34 -> 0.64 GB, both byte-identical; the four extracts at 1, 4 and 16 threads byte-identical. On the planet the peak is now in the first half of the tile phase (about 16 GB), and falls to about 10 GB by the time the file is closed; reading stays at 4-7 GB.🤖 Generated with Claude Code