Repository navigation
Conversation
CI results (PR runs against
|
|
Colin thoughts: I like this because it's simple and generally usable in many places. I dislike it because it still leaves a lot of performance on the table. I wonder if there's a heuristic we could use to know earlier that a query will be re-reading its inputs? More exotic: could we have something akin to a peephole optimizer that rewrites the queries into a format that is less likely to do multiple reads? Assumption: part of the waste is inherent in how the input sets are written, i.e. as a union of multiple statements instead of as a single statement with a suitable filter expression. |
|
Testing the assumption that the waste comes from writing the input as a union of statements instead of one statement with a suitable filter. I rewrote p04 (the prompting query, defined in the description) in four equivalent ways and ran each on three builds:
What this shows:
Knowing early that a query will re-read: yes, from the parsed query, before execution. A union with two or more query statements sharing an identical The rewritten queries
Generated by Claude Code |
cf4564c to
0bedb95
Compare
A union of query statements of one type with the same filters apart
from tag filters, such as node statements around the same point with
different tags, reads and decompresses the same blocks once per
statement. While such a union runs, each block read from a compressed
read-only file is kept decompressed, and later reads copy it instead of
decompressing it again. Other queries keep nothing.
The cache belongs to the transaction, which gives it to the indexes of
its read-only files, so it lives no longer than the snapshot it was read
from, and flush() empties it. It keeps only a block's used bytes, holds
at most a quarter of the query's [maxsize:] (128 MiB by default) per
transaction, and evicts nothing. Like other auxiliary structures it
isn't counted toward [maxsize:]; it stays within the headroom of the
process memory limit, 2 x maxsize + 1 GiB. Whether a union qualifies
only decides what is kept; results never depend on it.
Geofabrik Nova Scotia extract (2026-11-08) with the area rules run,
warm cache, 4-core VM, 15 alternating pairs: CPU time 28% lower, peak
memory 19 -> 51 MB:
[out:json][timeout:45]; (node["tourism"](around:25000,44.6488,-63.5752);
way["tourism"](around:25000,44.6488,-63.5752);
node["amenity"~"restaurant|cafe|bar|pub|fast_food|ice_cream"](around:25000,44.6488,-63.5752);
way["amenity"~"restaurant|cafe|bar|pub"](around:25000,44.6488,-63.5752);
node["leisure"~"park|garden|stadium|sports_centre|marina|golf_course"](around:25000,44.6488,-63.5752);
way["leisure"~"park|garden|stadium|nature_reserve"](around:25000,44.6488,-63.5752);
node["historic"](around:25000,44.6488,-63.5752);
way["historic"](around:25000,44.6488,-63.5752);
node["natural"~"peak|beach|waterfall"](around:25000,44.6488,-63.5752);
); out body center 500;
Co-Authored-By: Colin Dellow <cldellow@gmail.com>
0bedb95 to
47de27f
Compare
Fixes #166.
Change
A query that unions several statements over the same area decompresses each data block once per statement. Point-of-interest lookups, the main load on the public servers, have this shape. The prompting query p04 decompresses
nodes.binblocks 1,709 times for 263 distinct blocks, and LZ4 decompression takes 49% of its CPU time.While a union that will re-read blocks runs, each block it decompresses is kept, and later reads copy it instead of decompressing it again. Three commits:
Block_Cache(template_db/file_blocks_index.h, used byFile_Blocks::read_block_intemplate_db/file_blocks.h). The read-only file index owns a cache of decompressed blocks by block position:[maxsize:].check_idx == false; they don't start with a size).Union_Statement::executechecks whether two or more of its query statements have the sameQuery_Statement::block_signature(), their type plus their filters other than tag filters rendered as QL (e.g.node(around:25000,44.6488,-63.5752)). Such statements read the same blocks. While that union runs,Block_Cache::rereads_expected()is raised. Statements of different types read different files, so a union of a node, a way and a relation statement over one bbox doesn't count.rereads_expected()is raised, from their first read. Blocks kept during the union still serve later reads, e.g. byout center. Every other query runs as before.About 100 lines in five files. No change to the query API or the on-disk format.
How the policy was chosen
The first commit alone kept a block on its third read in every query. That gave p04 −20% against master, but made p06 and t04 4–8% slower on
all-changes, p10 about 4% slower against master, and used up to 32 MiB on queries that gain nothing. Caching on the second read everywhere made q06, p08 and t01 9–13% slower. Inside detected unions, caching from the first read beats caching from the second (p04 −14% vs −5% against the first commit). Leaving the type out of the signature flagged unions such as t10 (a node, a way and a relation statement over one bbox) that never re-read, filling the cache for nothing. The 2nd and 3rd commits are those findings.Impact
Geofabrik Nova Scotia extract imported with
--meta, LZ4 compression, page cache warm, 4-core cloud VM.bench/compare.py --paired --reps 25 --rss: CPU time (user+system) per query, 25 alternating pairs. Baseline:upstream/master(8edf11b); candidate: this PR (cf4564c). Output is identical for every query.Point-of-interest set (
bench/queries-poi, prompting query p04)The POI queries, run on the plain database except p07, which needs areas:
[out:json][timeout:30]; (node["amenity"="school"](around:3000,44.6488,-63.5752); way["amenity"="school"](around:3000,44.6488,-63.5752); node["amenity"="kindergarten"](around:3000,44.6488,-63.5752); node["amenity"="hospital"](around:5000,44.6488,-63.5752); way["amenity"="hospital"](around:5000,44.6488,-63.5752); node["amenity"="pharmacy"](around:3000,44.6488,-63.5752); node["shop"~"supermarket|convenience|mall"](around:2000,44.6488,-63.5752); way["shop"~"supermarket|convenience|mall"](around:2000,44.6488,-63.5752); node["highway"="bus_stop"](around:1500,44.6488,-63.5752); way["leisure"="park"](around:2000,44.6488,-63.5752); way["highway"~"primary|secondary|tertiary"](around:2000,44.6488,-63.5752); way["highway"~"residential|unclassified|service"](around:500,44.6488,-63.5752); way["landuse"="industrial"](around:3000,44.6488,-63.5752); node["amenity"="fuel"](around:3000,44.6488,-63.5752); ); out center body;[out:json][timeout:25]; (node["amenity"~"fuel|cafe"]["brand"~"tim hortons|irving|esso|shell|petro-canada|ultramar|circle k|needs|starbucks",i](44.45,-63.85,44.85,-63.35); node["amenity"~"fuel|cafe"]["name"~"tim hortons|irving|esso|shell|petro-canada|ultramar|circle k|needs|starbucks",i](44.45,-63.85,44.85,-63.35); way["amenity"~"fuel|cafe"]["brand"~"tim hortons|irving|esso|shell|petro-canada|ultramar|circle k|needs|starbucks",i](44.45,-63.85,44.85,-63.35); node["shop"~"supermarket|convenience"]["brand"~"sobeys|superstore|atlantic superstore|walmart|lawtons|shoppers drug mart|foodland",i](44.45,-63.85,44.85,-63.35); way["shop"~"supermarket|convenience"]["brand"~"sobeys|superstore|atlantic superstore|walmart|lawtons|shoppers drug mart|foodland",i](44.45,-63.85,44.85,-63.35); ); out center tags;[out:json][timeout:300]; (node["amenity"="fuel"]; way["amenity"="fuel"]; ); out center tags;[out:json][timeout:45]; (node["tourism"](around:25000,44.6488,-63.5752); way["tourism"](around:25000,44.6488,-63.5752); node["amenity"~"restaurant|cafe|bar|pub|fast_food|ice_cream"](around:25000,44.6488,-63.5752); way["amenity"~"restaurant|cafe|bar|pub"](around:25000,44.6488,-63.5752); node["leisure"~"park|garden|stadium|sports_centre|marina|golf_course"](around:25000,44.6488,-63.5752); way["leisure"~"park|garden|stadium|nature_reserve"](around:25000,44.6488,-63.5752); node["historic"](around:25000,44.6488,-63.5752); way["historic"](around:25000,44.6488,-63.5752); node["natural"~"peak|beach|waterfall"](around:25000,44.6488,-63.5752); ); out body center 500;[out:json][timeout:15]; (node["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school)$"](around:4828,44.6488,-63.5752); way["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school)$"](around:4828,44.6488,-63.5752); node["shop"~"^(supermarket|convenience|bakery)$"](around:4828,44.6488,-63.5752); way["shop"~"^(supermarket|convenience|bakery)$"](around:4828,44.6488,-63.5752); node["leisure"~"^(park|playground|fitness_centre)$"](around:4828,44.6488,-63.5752); way["leisure"~"^(park|playground|fitness_centre)$"](around:4828,44.6488,-63.5752); relation["leisure"~"^(park|recreation_ground)$"](around:4828,44.6488,-63.5752); ); out center tags;[out:json][timeout:90]; (way["maxspeed"]["highway"="primary"](43.3,-66.5,47.1,-59.6); way["bridge"]["maxspeed"](43.3,-66.5,47.1,-59.6); way["tunnel"](43.3,-66.5,47.1,-59.6); node["maxheight"](43.3,-66.5,47.1,-59.6); ); out center;[out:json][timeout:30]; area["name"="Nova Scotia"]["admin_level"="4"]->.p; (way["boundary"="administrative"]["admin_level"="6"](area.p); relation["boundary"="administrative"]["admin_level"="6"](area.p); ); out geom;[out:json][timeout:30]; (node["amenity"="clinic"]["name"~"dental|medical|health|family|physio",i](around:25000,44.6488,-63.5752); way["amenity"="clinic"]["name"~"dental|medical|health|family|physio",i](around:25000,44.6488,-63.5752); node["healthcare"~"dentist|physiotherapist|optometrist",i](around:25000,44.6488,-63.5752); way["healthcare"~"dentist|physiotherapist|optometrist",i](around:25000,44.6488,-63.5752); ); out body center qt;[out:json][timeout:25][bbox:44.6440,-63.5800,44.6520,-63.5650]; (node[amenity]; way[amenity]; relation[amenity]; node[building~"^(commercial|retail|office|public|civic|school|university|hospital|church|hotel|supermarket)$"]; way[building~"^(commercial|retail|office|public|civic|school|university|hospital|church|hotel|supermarket)$"]; node[entrance]; node[highway~"^(bus_stop|crossing|elevator)$"]; way[highway~"^(cycleway|footway|living_street|path|pedestrian|primary|residential|secondary|service|steps|tertiary|trunk|unclassified)$"]; node[leisure]; way[leisure~"^(pitch|park|garden|playground)$"]; node[shop]; way[shop]; node[tourism]; way[tourism]; ); out geom;[out:json][timeout:420]; (node["amenity"="fuel"](43.3,-66.5,47.1,-59.6); way["amenity"="fuel"](43.3,-66.5,47.1,-59.6); ); out center;Other sets
No query is significantly slower; a few unions and some single statements are 2–5% faster:
Peak RSS rises only for unions the heuristic detects: p01, p02, p04, p05, p06, p08, p09, t04 and t11, by up to the 32 MiB limit.
On
all-changes(d6f343b without these commits against e0954f4 with them, same method), the gains are larger: p04 0.675 (0.664–0.687), p05 0.685 (0.664–0.706), p01 0.707 (0.691–0.723), p09 0.767 (0.755–0.779), t04 0.846 (0.823–0.870), t11 0.890 (0.856–0.925), p02 0.923 (0.907–0.939). Only p10 looks slower, p10 1.018 (1.003–1.032), sign test p=0.11. p10 is not a detected union, and its peak RSS is unchanged (10.3 → 10.6 MB).Testing
Output is identical to the baseline for all 37 queries above.
This PR's CI (cf4564c):
fuzzrun 45280378363: 5,986 same, 14 skipped, 0 differences.historyrun 45280378230: 1,998 same, 2 skipped, 0 differences.propsrun 45280377959: 60 seeds, no new failures.The skips time out on both builds, except plain query 1007, which takes about 30–31 s on both builds, right at
fuzz.py's 30 s limit (see the earlier comment).On
all-changes(e0954f4):fuzzrun 45281762847: 5,986 same, 14 skipped.historyrun 45281767630: 1,999 same, 1 skipped.propsrun 45281771102: 60 seeds, no new failures.Mutation testing: a mutant that keeps each cached block without its last 8 bytes is caught by six props checks (
mutaterun 45216773318, on the first commit). Mutants of the 2nd and 3rd commits can only change which blocks are cached, never what a read returns, so they are equivalent and recorded as such.Limitations and open questions
https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
Generated by Claude Code