Merge pull request #7232 from RoosterDragon/yaml-parse-perf

Improve YAML parse time.
This commit is contained in:
Paul Chote
2014-12-31 09:20:48 +13:00
2 changed files with 8 additions and 18 deletions

View File

@@ -142,7 +142,7 @@ namespace OpenRA
return nd.ContainsKey(s) ? nd[s].Nodes : new List<MiniYamlNode>(); return nd.ContainsKey(s) ? nd[s].Nodes : new List<MiniYamlNode>();
} }
static List<MiniYamlNode> FromLines(string[] lines, string filename) static List<MiniYamlNode> FromLines(IEnumerable<string> lines, string filename)
{ {
var levels = new List<List<MiniYamlNode>>(); var levels = new List<List<MiniYamlNode>>();
levels.Add(new List<MiniYamlNode>()); levels.Add(new List<MiniYamlNode>());
@@ -152,8 +152,9 @@ namespace OpenRA
{ {
var line = ll; var line = ll;
++lineNo; ++lineNo;
if (line.Contains('#')) var commentIndex = line.IndexOf('#');
line = line.Substring(0, line.IndexOf('#')).TrimEnd(' ', '\t'); if (commentIndex != -1)
line = line.Substring(0, commentIndex).TrimEnd(' ', '\t');
var t = line.TrimStart(' ', '\t'); var t = line.TrimStart(' ', '\t');
if (t.Length == 0) if (t.Length == 0)
continue; continue;
@@ -189,12 +190,8 @@ namespace OpenRA
public static List<MiniYamlNode> FromFileInPackage(string path) public static List<MiniYamlNode> FromFileInPackage(string path)
{ {
var lines = new List<string>();
using (var stream = GlobalFileSystem.Open(path)) using (var stream = GlobalFileSystem.Open(path))
using (var reader = new StreamReader(stream)) return FromLines(stream.ReadAllLines(), path);
while (!reader.EndOfStream)
lines.Add(reader.ReadLine());
return FromLines(lines.ToArray(), path);
} }
public static Dictionary<string, MiniYaml> DictFromFile(string path) public static Dictionary<string, MiniYaml> DictFromFile(string path)

View File

@@ -124,18 +124,11 @@ namespace OpenRA
public static IEnumerable<string> ReadAllLines(this Stream s) public static IEnumerable<string> ReadAllLines(this Stream s)
{ {
string line;
using (var sr = new StreamReader(s)) using (var sr = new StreamReader(s))
{ while ((line = sr.ReadLine()) != null)
for (;;)
{
var line = sr.ReadLine();
if (line == null)
yield break;
else
yield return line; yield return line;
} }
}
}
// The string is assumed to be length-prefixed, as written by WriteString() // The string is assumed to be length-prefixed, as written by WriteString()
public static string ReadString(this Stream s, Encoding encoding, int maxLength) public static string ReadString(this Stream s, Encoding encoding, int maxLength)