Skip to content

Write tag strings without an escaped copy when nothing needs escaping - #26

Closed
cldellow wants to merge 1 commit into
masterfrom
escape-fast-path
Closed

cldellow wants to merge 1 commit into
masterfrom
escape-fast-path

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #23.

Change

escape_cstr (expat/escape_json.cc) and escape_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_Escaped and Xml_Escaped stream helpers next to the existing functions:

  • Common case: each scans the string once and, if no character would be escaped, writes the string directly.
  • Otherwise: it falls back to escape_cstr/escape_xml as 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.cc and output_xml.cc, and roles in output_custom.cc. No field widths are set anywhere in the output code, so ostream::write and operator<< 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.

Query master This PR Change
[out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags; (12,420 buildings, 13 MB) 454 ms 442 ms −3%
[out:xml]; way[building](44.60,-63.66,44.70,-63.52); out tags; (15 MB) 471 ms 443 ms −6%

A wider 5-run comparison (adding out center on the same buildings, out body on 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

  • Helpers against the originals: 2,132,529 strings checked, with 0 mismatches. The strings were the empty string, every 1-byte string, every 2-byte string (alone and padded), 2 million random strings rich in ", \, <, >, & and control characters, and 1,200 real tag keys and values.
  • Differential fuzzer, seeds 2 and 3: 6,000 queries against 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 on master.
  • Update, re-fuzzed with the corrected fuzzer: this change has since been re-fuzzed as part of the all-changes branch (All changes combined (tracking PR) #12), with the corrected fuzzer and a bigger vocabulary (areas, history, users and 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

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
@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 21, 2026 — with Claude
@cldellow cldellow added the human-reviewed Colin has reviewed this. label Nov 21, 2026
@cldellow

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Owner Author

Superseded by #206, which writes escaped strings in one pass with the escaping rules defined once, as in Go's encoding/xml and libxml2. On upstream/master a9d8f15 the two are within noise of each other on every query measured; #206 has the simpler design. Details and numbers are in #206.


Generated by Claude Code

@cldellow cldellow closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human-reviewed Colin has reviewed this. query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Escaping tag strings allocates and copies every key and value

2 participants