Repository navigation
Conversation
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
This was referenced Nov 24, 2026
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.
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) andescape_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).Xml_EscapedandJson_Escaped(as in Write tag strings without an escaped copy when nothing needs escaping #26) write straight to the stream through the loop. The eight call sites are the same as Write tag strings without an escaped copy when nothing needs escaping #26's: tags, users and roles inoutput_json.ccandoutput_xml.cc, and roles inoutput_custom.cc.escape_xmlandescape_cstrbuild their string with the same loop and table.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.
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:
[out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags;[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;[out:xml]; nwr(44.640,-63.590,44.655,-63.570); (._;>;); out meta;[out:xml]; (node(44.6440,-63.5800,44.6520,-63.5650); <;); (._;>;); out meta;[out:json]; nwr[name](44.4,-64.0,45.0,-63.0); out tags;(10.7 MB)[out:xml]; nwr[name](44.4,-64.0,45.0,-63.0); out tags;(12.5 MB)[out:json]; nwr["name"~"[&<>\"]"]; out tags;(only names that need escaping)[out:xml]; nwr["name"~"[&<>\"]"]; out tags;[out:csv(::count)]; nwr[amenity](44.60,-63.66,44.70,-63.52); out count;Testing
escape_xml,escape_cstr,Xml_EscapedandJson_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.fuzz,historyandpropsrun 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