Skip to content

Escaping tag strings allocates and copies every key and value #23

Description

@cldellow

Problem

Every tag key and value written to JSON or XML output goes through escape_cstr (expat/escape_json.cc) or escape_xml (expat/escape_xml.cc). Both allocate a new string reserved at twice the input length and copy the input one character at a time, even though most tag strings contain nothing that needs escaping. The 16 call sites in output_formats/ then insert the result into the stream, so each string is copied twice.

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, 13 MB of output) takes about 330 ms CPU. In its profile:

Function Share of CPU samples
escape_cstr itself 6.1%
std::__ostream_insert 4.8%
malloc, free and related about 12% in total; part of it comes from these temporary strings

The same path runs for every out tags, body, meta, center and geom in JSON or XML.

Proposed fix (no format change, no API change)

Write tag strings to the stream through a helper:

  • Common case: scan the string once, and if nothing needs escaping, write it directly.
  • Otherwise: fall back to the existing escaping function.

Output stays byte-identical by construction, because a string with nothing to escape escapes to itself.

🤖 Generated with Claude Code

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

Activity

  1. 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