Repository navigation
Conversation
escape_cstr and escape_xml build a new string, reserved at twice the
input length, for every tag key, tag value, user name and role written
to JSON or XML output, even though most of these strings contain nothing
to escape.
Add Json_Escaped and Xml_Escaped stream helpers. They scan the string
once and, if no character would be escaped, write it directly;
otherwise they fall back to escape_cstr/escape_xml. Output is identical,
because a string with nothing to escape escapes to itself. Use them at
the eight call sites that stream escaped strings in output_json.cc,
output_xml.cc and output_custom.cc.
Checked against escape_cstr and escape_xml for 2.1 million strings
(every one- and two-byte string, random strings rich in special
characters, and 1,200 real tag keys and values). No mismatches.
Nova Scotia extract, CPU time, 15 alternating runs:
[out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags;
454 ms -> 442 ms
[out:xml]; way[building](44.60,-63.66,44.70,-63.52); out tags;
471 ms -> 443 ms
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
Owner
Author
|
Colin's thoughts on the change: Low risk, medium confidence, low benefit. It's not very invasive, mainly bread-and-butter stuff to reduce allocations. |
Owner
Author
|
Superseded by #206, which writes escaped strings in one pass with the escaping rules defined once, as in Go's Generated by Claude Code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #23.
Change
escape_cstr(expat/escape_json.cc) andescape_xml(expat/escape_xml.cc) build a new string, reserved at twice the input length, for every tag key, tag value, user name and member role written to JSON or XML output. Most of those strings contain nothing to escape.This adds
Json_EscapedandXml_Escapedstream helpers next to the existing functions:escape_cstr/escape_xmlas before.Output is identical by construction, because a string with nothing to escape escapes to itself. The eight call sites that stream escaped strings now use them: tags, users and roles in
output_json.ccandoutput_xml.cc, and roles inoutput_custom.cc. No field widths are set anywhere in the output code, soostream::writeandoperator<<produce the same characters.Seven files, about 60 lines. Based on fossgis
master. No change to the database format or the query API.Impact
CPU time (user + system), median of 15 alternating runs per build, Nova Scotia test database (Geofabrik extract of 2026-11-08, imported with
--meta), warm cache, 4-core VM. Output is byte-identical.master[out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags;(12,420 buildings, 13 MB)[out:xml]; way[building](44.60,-63.66,44.70,-63.52); out tags;(15 MB)A wider 5-run comparison (adding
out centeron the same buildings,out bodyon amenities and highways, and benchmark q08[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;) showed −5% to +4%, all within noise.Profile of the JSON query: escaping falls from 4.7% of CPU samples (
escape_cstr) to 3.1% (the new scan), and stream insertion from 3.7% to 2.0%.The gain is modest. Most tag strings are short, so the scan itself remains a noticeable share of the work.
Testing
",\,<,>,&and control characters, and 1,200 real tag keys and values.master(bench/fuzz.py, see Benchmark queries and differential fuzzer #2), mixing JSON and XML output with tags, metadata and roles. 0 differences; 12 skipped, all of which time out onmaster.complete). That covered 10,000 queries across the plain, area and history databases (6,000 / 2,000 / 2,000), with 0 differences. The per-PR fuzz run above predates a fuzzer fix: about 8% of its queries were invalid on both builds, so it rests on roughly 5,500 queries that ran (see Benchmark queries and differential fuzzer #2).Memory
No new allocations, and fewer temporary strings.
Limitations
Generated by Claude Code