Speed up documentation processing by caching the results of the last transformation.

This commit is contained in:
the_fiddler 2010-10-12 11:00:46 +00:00
parent 9aded6ac44
commit d352aed114

View file

@ -14,6 +14,9 @@ namespace Bind
static readonly XslCompiledTransform xslt = new XslCompiledTransform(); static readonly XslCompiledTransform xslt = new XslCompiledTransform();
static readonly XmlReaderSettings settings = new XmlReaderSettings(); static readonly XmlReaderSettings settings = new XmlReaderSettings();
string Text;
string LastFile;
public DocProcessor(string transform_file) public DocProcessor(string transform_file)
{ {
xslt.Load(transform_file); xslt.Load(transform_file);
@ -27,35 +30,40 @@ namespace Bind
// Todo: Some files include more than 1 function - find a way to map these extra functions. // Todo: Some files include more than 1 function - find a way to map these extra functions.
public string ProcessFile(string file) public string ProcessFile(string file)
{ {
string text = File.ReadAllText(file); if (LastFile == file)
return Text;
Match m = remove_mathml.Match(text); LastFile = file;
Text = File.ReadAllText(file);
Match m = remove_mathml.Match(Text);
while (m.Length > 0) while (m.Length > 0)
{ {
string removed = text.Substring(m.Index, m.Length); string removed = Text.Substring(m.Index, m.Length);
text = text.Remove(m.Index, m.Length); Text = Text.Remove(m.Index, m.Length);
int equation = removed.IndexOf("eqn"); int equation = removed.IndexOf("eqn");
if (equation > 0) if (equation > 0)
{ {
text = text.Insert(m.Index, Text = Text.Insert(m.Index,
"<![CDATA[" + "<![CDATA[" +
removed.Substring(equation + 4, removed.IndexOf(":-->") - equation - 4) + removed.Substring(equation + 4, removed.IndexOf(":-->") - equation - 4) +
"]]>"); "]]>");
} }
m = remove_mathml.Match(text); m = remove_mathml.Match(Text);
} }
XmlReader doc = null; XmlReader doc = null;
try try
{ {
// The pure XmlReader is ~20x faster than the XmlTextReader. // The pure XmlReader is ~20x faster than the XmlTextReader.
doc = XmlReader.Create(new StringReader(text), settings); doc = XmlReader.Create(new StringReader(Text), settings);
//doc = new XmlTextReader(new StringReader(text)); //doc = new XmlTextReader(new StringReader(text));
using (StringWriter sw = new StringWriter()) using (StringWriter sw = new StringWriter())
{ {
xslt.Transform(doc, null, sw); xslt.Transform(doc, null, sw);
return sw.ToString().TrimEnd('\n'); Text = sw.ToString().TrimEnd('\n');
return Text;
} }
} }
catch (XmlException e) catch (XmlException e)