[DatFile] Take care of more unnecessary nested threading

This commit is contained in:
Matt Nadareski
2017-06-14 12:49:55 -07:00
parent b348cb5065
commit 1500875822
3 changed files with 37 additions and 58 deletions

View File

@@ -541,7 +541,7 @@ namespace SabreTools.Library.Dats
{
List<DatItem> items = this[key].ToList();
List<DatItem> newItems = new List<DatItem>();
Parallel.ForEach(items, Globals.ParallelOptions, item =>
foreach (DatItem item in items)
{
DatItem newItem = item;
string filename = inputs[newItem.SystemID].Split('¬')[0];
@@ -553,11 +553,8 @@ namespace SabreTools.Library.Dats
+ Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar
+ newItem.Machine.Name;
lock (newItems)
{
newItems.Add(newItem);
}
});
Remove(key);
AddRange(key, newItems);

View File

@@ -51,7 +51,7 @@ namespace SabreTools.Library.Dats
List<DatItem> roms = this[key];
// Now add each of the roms to their respective games
Parallel.ForEach(roms, Globals.ParallelOptions, rom =>
foreach (DatItem rom in roms)
{
string newkey = "";
@@ -132,7 +132,7 @@ namespace SabreTools.Library.Dats
}
sortable[newkey].Add(rom);
}
});
}
});
// Now go through and sort all of the individual lists
@@ -182,7 +182,7 @@ namespace SabreTools.Library.Dats
// For every item in the current key
List<DatItem> items = this[key];
List<DatItem> newitems = new List<DatItem>();
Parallel.ForEach(items, Globals.ParallelOptions, item =>
foreach (DatItem item in items)
{
// If the rom passes the filter, include it
if (filter.ItemPasses(item))
@@ -212,7 +212,7 @@ namespace SabreTools.Library.Dats
newitems.Add(item);
}
}
});
}
Remove(key);
AddRange(key, newitems);
@@ -232,14 +232,14 @@ namespace SabreTools.Library.Dats
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> items = this[key];
Parallel.ForEach(items, Globals.ParallelOptions, item =>
foreach (DatItem item in items)
{
// If the key mapping doesn't exist, add it
if (!mapping.ContainsKey(item.Machine.Name))
{
mapping.TryAdd(item.Machine.Name, item.Machine.Description.Replace('/', '_').Replace("\"", "''"));
}
});
}
});
// Now we loop through every item and update accordingly
@@ -248,41 +248,35 @@ namespace SabreTools.Library.Dats
{
List<DatItem> items = this[key];
List<DatItem> newItems = new List<DatItem>();
Parallel.ForEach(items, Globals.ParallelOptions, item =>
foreach (DatItem item in items)
{
// Clone the item first for easier working
DatItem newItem = (DatItem)item.Clone();
// Update machine name
if (!String.IsNullOrEmpty(newItem.Machine.Name) && mapping.ContainsKey(newItem.Machine.Name))
if (!String.IsNullOrEmpty(item.Machine.Name) && mapping.ContainsKey(item.Machine.Name))
{
newItem.Machine.Name = mapping[newItem.Machine.Name];
item.Machine.Name = mapping[item.Machine.Name];
}
// Update cloneof
if (!String.IsNullOrEmpty(newItem.Machine.CloneOf) && mapping.ContainsKey(newItem.Machine.CloneOf))
if (!String.IsNullOrEmpty(item.Machine.CloneOf) && mapping.ContainsKey(item.Machine.CloneOf))
{
newItem.Machine.CloneOf = mapping[newItem.Machine.CloneOf];
item.Machine.CloneOf = mapping[item.Machine.CloneOf];
}
// Update romof
if (!String.IsNullOrEmpty(newItem.Machine.RomOf) && mapping.ContainsKey(newItem.Machine.RomOf))
if (!String.IsNullOrEmpty(item.Machine.RomOf) && mapping.ContainsKey(item.Machine.RomOf))
{
newItem.Machine.RomOf = mapping[newItem.Machine.RomOf];
item.Machine.RomOf = mapping[item.Machine.RomOf];
}
// Update sampleof
if (!String.IsNullOrEmpty(newItem.Machine.SampleOf) && mapping.ContainsKey(newItem.Machine.SampleOf))
if (!String.IsNullOrEmpty(item.Machine.SampleOf) && mapping.ContainsKey(item.Machine.SampleOf))
{
newItem.Machine.SampleOf = mapping[newItem.Machine.SampleOf];
item.Machine.SampleOf = mapping[item.Machine.SampleOf];
}
// Add the new newItem to the output list
lock (newItems)
{
newItems.Add(newItem);
// Add the new item to the output list
items.Add(item);
}
});
// Replace the old list of roms with the new one
Remove(key);
@@ -308,7 +302,7 @@ namespace SabreTools.Library.Dats
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> items = this[key];
Parallel.For(0, items.Count, Globals.ParallelOptions, j =>
for (int j = 0; j < items.Count; j++)
{
DatItem item = items[j];
if (item.Type == ItemType.Rom)
@@ -335,11 +329,8 @@ namespace SabreTools.Library.Dats
rom.SHA512 = null;
}
lock (items)
{
items[j] = rom;
}
}
else if (item.Type == ItemType.Disk)
{
Disk disk = (Disk)item;
@@ -364,12 +355,9 @@ namespace SabreTools.Library.Dats
disk.SHA512 = null;
}
lock (items)
{
items[j] = disk;
}
}
});
Remove(key);
AddRange(key, items);

View File

@@ -32,23 +32,17 @@ namespace SabreTools.Library.Dats
{
// Make sure all of the extensions have a dot at the beginning
List<string> newExtA = new List<string>();
Parallel.ForEach(extA, Globals.ParallelOptions, s =>
{
lock (newExtA)
foreach (string s in extA)
{
newExtA.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
}
});
string newExtAString = string.Join(",", newExtA);
List<string> newExtB = new List<string>();
Parallel.ForEach(extB, Globals.ParallelOptions, s =>
{
lock (newExtB)
foreach (string s in extB)
{
newExtB.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant());
}
});
string newExtBString = string.Join(",", newExtB);
// Set all of the appropriate outputs for each of the subsets
@@ -94,7 +88,7 @@ namespace SabreTools.Library.Dats
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> items = this[key];
Parallel.ForEach(items, Globals.ParallelOptions, item =>
foreach (DatItem item in items)
{
if (newExtA.Contains(Path.GetExtension(item.Name.ToUpperInvariant())))
{
@@ -109,7 +103,7 @@ namespace SabreTools.Library.Dats
datdataA.Add(key, item);
datdataB.Add(key, item);
}
});
}
});
// Get the output directory
@@ -316,7 +310,7 @@ namespace SabreTools.Library.Dats
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> items = this[key];
Parallel.ForEach(items, Globals.ParallelOptions, item =>
foreach (DatItem item in items)
{
// If the file is not a Rom or Disk, continue
if (item.Type != ItemType.Disk && item.Type != ItemType.Rom)
@@ -370,7 +364,7 @@ namespace SabreTools.Library.Dats
{
other.Add(key, item);
}
});
}
});
// Get the output directory
@@ -593,7 +587,7 @@ namespace SabreTools.Library.Dats
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
{
List<DatItem> items = this[key];
Parallel.ForEach(items, Globals.ParallelOptions, item =>
foreach(DatItem item in items)
{
// If the file is a Rom
if (item.Type == ItemType.Rom)
@@ -610,7 +604,7 @@ namespace SabreTools.Library.Dats
{
sampledat.Add(key, item);
}
});
}
});
// Get the output directory