Skip to content

The same data blocks are decompressed more than once per query #16

Open

Description

@cldellow

Problem

Within a single query, the same compressed data blocks are read and decompressed more than once. Decompression (LZ4) is already the largest CPU cost for recursion queries, so repeated reads are wasted work.

Evidence

Block reads traced with strace (each read maps a block and then decompresses it), Nova Scotia test database (see #2), build with all the changes in #12:

Query nodes.bin block reads Distinct blocks Repeated
[out:csv(::count)]; rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count; (benchmark q07) 2,570 1,489 42%
[out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom; (benchmark q10) 187 94 50%: every block read twice
[out:csv(::count)]; way[highway=motorway]; (._;>;); out count; (benchmark q05) 447 447 none

In q07, LZ4 decompression is 48% of CPU samples, so removing the repeats could save roughly 20% of that query (48% × 42%). In q10, decompression is only about 5%.

Likely causes:

  • q10: the bbox filter fetches each way's nodes to test its geometry, and out geom fetches the same nodes again to print them.
  • q07: >> collects the relations' node members and their ways' nodes in separate passes over overlapping blocks.

Possible fix (no format change, no API change)

Keep a small, bounded cache of decompressed blocks per query process in the block-read path (template_db/file_blocks.h, read_block), so a block decompressed once is reused while it stays in the cache. This needs a memory bound and a measurement of peak memory against master. Not attempted yet.

🤖 Generated with Claude Code

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

Activity

  1. cldellow commented on Nov 21, 2026

    @cldellow
    OwnerAuthor

    Update: fixing the cause instead of caching

    An LRU cache of decompressed blocks, simulated on each query's real block-read order (Nova Scotia test database, build with all changes applied), would rarely pay off:

    Query Block reads Hit rate at 16 / 32 / 64 / 128 / 256 MB Best possible
    rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); out count; 3,973 2% / 2% / 2% / 2% / 29% 29%
    [out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom; 411 23% / 37% / 37% / 37% / 37% 37%
    [out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta; 954 0% / 8% / 41% / 41% / 41% 41%
    way[highway](44.60,-63.66,44.70,-63.52); out count;, nwr[amenity=restaurant]; out count; 0% at every size 0%
    • The boundary query needs at least 256 MB per query process. It reads the same blocks in two long passes, so a smaller cache evicts them before the second pass.
    • The other two would save only about 2% and 7%. Decompression is a small share of their CPU time.

    So the fix (#18) instead removes the second pass. >>, and > with relations, now collect relation node members and way nodes in one pass over the node blocks. See #17 for the coordinate formatting part of the out geom query.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


    Generated by Claude Code

  2. added
    query-perf
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    query-perf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions