You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Escaping tag strings allocates and copies every key and value #23
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.
Activity