From 9355a016ca660fcc3dda1592374e7c6d6ed58b56 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Tue, 19 Apr 2011 01:24:26 +0000 Subject: [PATCH] ChunkRef switched to factory method to fix performance defect --- .../SubstrateCS/Source/BlockInterface.cs | 5 ++++ .../SubstrateCS/Source/ChunkFileManager.cs | 12 ++++---- Substrate/SubstrateCS/Source/ChunkManager.cs | 29 ++++++++++--------- Substrate/SubstrateCS/Source/ChunkRef.cs | 11 +++++-- Substrate/SubstrateCS/Source/Region.cs | 12 ++++---- Substrate/SubstrateCS/Substrate.csproj | 5 ++++ 6 files changed, 44 insertions(+), 30 deletions(-) diff --git a/Substrate/SubstrateCS/Source/BlockInterface.cs b/Substrate/SubstrateCS/Source/BlockInterface.cs index fdb13d2..0a8059f 100644 --- a/Substrate/SubstrateCS/Source/BlockInterface.cs +++ b/Substrate/SubstrateCS/Source/BlockInterface.cs @@ -66,6 +66,11 @@ namespace Substrate bool SetBlockLight (int x, int y, int z, int light); bool SetBlockSkyLight (int x, int y, int z, int light); + + //--- + + //void ResetBlockLight (); + //void ResetSkyLight (); } public interface IPropertyBlockContainer : IBlockContainer diff --git a/Substrate/SubstrateCS/Source/ChunkFileManager.cs b/Substrate/SubstrateCS/Source/ChunkFileManager.cs index bb412d8..ae24391 100644 --- a/Substrate/SubstrateCS/Source/ChunkFileManager.cs +++ b/Substrate/SubstrateCS/Source/ChunkFileManager.cs @@ -113,14 +113,12 @@ namespace Substrate return c; } - try { - c = new ChunkRef(this, this, cx, cz); + c = ChunkRef.Create(this, this, cx, cz); + if (c != null) { _cache[k].Target = c; - return c; - } - catch (MissingChunkException) { - return null; } + + return c; } public ChunkRef CreateChunk (int cx, int cz) @@ -129,7 +127,7 @@ namespace Substrate Chunk c = new Chunk(cx, cz); c.Save(GetChunkOutStream(cx, cz)); - ChunkRef cr = new ChunkRef(this, this, cx, cz); + ChunkRef cr = ChunkRef.Create(this, this, cx, cz); ChunkKey k = new ChunkKey(cx, cz); _cache[k] = new WeakReference(cr); diff --git a/Substrate/SubstrateCS/Source/ChunkManager.cs b/Substrate/SubstrateCS/Source/ChunkManager.cs index 96e6248..6a839da 100644 --- a/Substrate/SubstrateCS/Source/ChunkManager.cs +++ b/Substrate/SubstrateCS/Source/ChunkManager.cs @@ -181,44 +181,47 @@ namespace Substrate public void RelightDirtyChunks () { - List dirty = new List(); + //List dirty = new List(); + Dictionary dirty = new Dictionary(); IEnumerator en = _cache.GetDirtyEnumerator(); while (en.MoveNext()) { - dirty.Add(en.Current); + ChunkKey key = new ChunkKey(en.Current.X, en.Current.Z); + dirty[key] = en.Current; } - foreach (ChunkRef chunk in dirty) { + foreach (ChunkRef chunk in dirty.Values) { chunk.ResetBlockLight(); chunk.ResetSkyLight(); } - foreach (ChunkRef chunk in dirty) { + foreach (ChunkRef chunk in dirty.Values) { chunk.RebuildBlockLight(); chunk.RebuildSkyLight(); } - foreach (ChunkRef chunk in dirty) { - ChunkRef east = chunk.GetEastNeighbor(); - if (!east.IsDirty) { + foreach (ChunkRef chunk in dirty.Values) { + + if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z - 1))) { + ChunkRef east = chunk.GetEastNeighbor(); chunk.UpdateEdgeBlockLight(east); chunk.UpdateEdgeSkyLight(east); } - ChunkRef west = chunk.GetWestNeighbor(); - if (!west.IsDirty) { + if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z + 1))) { + ChunkRef west = chunk.GetWestNeighbor(); chunk.UpdateEdgeBlockLight(west); chunk.UpdateEdgeSkyLight(west); } - ChunkRef north = chunk.GetNorthNeighbor(); - if (!north.IsDirty) { + if (!dirty.ContainsKey(new ChunkKey(chunk.X - 1, chunk.Z))) { + ChunkRef north = chunk.GetNorthNeighbor(); chunk.UpdateEdgeBlockLight(north); chunk.UpdateEdgeSkyLight(north); } - ChunkRef south = chunk.GetSouthNeighbor(); - if (!south.IsDirty) { + if (!dirty.ContainsKey(new ChunkKey(chunk.X + 1, chunk.Z))) { + ChunkRef south = chunk.GetSouthNeighbor(); chunk.UpdateEdgeBlockLight(south); chunk.UpdateEdgeSkyLight(south); } diff --git a/Substrate/SubstrateCS/Source/ChunkRef.cs b/Substrate/SubstrateCS/Source/ChunkRef.cs index 4981834..b008b5d 100644 --- a/Substrate/SubstrateCS/Source/ChunkRef.cs +++ b/Substrate/SubstrateCS/Source/ChunkRef.cs @@ -52,16 +52,21 @@ namespace Substrate get { return _dirty; } } - public ChunkRef (IChunkContainer container, IChunkCache cache, int cx, int cz) + private ChunkRef (IChunkContainer container, IChunkCache cache, int cx, int cz) { _container = container; _cache = cache; _cx = cx; _cz = cz; + } - if (!_container.ChunkExists(cx, cz)) { - throw new MissingChunkException(); + public static ChunkRef Create (IChunkContainer container, IChunkCache cache, int cx, int cz) + { + if (!container.ChunkExists(cx, cz)) { + return null; } + + return new ChunkRef(container, cache, cx, cz); } public int BlockGlobalX (int x) diff --git a/Substrate/SubstrateCS/Source/Region.cs b/Substrate/SubstrateCS/Source/Region.cs index 6b83ed2..8cde017 100644 --- a/Substrate/SubstrateCS/Source/Region.cs +++ b/Substrate/SubstrateCS/Source/Region.cs @@ -237,14 +237,12 @@ namespace Substrate return c; } - try { - c = new ChunkRef(this, _cache, lcx, lcz); + c = ChunkRef.Create(this, _cache, lcx, lcz); + if (c != null) { _cache.Insert(c); - return c; - } - catch (MissingChunkException) { - return null; } + + return c; } public ChunkRef CreateChunk (int lcx, int lcz) @@ -262,7 +260,7 @@ namespace Substrate Chunk c = new Chunk(cx, cz); c.Save(GetChunkOutStream(lcx, lcz)); - ChunkRef cr = new ChunkRef(this, _cache, lcx, lcz); + ChunkRef cr = ChunkRef.Create(this, _cache, lcx, lcz); _cache.Insert(cr); return cr; diff --git a/Substrate/SubstrateCS/Substrate.csproj b/Substrate/SubstrateCS/Substrate.csproj index 12e9287..d8c4f71 100644 --- a/Substrate/SubstrateCS/Substrate.csproj +++ b/Substrate/SubstrateCS/Substrate.csproj @@ -59,9 +59,14 @@ Assemblies\Ionic.Zlib.dll + + + + +