Skip to content

id map: read a window only as far as the ids looked up in it - #272

Open
cldellow wants to merge 1 commit into
masterfrom
map-partial-reads
Open

cldellow wants to merge 1 commit into
masterfrom
map-partial-reads

Conversation

@cldellow

Copy link
Copy Markdown
Owner

Fixes #269.

Change

Random_File::move_cache_window (template_db/random_file.h) loaded a whole 256 KiB id-map window (65,536 ids) for a lookup that reads one 4-byte entry: an LZ4 window was decompressed in full, an uncompressed one read in full.

  • Random_File::get(pos, last): last is the largest key the caller will look up before leaving pos's window. move_cache_window then decompresses (new LZ4_Inflate::decompress_partial, around LZ4_decompress_safe_partial) or reads only up to that entry, and records how many bytes of the window are valid. A later lookup further into the window reads the rest; if a partial decompression returns too little, it falls back to the whole window.
  • put (imports) and zlib-compressed maps keep whole windows.
  • get_indexes_ (data/collect_items.h) passes each window's last id, which its sorted input gives exactly. It serves id queries, id filters in query statements, diffs and timeline.

4 files, +82/−8. No change to the database format or the query API.

Impact

CPU time (user + system), Geofabrik France extract imported with --meta and lz4-compressed maps, warm cache, 4-core Intel Xeon VM (2.1 GHz), against upstream/master 555f744, 15 alternating pairs; paired ratio with 95% CI. Output identical.

Query upstream/master This PR Ratio (95% CI)
id query: [out:csv(::count)]; node(id:…); out count; with 2,059 node ids, every 50th node (in id order) of the seamark query's output 169 ms 155 ms 0.931 (0.910–0.953), faster in 14 of 15
[out:csv(::count)]; node['seamark:type']; out count; 3.92 s 3.79 s 0.974 (0.965–0.984)
seamark: (rel['seamark:type']; >; way['seamark:type']; >; node['seamark:type']; ); out meta; 9.82 s 9.72 s 0.992 (0.986–0.998)

On the all-changes branch (#12), where commit 83 looks candidates up by their exact indices, node['seamark:type'] gains more: 0.910 (0.871–0.952).

Benchmark queries: sums of medians over 3 pairs, 0.98 on the plain set (21 queries) and 0.98 on the area set (16); the timing workflow measures them properly.

Testing

  • Output identical to upstream/master 555f744 on the queries above and on the 37 benchmark queries (Nova Scotia, bench/queries, queries-poi, queries-areas, queries-tools, 3 alternating pairs each).
  • CI: fuzz, history, props and timing run on this PR.

Limitations

  • France's maps are sparse (only France's ids are set), so its windows are mostly zeros, which decompress quickly. On a planet database windows are full, and the saving per window should be larger; not measured.
  • LZ4_decompress_safe_partial needs LZ4 1.9.0 or later to be reliable (it had a bug before 1.9.0).

Generated by Claude Code

Random_File::move_cache_window loaded the whole 256 KiB window of an id map
(65,536 ids) for a lookup that reads one 4-byte entry: an LZ4 window was
decompressed in full, an uncompressed one read in full.

Random_File::get(pos, last) takes the largest key the caller will look up
before leaving pos's window, and move_cache_window decompresses
(LZ4_Inflate::decompress_partial, around LZ4_decompress_safe_partial) or
reads only that far. A later lookup further into the window reads the rest.
put and zlib-compressed maps keep whole windows. get_indexes_ passes each
window's last id, which its sorted input gives exactly.

On the Geofabrik France extract (--meta, lz4-compressed maps), warm cache,
4-core VM, median CPU of 15 alternating runs, for 2,059 node ids (every 50th
node, in id order, of the output of the query below):
  [out:csv(::count)]; node(id:...); out count;    169 ms -> 155 ms
  (rel['seamark:type']; >; way['seamark:type']; >; node['seamark:type']; ); out meta;
    9.82 s -> 9.72 s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 29, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

CI against upstream/master 555f744: fuzz (run) 6,000 queries, 5,995 same, 5 skipped (timeouts), 0 differences; history (run) 2,000 queries, all same; props (run) 60 seeds, no new failures; timing (run, 5 ARM runners) total 0.996 over the 27 benchmark, area and client-tool queries, 7 faster and none slower, outputs identical.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Id map lookups load a whole 256 KiB window for one 4-byte entry

2 participants