ChunkRef switched to factory method to fix performance defect

This commit is contained in:
Justin Aquadro 2011-04-19 01:24:26 +00:00
parent 324d997f52
commit 9355a016ca
6 changed files with 44 additions and 30 deletions

View file

@ -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

View file

@ -113,15 +113,13 @@ 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;
}
}
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);

View file

@ -181,44 +181,47 @@ namespace Substrate
public void RelightDirtyChunks ()
{
List<ChunkRef> dirty = new List<ChunkRef>();
//List<ChunkRef> dirty = new List<ChunkRef>();
Dictionary<ChunkKey, ChunkRef> dirty = new Dictionary<ChunkKey, ChunkRef>();
IEnumerator<ChunkRef> 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) {
foreach (ChunkRef chunk in dirty.Values) {
if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z - 1))) {
ChunkRef east = chunk.GetEastNeighbor();
if (!east.IsDirty) {
chunk.UpdateEdgeBlockLight(east);
chunk.UpdateEdgeSkyLight(east);
}
if (!dirty.ContainsKey(new ChunkKey(chunk.X, chunk.Z + 1))) {
ChunkRef west = chunk.GetWestNeighbor();
if (!west.IsDirty) {
chunk.UpdateEdgeBlockLight(west);
chunk.UpdateEdgeSkyLight(west);
}
if (!dirty.ContainsKey(new ChunkKey(chunk.X - 1, chunk.Z))) {
ChunkRef north = chunk.GetNorthNeighbor();
if (!north.IsDirty) {
chunk.UpdateEdgeBlockLight(north);
chunk.UpdateEdgeSkyLight(north);
}
if (!dirty.ContainsKey(new ChunkKey(chunk.X + 1, chunk.Z))) {
ChunkRef south = chunk.GetSouthNeighbor();
if (!south.IsDirty) {
chunk.UpdateEdgeBlockLight(south);
chunk.UpdateEdgeSkyLight(south);
}

View file

@ -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)

View file

@ -237,15 +237,13 @@ 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;
}
}
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;

View file

@ -59,9 +59,14 @@
<HintPath>Assemblies\Ionic.Zlib.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Source\ChunkCache.cs" />
<None Include="Source\Experimental\BlockInterface.cs" />
<Compile Include="Source\Level.cs" />
<Compile Include="Source\PlayerManager.cs" />
<Compile Include="Source\PlayerFile.cs" />