1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 04:22:54 +08:00

Cache skin filename to path mapping to reduce realm lookups during gameplay skin changes

This commit is contained in:
Dean Herbert 2021-12-01 02:27:41 +09:00
parent ef3d0ee0db
commit 9411b42d0a

View File

@ -3,10 +3,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Game.Database;
using osu.Game.Extensions; using osu.Game.Extensions;
using Realms; using Realms;
@ -14,12 +12,36 @@ namespace osu.Game.Skinning
{ {
public class LegacyDatabasedSkinResourceStore : ResourceStore<byte[]> public class LegacyDatabasedSkinResourceStore : ResourceStore<byte[]>
{ {
private readonly ILive<SkinInfo> source; private readonly Dictionary<string, string> fileToStoragePathMapping = new Dictionary<string, string>();
private readonly IDisposable subscription;
public LegacyDatabasedSkinResourceStore(SkinInfo source, IResourceStore<byte[]> underlyingStore) public LegacyDatabasedSkinResourceStore(SkinInfo source, IResourceStore<byte[]> underlyingStore)
: base(underlyingStore) : base(underlyingStore)
{ {
this.source = source.ToLive(); subscription = source.Files.SubscribeForNotifications((sender, changes, error) =>
{
if (changes == null)
return;
// If a large number of changes are made on skin files, this may be better suited to being cleared here
// and reinitialised on next usage.
initialiseFileCache(source);
});
initialiseFileCache(source);
}
~LegacyDatabasedSkinResourceStore()
{
Dispose(false);
}
private void initialiseFileCache(SkinInfo source)
{
fileToStoragePathMapping.Clear();
foreach (var f in source.Files)
fileToStoragePathMapping[f.Filename.ToLowerInvariant()] = f.File.GetStoragePath();
} }
protected override IEnumerable<string> GetFilenames(string name) protected override IEnumerable<string> GetFilenames(string name)
@ -32,18 +54,16 @@ namespace osu.Game.Skinning
} }
} }
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
subscription?.Dispose();
}
private string getPathForFile(string filename) => private string getPathForFile(string filename) =>
source.PerformRead(s => fileToStoragePathMapping.TryGetValue(filename.ToLower(), out string path) ? path : null;
{
if (s.IsManaged)
{
// avoid enumerating all files if this is a managed realm instance.
return s.Files.Filter(@"Filename ==[c] $0", filename).FirstOrDefault()?.File.GetStoragePath();
}
return s.Files.FirstOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.File.GetStoragePath(); public override IEnumerable<string> GetAvailableResources() => fileToStoragePathMapping.Keys;
});
public override IEnumerable<string> GetAvailableResources() => source.PerformRead(s => s.Files.Select(f => f.Filename));
} }
} }