From 3b1166d1e665594d0cc24f8b3d1ef010928ec70f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 1 Aug 2017 17:37:21 +0900 Subject: [PATCH] Optimise file lookups and other database operations FirstOrDefault when called on a TableQuery with a predicate doesn't use table indices --- osu.Game/Beatmaps/BeatmapManager.cs | 4 ++-- osu.Game/Database/DatabaseBackedStore.cs | 2 +- osu.Game/IO/FileStore.cs | 12 +++++------- osu.Game/Rulesets/RulesetStore.cs | 2 +- osu.sln.DotSettings | 2 +- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index 6059db0a36..105c8d9623 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -174,7 +174,7 @@ namespace osu.Game.Beatmaps if (!beatmaps.Delete(beatmapSet)) return; if (!beatmapSet.Protected) - files.Dereference(beatmapSet.Files.Select(f => f.FileInfo)); + files.Dereference(beatmapSet.Files.Select(f => f.FileInfo).ToArray()); } /// @@ -188,7 +188,7 @@ namespace osu.Game.Beatmaps if (!beatmaps.Undelete(beatmapSet)) return; if (!beatmapSet.Protected) - files.Reference(beatmapSet.Files.Select(f => f.FileInfo)); + files.Reference(beatmapSet.Files.Select(f => f.FileInfo).ToArray()); } /// diff --git a/osu.Game/Database/DatabaseBackedStore.cs b/osu.Game/Database/DatabaseBackedStore.cs index e3afd9688a..d8e2e35bd7 100644 --- a/osu.Game/Database/DatabaseBackedStore.cs +++ b/osu.Game/Database/DatabaseBackedStore.cs @@ -41,7 +41,7 @@ namespace osu.Game.Database { var storeName = GetType().Name; - var reportedVersion = Connection.Table().FirstOrDefault(s => s.StoreName == storeName) ?? new StoreVersion + var reportedVersion = Connection.Table().Where(s => s.StoreName == storeName).FirstOrDefault() ?? new StoreVersion { StoreName = storeName, Version = 0 diff --git a/osu.Game/IO/FileStore.cs b/osu.Game/IO/FileStore.cs index e55af2e23a..8097f1f3a3 100644 --- a/osu.Game/IO/FileStore.cs +++ b/osu.Game/IO/FileStore.cs @@ -2,7 +2,6 @@ // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; -using System.Collections.Generic; using System.IO; using System.Linq; using osu.Framework.Extensions; @@ -83,10 +82,9 @@ namespace osu.Game.IO { string hash = data.ComputeSHA2Hash(); - var info = new FileInfo { Hash = hash }; - - var existing = Connection.Table().FirstOrDefault(f => f.Hash == info.Hash); + var existing = Connection.Table().Where(f => f.Hash == hash).FirstOrDefault(); + var info = existing ?? new FileInfo { Hash = hash }; if (existing != null) { info = existing; @@ -106,11 +104,11 @@ namespace osu.Game.IO Connection.Insert(info); } - Reference(new[] { info }); + Reference(info); return info; } - public void Reference(IEnumerable files) + public void Reference(params FileInfo[] files) { Connection.RunInTransaction(() => { @@ -125,7 +123,7 @@ namespace osu.Game.IO }); } - public void Dereference(IEnumerable files) + public void Dereference(params FileInfo[] files) { Connection.RunInTransaction(() => { diff --git a/osu.Game/Rulesets/RulesetStore.cs b/osu.Game/Rulesets/RulesetStore.cs index 88aee2bffc..1564df1366 100644 --- a/osu.Game/Rulesets/RulesetStore.cs +++ b/osu.Game/Rulesets/RulesetStore.cs @@ -62,7 +62,7 @@ namespace osu.Game.Rulesets { var us = createRulesetInfo(r); - var existing = Query().FirstOrDefault(ri => ri.InstantiationInfo == us.InstantiationInfo); + var existing = Query().Where(ri => ri.InstantiationInfo == us.InstantiationInfo).FirstOrDefault(); if (existing == null) Connection.Insert(us); diff --git a/osu.sln.DotSettings b/osu.sln.DotSettings index e3eae96ca8..06d160ad31 100644 --- a/osu.sln.DotSettings +++ b/osu.sln.DotSettings @@ -115,7 +115,7 @@ WARNING WARNING WARNING - WARNING + HINT WARNING WARNING WARNING