From 70b6e98b2fc550a52d8569e490886cc2864d02f2 Mon Sep 17 00:00:00 2001 From: Evan Dixon Date: Thu, 29 Dec 2016 20:11:51 -0600 Subject: [PATCH] More database speed improvements Extending 8b274ddbc35e810608744d47c1bb873682b4d3b6 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. --- PKHeX/Subforms/SAV_Database.cs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/PKHeX/Subforms/SAV_Database.cs b/PKHeX/Subforms/SAV_Database.cs index 840dab732..b01dcadc2 100644 --- a/PKHeX/Subforms/SAV_Database.cs +++ b/PKHeX/Subforms/SAV_Database.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; @@ -81,27 +82,22 @@ namespace PKHeX p.ContextMenuStrip = mnu; // Load Data - RawDB = new List(); + var dbTemp = new ConcurrentBag(); 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(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(RawDB.Where(pk => pk.ChecksumValid && pk.Species != 0 && pk.Sanity == 0)); - RawDB = new List(RawDB.Distinct()); + RawDB = new List(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) =>