mwrock / RequestReduce

Instantly makes your .net website faster by reducing the number and size of requests with almost no effort.
www.requestreduce.org
Apache License 2.0
228 stars 49 forks source link

ResponseTransformer slow for large pages #263

Open jeremycampbellaustin opened 10 years ago

jeremycampbellaustin commented 10 years ago

I changed this loop:

var result = preTransform; foreach (var match in transformableMatches) { var idx = result.IndexOf(match, StringComparison.Ordinal); result = result.Remove(idx, match.Length); if(idx == result.Length) continue; if(result[idx] == '\r') result = result.Remove(idx, 1); if (result[idx] == '\n') result = result.Remove(idx, 1); } return result;

To this loop: StringBuilder resultNew = new StringBuilder(); using (StringReader stringReader = new StringReader(preTransform)) { string line; while ((line = stringReader.ReadLine()) != null) { if (!Filtered(transformableMatches, line)) { resultNew.AppendLine(line); } } } return resultNew.ToString();

private bool Filtered(List transformableMatches, string line) { int indexOf = -1; for(int i = 0; i < transformableMatches.Count; ++i) { if (line.Contains(transformableMatches[i])) { indexOf = i; break; } }

        return indexOf >= 0;
    }

Also this line: preTransform = preTransform.Insert(insertionIdx + 1, resource.TransformedMarkupTag(transform));

To this line: preTransform = preTransform.Insert(insertionIdx + 1, String.Format("{0}{1}", resource.TransformedMarkupTag(transform), Environment.NewLine));

Server processing time went from ~10sec to less than 1sec.

If someone peer programs with me and shows me the ropes, how to run the tests, etc. I will update the source.