Internal: optimized WAD.FindLump() and WAD.FindLumpIndex() functions. They should work ~30x faster now (although, overall performance increase because of this change is fairly noticeable).

This commit is contained in:
MaxED 2014-09-09 13:54:59 +00:00
parent 5e7de03847
commit 7b70316b86

View file

@ -52,9 +52,10 @@ namespace CodeImp.DoomBuilder.IO
// Lumps
private List<Lump> lumps;
private Dictionary<long, List<int>> lookup; //mxd
// Status
private bool isreadonly;
private readonly bool isreadonly;
private bool isdisposed;
#endregion
@ -171,6 +172,7 @@ namespace CodeImp.DoomBuilder.IO
// New lumps array
lumps = new List<Lump>(numlumps);
lookup = new Dictionary<long, List<int>>(); //mxd
// Write the headers
WriteHeaders();
@ -205,6 +207,7 @@ namespace CodeImp.DoomBuilder.IO
// Dispose old lumps and create new list
if(lumps != null) foreach(Lump l in lumps) l.Dispose();
lumps = new List<Lump>(numlumps);
lookup = new Dictionary<long, List<int>>(); //mxd
// Go for all lumps
for(int i = 0; i < numlumps; i++)
@ -216,6 +219,10 @@ namespace CodeImp.DoomBuilder.IO
// Create the lump
lumps.Add(new Lump(file, this, fixedname, offset, length));
// Add to lookup (mxd)
if(!lookup.ContainsKey(lumps[i].LongName)) lookup.Add(lumps[i].LongName, new List<int>());
lookup[lumps[i].LongName].Add(i);
}
}
@ -266,8 +273,6 @@ namespace CodeImp.DoomBuilder.IO
// This creates a new lump in the WAD file
public Lump Insert(string name, int position, int datalength)
{
Lump lump;
// We will be adding a lump
numlumps++;
@ -275,8 +280,12 @@ namespace CodeImp.DoomBuilder.IO
file.SetLength(file.Length + datalength + 16);
// Create the lump
lump = new Lump(file, this, Lump.MakeFixedName(name, ENCODING), lumpsoffset, datalength);
Lump lump = new Lump(file, this, Lump.MakeFixedName(name, ENCODING), lumpsoffset, datalength);
lumps.Insert(position, lump);
// Insert into lookup (mxd)
if(!lookup.ContainsKey(lumps[position].LongName)) lookup.Add(lumps[position].LongName, new List<int>());
lookup[lumps[position].LongName].Add(position);
// Advance lumps table offset
lumpsoffset += datalength;
@ -291,11 +300,10 @@ namespace CodeImp.DoomBuilder.IO
// This removes a lump from the WAD file by index
public void RemoveAt(int index)
{
Lump l;
// Remove from list
l = lumps[index];
Lump l = lumps[index];
lumps.RemoveAt(index);
lookup[l.LongName].Remove(index); //mxd
l.Dispose();
numlumps--;
@ -307,8 +315,10 @@ namespace CodeImp.DoomBuilder.IO
public void Remove(Lump lump)
{
// Remove from list
int pos = lumps.IndexOf(lump); //mxd
lumps.Remove(lump);
lump.Dispose();
lookup[lump.LongName].Remove(pos); //mxd
numlumps--;
// Write the new headers
@ -318,31 +328,40 @@ namespace CodeImp.DoomBuilder.IO
// This finds a lump by name, returns null when not found
public Lump FindLump(string name)
{
int index = FindLumpIndex(name);
if(index == -1)
return null;
else
return lumps[index];
//mxd
long longname = Lump.MakeLongName(name);
if (!lookup.ContainsKey(longname)) return null;
return lumps[lookup[longname][0]];
}
// This finds a lump by name, returns null when not found
public Lump FindLump(string name, int start)
{
int index = FindLumpIndex(name, start);
if(index == -1)
return null;
else
return lumps[index];
//mxd
long longname = Lump.MakeLongName(name);
if(!lookup.ContainsKey(longname)) return null;
foreach(int pos in lookup[longname]) {
if(pos >= start && pos <= lumps.Count - 1)
return lumps[pos];
}
return null;
}
// This finds a lump by name, returns null when not found
public Lump FindLump(string name, int start, int end)
{
int index = FindLumpIndex(name, start, end);
if(index == -1)
return null;
else
return lumps[index];
long longname = Lump.MakeLongName(name);
if(!lookup.ContainsKey(longname)) return null;
// Fix end when it exceeds length
if(end > (lumps.Count - 1)) end = lumps.Count - 1;
foreach(int pos in lookup[longname]) {
if(pos >= start && pos <= end)
return lumps[pos];
}
return null;
}
// This finds a lump by name, returns -1 when not found
@ -365,19 +384,15 @@ namespace CodeImp.DoomBuilder.IO
if(name.Length > 8) return -1;//mxd. Can't be here. Go away!
long longname = Lump.MakeLongName(name);
if(!lookup.ContainsKey(longname)) return -1;
// Fix end when it exceeds length
if(end > (lumps.Count - 1)) end = lumps.Count - 1;
// Loop through the lumps
for(int i = start; i <= end; i++)
// Find in the lookup (~30x faster than DB2 implementation) (mxd)
foreach(int pos in lookup[longname])
{
// Check if the lump name matches
if(lumps[i].LongName == longname)
{
// Found the lump!
return i;
}
if(pos >= start && pos <= end) return pos;
}
// Nothing found