title: Has anyone implemented a Regex and/or Xml parser around StringBuilders or Streams?
body: <p>I'm building a stress-testing client that hammers servers and analyzes responses using as many threads as the client can muster. I'm constantly finding myself throttled by garbage collection (and/or lack thereof), and in most cases, it comes down to strings that I'm instantiating only to pass them off to a Regex or an Xml parsing routine. </p>
  
  <p>If you decompile the Regex class, you'll see that <em>internally</em>, it uses StringBuilders to do nearly everything, but you can't <em>pass</em> it a string builder; it helpfully dives down into private methods before starting to use them, so extension methods aren't going to solve it either. You're in a similar situation if you want to get an object graph out of the parser in System.Xml.Linq. </p>
  
  <p>This is not a case of pedantic over-optimization-in-advance. I've looked at the <a href="https://stackoverflow.com/questions/3504850/regex-replacements-inside-a-stringbuilder" title="Regex replacements inside a StringBuilder">Regex replacements inside a StringBuilder</a> question and others. I've also profiled my app to see where the ceilings are coming from, and using <code>Regex.Replace()</code> now is indeed introducing significant overhead in a method chain where I'm trying to hit a server with millions of requests per hour and examine XML responses for errors and embedded diagnostic codes. I've already gotten rid of just about every other inefficiency that's throttling the throughput, and I've even cut a lot of the Regex overhead out by extending StringBuilder to do wildcard find/replace when I don't need capture groups or backreferences, but it seems to me that someone would have wrapped up a custom StringBuilder (or better yet, Stream) based Regex and Xml parsing utility by now. </p>
  
  <p>Ok, so rant over, but am I going to have to do this myself?</p>
  
  <p><strong>Update:</strong> I found a workaround which lowered peak memory consumption from multiple gigabytes to a few hundred megs, so I'm posting it below. I'm not adding it as an answer because a) I generally hate to do that, and b) I still want to find out if someone takes the time to customize StringBuilder to do Regexes (or vice-versa) before I do.</p>
  
  <p>In my case, I could not use XmlReader because the stream I am ingesting contains some invalid binary content in certain elements. In order to parse the XML, I have to empty out those elements. I was previously using a single static compiled Regex instance to do the replace, and this consumed memory like mad (I'm trying to process ~300 10KB docs/sec). The change that drastically reduced consumption was:</p>
  
  <ol>
  <li>I added the code from this <a href="http://www.codeproject.com/Tips/129369/StringBuilder-Extensions" rel="nofollow noreferrer">StringBuilder Extensions article on
  CodeProject</a> for the handy <code>IndexOf</code> method. </li>
  <li>I added a (very) crude <code>WildcardReplace</code> method that allows <em>one</em> wildcard character (* or ?) per invocation  </li>
  <li>I replaced the Regex usage with a <code>WildcardReplace()</code> call to empty the contents of the offending elements</li>
  </ol>
  
  <p>This is very unpretty and tested only as far as my own purposes required; I would have made it more elegant and powerful, but YAGNI and all that, and I'm in a hurry. Here's the code:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Performs basic wildcard find and replace on a string builder, observing one of two 
  /// wildcard characters: * matches any number of characters, or ? matches a single character.
  /// Operates on only one wildcard per invocation; 2 or more wildcards in &lt;paramref name="find"/&gt;
  /// will cause an exception.
  /// All characters in &lt;paramref name="replaceWith"/&gt; are treated as literal parts of 
  /// the replacement text.
  /// &lt;/summary&gt;
  /// &lt;param name="find"&gt;&lt;/param&gt;
  /// &lt;param name="replaceWith"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static StringBuilder WildcardReplace(this StringBuilder sb, string find, string replaceWith) {
      if (find.Split(new char[] { '*' }).Length &gt; 2 || find.Split(new char[] { '?' }).Length &gt; 2 || (find.Contains("*") &amp;&amp; find.Contains("?"))) {
          throw new ArgumentException("Only one wildcard is supported, but more than one was supplied.", "find");
      } 
      // are we matching one character, or any number?
      bool matchOneCharacter = find.Contains("?");
      string[] parts = matchOneCharacter ? 
          find.Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries) 
          : find.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
      int startItemIdx; 
      int endItemIdx;
      int newStartIdx = 0;
      int length;
      while ((startItemIdx = sb.IndexOf(parts[0], newStartIdx)) &gt; 0 
          &amp;&amp; (endItemIdx = sb.IndexOf(parts[1], startItemIdx + parts[0].Length)) &gt; 0) {
          length = (endItemIdx + parts[1].Length) - startItemIdx;
          newStartIdx = startItemIdx + replaceWith.Length;
          // With "?" wildcard, find parameter length should equal the length of its match:
          if (matchOneCharacter &amp;&amp; length &gt; find.Length)
              break;
          sb.Remove(startItemIdx, length);
          sb.Insert(startItemIdx, replaceWith);
      }
      return sb;
  }
  </code></pre>