Skip to content

Write escaped strings in one pass, copying runs that need no escaping - #206

Open
cldellow wants to merge 1 commit into
masterfrom
escape-run-copy
Open

cldellow wants to merge 1 commit into
masterfrom
escape-run-copy

Conversation

@cldellow

Copy link
Copy Markdown
Owner

Fixes #23.

Replaces #26 with a single-pass design. Same call sites, same output, and the escaping rules are defined once.

Change

escape_xml (expat/escape_xml.cc) and escape_cstr (expat/escape_json.cc) built an escaped copy of every tag key, tag value, user name and member role written to XML or JSON output, though most need no escaping.

Now each format has one loop, driven by a 256-entry table of what each byte is written as (nothing for bytes written as they are). The loop writes each run of bytes that need no escaping as it is, and the replacement for each byte that does. This is the pattern of Go's encoding/xml (escapeText) and libxml2's attribute serializer (xmlBufAttrSerializeTxtContent).

Seven files. No change to the database format or the query API.

Impact

CPU time (user + system), Nova Scotia extract of 2026-11-08 with the area rules run, warm cache, 4-core VM. 25 alternating pairs per query, all three builds on upstream/master a9d8f15. Paired B/A with 95% CI. Output is byte-identical in all 27 comparisons.

Query this PR / upstream #26 / upstream this PR / #26
H 0.952 (0.917–0.989) 0.934 (0.892–0.977) 1.029 (0.998–1.061)
q08 0.957 (0.921–0.994) 0.995 (0.956–1.036) 1.012 (0.980–1.045)
q12 0.977 (0.937–1.019) 0.982 (0.943–1.023) 1.021 (0.991–1.052)
t05 0.951 (0.902–1.002) 0.980 (0.927–1.036) 1.010 (0.981–1.040)
J1 0.998 (0.974–1.022) 0.987 (0.957–1.018) 0.981 (0.961–1.000)
X1 0.982 (0.955–1.011) 0.993 (0.962–1.026) 1.001 (0.973–1.029)
E1 0.989 (0.959–1.020) 1.009 (0.961–1.060) 1.021 (0.986–1.057)
E2 1.011 (0.963–1.061) 0.979 (0.933–1.028) 0.990 (0.959–1.022)
control 0.969 (0.918–1.022) 0.940 (0.885–0.999) 1.018 (0.958–1.081)

The control query prints no strings, so neither change can affect it. Its −6% for #26 shows the noise on this VM during the run, about ±6%. Against that:

Query Text
H [out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags;
q08 [out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;
q12 [out:xml]; nwr(44.640,-63.590,44.655,-63.570); (._;>;); out meta;
t05 [out:xml]; (node(44.6440,-63.5800,44.6520,-63.5650); <;); (._;>;); out meta;
J1 [out:json]; nwr[name](44.4,-64.0,45.0,-63.0); out tags; (10.7 MB)
X1 [out:xml]; nwr[name](44.4,-64.0,45.0,-63.0); out tags; (12.5 MB)
E1 [out:json]; nwr["name"~"[&<>\"]"]; out tags; (only names that need escaping)
E2 [out:xml]; nwr["name"~"[&<>\"]"]; out tags;
control [out:csv(::count)]; nwr[amenity](44.60,-63.66,44.70,-63.52); out count;

Testing

  • Against the original functions: 200,513 strings, 0 mismatches, for escape_xml, escape_cstr, Xml_Escaped and Json_Escaped. The strings were the empty string, every 1-byte string, every byte padded inside text, and 200,000 random strings rich in &, ", <, >, \, control characters and NUL.
  • Output: byte-identical on all nine queries above, for all three builds.
  • CI: fuzz, history and props run on this PR.

Memory

No new allocation beyond one 2 KB table per format, built once. Fewer temporary strings.

Limitations

https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm


Generated by Claude Code

escape_xml and escape_cstr built an escaped copy of every key, value,
user and role written, even though most need no escaping. Printing now
writes the string straight to the stream in one pass: runs of bytes that
need no escaping are written as they are, and each byte that does is
replaced from a 256-entry table, as in Go's encoding/xml (escapeText) and
libxml2's attribute serializer. escape_xml and escape_cstr use the same
table and loop, so the escaping rules are defined once.

Output is unchanged: for every byte value and 200,000 random strings,
both functions and the stream writers give the same result as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm
@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 24, 2026 — with Claude
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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