XccGlobalDatabase allocation improvements

- Use the count to size the capacity of the list.
- Use a char array as a buffer, so will can build each string directly rather than needing a ToArray call first.
This commit is contained in:
RoosterDragon
2020-10-18 13:39:20 +01:00
committed by abcdefg30
parent 4f34d3edb3
commit 7c8dc5d5f4

View File

@@ -15,7 +15,7 @@ using System.IO;
namespace OpenRA.Mods.Cnc.FileFormats namespace OpenRA.Mods.Cnc.FileFormats
{ {
public class XccGlobalDatabase : IDisposable public sealed class XccGlobalDatabase : IDisposable
{ {
public readonly string[] Entries; public readonly string[] Entries;
readonly Stream s; readonly Stream s;
@@ -25,22 +25,27 @@ namespace OpenRA.Mods.Cnc.FileFormats
s = stream; s = stream;
var entries = new List<string>(); var entries = new List<string>();
var chars = new char[32];
while (s.Peek() > -1) while (s.Peek() > -1)
{ {
var count = s.ReadInt32(); var count = s.ReadInt32();
var chars = new List<char>(); entries.Capacity += count;
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {
byte c;
// Read filename // Read filename
byte c;
var charsIndex = 0;
while ((c = s.ReadUInt8()) != 0) while ((c = s.ReadUInt8()) != 0)
chars.Add((char)c); {
entries.Add(new string(chars.ToArray())); if (charsIndex >= chars.Length)
chars.Clear(); Array.Resize(ref chars, chars.Length * 2);
chars[charsIndex++] = (char)c;
}
entries.Add(new string(chars, 0, charsIndex));
// Skip comment // Skip comment
while ((c = s.ReadUInt8()) != 0) { } while (s.ReadUInt8() != 0) { }
} }
} }