Optimal string manipulation in XmlTextWriter?

Note: this entry has moved.

Lately I've been digging inside the XmlTextWriter class. I'm working on an alternate implementation to the traditional state machine based on arrays, one based on a mix of hierarchical state machines and DOM-like events propagation, for an XmlWriter-inherited class.
During this investigation, I found several places where string manipulation is not optimal in aforementioned class. Specifically, even if it uses the StringBuilder class, it mixes calls to it with String.Concat, which is completely useless. Look at the following example taken from the StartDocument method (called by WriteStartDocument):

builder1.Append(string.Concat(" encoding=", this.quoteChar));

This is functionally equivalent to:

builder1.Append(" encoding=").Append(this.quoteChar);

So, why are the strings concatenated? Even temporary arrays of strings are built only to be concatenated and passed to the Append method later. Do these guys now something about string handling we don't or is this just a bit more inefficient code?

References:

2 Comments

  • Have you looked at the IL code difference between the two? The compiler optimizes alot for you. I would investigate the IL code before I called something useless or less than optimal.

  • Mmmm.... I'm sure you didn't pay ENOUGH attention to it. It's not a matter of the IL generated by that line of code, but the methods they call. The first one goes through String.Concat, which allocates a new string with enough space, then fills it with the two paramenters, only to hand this ultimately to the Append method of the StringBuilder.

    Appending directly to this object instead of Concat'ing first is better because the StringBuilder already works with an internal buffer of prealocatted space for appending strings, then, it doesn't incur the allocation cost on every Append call, unlike the Concat which ALWAYS does.

Comments have been disabled for this content.