More database speed improvements

Extending 8b274ddbc3

Changes and their reasoning:
- Used a concurrent bag inside Parallel.ForEach to avoid the overhead of lock.
- Removed `RawDB.Where(pk => pk != null)` because the Parallel.ForEach lambda handles this check
- Because LINQ extension methods' execution is deferred, it's best to wait to create a list until it's needed, so the extension methods were changed together without creating intermediate lists.
This commit is contained in:
Evan Dixon
2016-12-29 20:11:51 -06:00
parent fe26b5d8a1
commit 70b6e98b2f

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
@@ -81,27 +82,22 @@ public SAV_Database(Main f1)
p.ContextMenuStrip = mnu;
// Load Data
RawDB = new List<PKM>();
var dbTemp = new ConcurrentBag<PKM>();
var files = Directory.GetFiles(DatabasePath, "*", SearchOption.AllDirectories);
Parallel.ForEach(files, file =>
{
FileInfo fi = new FileInfo(file);
if (!fi.Extension.Contains(".pk") || !PKX.getIsPKM(fi.Length)) return;
var pk = PKMConverter.getPKMfromBytes(File.ReadAllBytes(file), file);
if (pk == null)
return;
lock (RawDB)
RawDB.Add(pk);
if (pk != null)
dbTemp.Add(pk);
});
RawDB = new List<PKM>(RawDB.Where(pk => pk != null).OrderBy(pk => pk.Identifier));
// Fetch from save file
foreach (var pkm in Main.SAV.BoxData.Where(pk => pk.Species != 0))
RawDB.Add(pkm);
// Prepare Database
RawDB = new List<PKM>(RawDB.Where(pk => pk.ChecksumValid && pk.Species != 0 && pk.Sanity == 0));
RawDB = new List<PKM>(RawDB.Distinct());
RawDB = new List<PKM>(dbTemp.OrderBy(pk => pk.Identifier)
.Concat(Main.SAV.BoxData.Where(pk => pk.Species != 0)) // Fetch from save file
.Where(pk => pk.ChecksumValid && pk.Species != 0 && pk.Sanity == 0)
.Distinct());
setResults(RawDB);
Menu_SearchSettings.DropDown.Closing += (sender, e) =>