Hoist XmlWriterSettings and dispose StringWriter in XmlOutputBuilder#713
Merged
tannergooding merged 1 commit intoJul 13, 2026
Conversation
`Contents` previously allocated a fresh `XmlWriterSettings` on every access and leaked the backing `StringWriter`. Hoist the settings to a `private static readonly` field so they are constructed once, and wrap the `StringWriter` (and the `XmlWriter`) in `using` scopes so both are disposed. The `XmlWriter` is disposed before `sw.ToString()`, which flushes it fully, so the produced XML is unchanged (replacing the prior explicit `Flush`). Also replace `new XText(value).ToString()` in `EscapeText` with a small purpose-built escaper for `<`, `>`, and `&`. This was verified byte-identical to `XText.ToString()` for every input except raw `\r`/`\n` (which `XText` newline-normalizes and which never appear in the type names `EscapeText` receives), so generated output is unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.
Modernizes
XmlOutputBuilder.cswith two behavior-preserving cleanups. Generated output is unchanged -- all ~3708 golden tests pass and the build is clean (0 warnings, 0 errors).Hoist
XmlWriterSettings+ disposeStringWriterContentspreviously allocated a freshXmlWriterSettingson every access and leaked the backingStringWriter. The settings are now aprivate static readonlyfield constructed once, and both theStringWriterandXmlWriterare wrapped inusingscopes. TheXmlWriteris disposed (which fully flushes it) beforesw.ToString(), so this replaces the old explicitwriter.Flush()while producing identical XML -- no truncation.Replace
XText-based escaping with a direct escaperEscapeTextusednew XText(value).ToString()solely to XML-escape a string. It now uses a small purpose-built escaper for<,>, and&. This was verified byte-identical toXText.ToString()across every UTF-16 code point plus realistic type names -- the only divergence is raw\r/\n, whichXTextnewline-normalizes and which never appear in the type namesEscapeTextreceives.SecurityElement.Escapewas rejected since it escapes a different character set ("/') and does not matchXText.Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com