feat: Use XmlWriter
to serialize TwiML instead of using XDocument
#669
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.
All third-party contributors acknowledge that any contributions they provide will be made under the same open-source license that the open-source project is provided under.
Currently the TwiML tree is constructed as a tree of objects which are then converted into a tree of objects from the
System.Xml.Linq
, to then be serialized to a string usingXDocument.Save
.This creates three copies of the same data in memory, one as TwiML objects, one as
System.Xml.Linq
objects, and one as a string.To reduce the amount of memory used and increase performance, this PR uses the
XmlWriter
APIs instead ofSystem.Xml.Linq
APIs.The TwiML tree is constructed just the same, but that tree is serialized without creating another copy of the tree, and can be written directly to
Stream
s,TextWriter
s, andXmlWriter
s.If the
Stream
is a file stream, a network stream, etc., the entire XML string wouldn't be loaded into memory as a result.If you do use a
TextWriter
or theToString
method, the string would be loaded in memory but this PR won't create another copy of the tree asSystem.Xml.Linq
objects.The resulting XML uses self-closing tags at the moment, which would reduce the size of the string and make HTTP responses containing TwiML smaller, but this can be reverted if necessary for backwards compatibility reasons.
Checklist