Fix strange d2k loading slowness

There is a strange issue that appears* when Theater calls
ISpriteFrame.Frames on the R8Reader. The R8Reader uses
IEnumerable.Cast<> which behaves slower and slower, which
makes map loading become 10+ times slower.

The changes here simply avoid the casting.

[*] This happens at least on Linux x86_64 with Mono 3.2.8.

See https://bugzilla.xamarin.com/show_bug.cgi?id=19668
This commit is contained in:
Pavlos Touboulidis
2014-05-11 05:55:38 +03:00
parent 17d3bc27ce
commit 2ec6df9680
8 changed files with 22 additions and 13 deletions

View File

@@ -62,8 +62,8 @@ namespace OpenRA.FileFormats
public class R8Reader : ISpriteSource
{
readonly List<R8Image> frames = new List<R8Image>();
public IEnumerable<ISpriteFrame> Frames { get { return frames.Cast<ISpriteFrame>(); } }
readonly List<ISpriteFrame> frames = new List<ISpriteFrame>();
public IEnumerable<ISpriteFrame> Frames { get { return frames; } }
public bool CacheWhenLoadingTileset { get { return true; } }
public readonly int ImageCount;

View File

@@ -86,8 +86,8 @@ namespace OpenRA.FileFormats
public class ShpD2Reader : ISpriteSource
{
List<Frame> headers = new List<Frame>();
public IEnumerable<ISpriteFrame> Frames { get { return headers.Cast<ISpriteFrame>(); } }
readonly List<ISpriteFrame> frames = new List<ISpriteFrame>();
public IEnumerable<ISpriteFrame> Frames { get { return frames; } }
public bool CacheWhenLoadingTileset { get { return false; } }
public ShpD2Reader(Stream s)
@@ -108,7 +108,7 @@ namespace OpenRA.FileFormats
for (var i = 0; i < imageCount; i++)
{
s.Position = offsets[i];
headers.Add(new Frame(s));
frames.Add(new Frame(s));
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
@@ -58,7 +59,8 @@ namespace OpenRA.FileFormats
public class ShpReader : ISpriteSource
{
readonly List<ImageHeader> headers = new List<ImageHeader>();
public IEnumerable<ISpriteFrame> Frames { get { return headers.Cast<ISpriteFrame>(); } }
Lazy<IEnumerable<ISpriteFrame>> spriteFrames;
public IEnumerable<ISpriteFrame> Frames { get { return spriteFrames.Value; } }
public bool CacheWhenLoadingTileset { get { return false; } }
public readonly Size Size;
@@ -93,6 +95,8 @@ namespace OpenRA.FileFormats
foreach (var h in headers)
Decompress(stream, h);
spriteFrames = Exts.Lazy(() => headers.Cast<ISpriteFrame>());
}
static byte[] ReadCompressedData(Stream stream, ImageHeader h)

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
@@ -46,7 +47,8 @@ namespace OpenRA.FileFormats
public class ShpTSReader : ISpriteSource
{
readonly List<FrameHeader> frames = new List<FrameHeader>();
public IEnumerable<ISpriteFrame> Frames { get { return frames.Cast<ISpriteFrame>(); } }
Lazy<IEnumerable<ISpriteFrame>> spriteFrames;
public IEnumerable<ISpriteFrame> Frames { get { return spriteFrames.Value; } }
public bool CacheWhenLoadingTileset { get { return false; } }
public ShpTSReader(Stream stream)
@@ -95,6 +97,8 @@ namespace OpenRA.FileFormats
}
}
}
spriteFrames = Exts.Lazy(() => frames.Cast<ISpriteFrame>());
}
}
}

View File

@@ -18,8 +18,8 @@ namespace OpenRA.FileFormats
{
public class TmpRAReader : ISpriteSource
{
readonly List<TmpTile> tiles = new List<TmpTile>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles.Cast<ISpriteFrame>(); } }
readonly List<ISpriteFrame> tiles = new List<ISpriteFrame>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles; } }
public bool CacheWhenLoadingTileset { get { return false; } }
public TmpRAReader(Stream s)

View File

@@ -37,8 +37,8 @@ namespace OpenRA.FileFormats
public class TmpTDReader : ISpriteSource
{
readonly List<TmpTile> tiles = new List<TmpTile>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles.Cast<ISpriteFrame>(); } }
readonly List<ISpriteFrame> tiles = new List<ISpriteFrame>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles; } }
public bool CacheWhenLoadingTileset { get { return false; } }
public TmpTDReader(Stream s)

View File

@@ -51,8 +51,8 @@ namespace OpenRA.FileFormats
public class TmpTSReader : ISpriteSource
{
readonly List<TmpTSTile> tiles = new List<TmpTSTile>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles.Cast<ISpriteFrame>(); } }
readonly List<ISpriteFrame> tiles = new List<ISpriteFrame>();
public IEnumerable<ISpriteFrame> Frames { get { return tiles; } }
public bool CacheWhenLoadingTileset { get { return false; } }
public TmpTSReader(Stream s)

View File

@@ -26,6 +26,7 @@ namespace OpenRA.Graphics
public interface ISpriteSource
{
// TODO: Change this to IReadOnlyList so users don't need to call .ToArray()
IEnumerable<ISpriteFrame> Frames { get; }
bool CacheWhenLoadingTileset { get; }
}