Skip to content

Tag collection for output deserializes every tag group it walks past #24

Closed as not planned

Description

@cldellow

Problem

Before printing tags, collect_tags_framed (data/tag_store.h) walks every tag entry in each coarse tile of the local tag index and keeps the entries of the objects being printed. The walk is expensive even for the entries it discards:

  • Strings for every tag group: the loop conditions read tag_it.index().index for every entry. The first index() call for each tag group deserializes a full Tag_Index_Local (Idx_Handle::object() in template_db/block_backend.h). That is a heap allocation plus the key and value std::strings, made for every (key, value) group in the tile, including groups that contain none of the wanted objects. The loop only needs the coarse index, which is an integer in the raw bytes (*((uint32*)data + 1) << 8).
  • A map lookup per entry: ids_by_coarse[coarse_index] is looked up on every entry, although it doesn't change inside the loop.
  • Per-tag string pairs: each kept tag is copied into a std::pair<std::string, std::string> in a per-object vector inside a std::map.

Evidence

Nova Scotia test database (see #2), build with all the changes in #12:

[out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags;

This query (12,420 buildings) takes about 330 ms CPU, of which finding the buildings is about 100 ms. In its profile:

Function Share of CPU samples
collect_tags_framed<Uint32_Index> itself 18%
vector growth for the tag pairs 2.2%
std::map operations on tags_by_id 1.9%
allocation and freeing in total, partly from here about 12%

Possible fix (no format change, no API change)

  • Read the coarse index from the raw entry instead of deserializing the whole tag group.
  • Deserialize the key and value only for groups that contain a wanted object.
  • Look up ids_by_coarse[coarse_index] once per tile.

The collected tags, and so the output, stay the same.

🤖 Generated with Claude Code

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

Activity

  1. cldellow commented on Nov 21, 2026

    @cldellow
    OwnerAuthor

    Findings: the deserialization premise was wrong

    A prototype implemented the proposed fix (branch tag-collect-raw-index, not opened as a PR):

    • read the coarse index from the raw entry in all ten tag-walk loops (data/tag_store.h, data/filter_ids_by_tags.h), via a new index_raw() accessor on the block iterator
    • look up ids_by_coarse[coarse_index] once per tile

    It made no measurable difference. Median CPU of 7 alternating runs per build, Nova Scotia test database (see #2), warm cache:

    Query baseline prototype Change
    [out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags; 464 ms 456 ms −2%
    [out:xml]; way[building](44.60,-63.66,44.70,-63.52); out tags; 449 ms 436 ms −3%
    [out:json]; way[building](44.60,-63.66,44.70,-63.52); out center; 616 ms 648 ms +5%
    [out:xml]; way[highway](44.60,-63.66,44.70,-63.52); out body; 251 ms 230 ms −8%
    [out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta; 377 ms 349 ms −7%

    These are all within the run-to-run spread.

    The profile explains why. Deserializing Tag_Index_Local (Tag_Index_Local::Tag_Index_Local(void*)) was only 0.45% of CPU samples for the first query before the change (0.32% after). collect_tags_framed's own time fell from 17.6% to 14.0%, mostly from the once-per-tile lookup.

    Where the time actually goes

    The loop walks every tag entry of every object in each coarse tile (256 quadtiles), and keeps the few wanted ones with a binary_search per entry. The first query reads 129 way_tags_local blocks for 12,420 buildings, most of whose entries belong to other ways (roads, landuse and so on).

    Skipping those entries would need seeking within a tile by object id, which the local tag index layout (sorted by coarse index, then key, then value, then object) doesn't support. That is a structural change, likely a format change, so it's out of scope for now.

    Smaller, untried ideas within the current layout:

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


    Generated by Claude Code

  2. cldellow commented on Nov 21, 2026

    @cldellow
    OwnerAuthor

    Closing as won't fix: skipping the per-tile walk over every tag entry would need seeking by object id within a coarse tile, which the local tag index layout doesn't support. That is a structural (likely on-disk format) change, out of scope for now. The findings are in the comment above.

    🤖 Generated with Claude Code


    Generated by Claude Code

  3. added
    wontfix
    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-perfwontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions