Add cache to asset references graph for faster displaying data

#3013
This commit is contained in:
Wojtek Figat
2024-10-25 18:53:10 +02:00
parent 5fb6676ee6
commit 12af7f6cc9

View File

@@ -2,7 +2,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using FlaxEditor.Content; using FlaxEditor.Content;
@@ -137,6 +139,7 @@ namespace FlaxEditor.Windows
} }
} }
private string _cacheFolder;
private Guid _assetId; private Guid _assetId;
private Surface _surface; private Surface _surface;
private Label _loadingLabel; private Label _loadingLabel;
@@ -161,6 +164,9 @@ namespace FlaxEditor.Windows
{ {
Title = assetItem.ShortName + " References"; Title = assetItem.ShortName + " References";
_cacheFolder = Path.Combine(Globals.ProjectCacheFolder, "References");
if (!Directory.Exists(_cacheFolder))
Directory.CreateDirectory(_cacheFolder);
_assetId = assetItem.ID; _assetId = assetItem.ID;
_surface = new Surface(this) _surface = new Surface(this)
{ {
@@ -189,7 +195,7 @@ namespace FlaxEditor.Windows
return node; return node;
} }
private void SearchRefs(Guid assetId) private unsafe void SearchRefs(Guid assetId)
{ {
// Skip assets that never contain references to prevent loading them // Skip assets that never contain references to prevent loading them
if (FlaxEngine.Content.GetAssetInfo(assetId, out var assetInfo) && if (FlaxEngine.Content.GetAssetInfo(assetId, out var assetInfo) &&
@@ -202,8 +208,24 @@ namespace FlaxEditor.Windows
if (_refs.ContainsKey(assetId)) if (_refs.ContainsKey(assetId))
return; return;
// Try to load cached references form previous run
var cachePath = Path.Combine(_cacheFolder, $"{FlaxEngine.Json.JsonSerializer.GetStringID(assetId)}.1.cache");
var hasInfo = FlaxEngine.Content.GetAssetInfo(assetId, out var info);
if (hasInfo && File.Exists(cachePath) && File.GetLastWriteTime(cachePath) > File.GetLastWriteTime(info.Path))
{
byte[] rawData = File.ReadAllBytes(cachePath);
Guid[] loadedRefs = new Guid[rawData.Length / sizeof(Guid)];
if (rawData.Length != 0)
{
fixed (byte* rawDataPtr = rawData)
fixed (Guid* loadedRefsPtr = loadedRefs)
Unsafe.CopyBlock(loadedRefsPtr, rawDataPtr, (uint)rawData.Length);
}
_refs[assetId] = loadedRefs;
return;
}
// Load asset (with cancel support) // Load asset (with cancel support)
//Debug.Log("Searching refs for " + assetInfo.Path);
var obj = FlaxEngine.Object.TryFind<FlaxEngine.Object>(ref assetId); var obj = FlaxEngine.Object.TryFind<FlaxEngine.Object>(ref assetId);
if (obj is Scene scene) if (obj is Scene scene)
{ {
@@ -226,7 +248,21 @@ namespace FlaxEditor.Windows
return; return;
// Get direct references // Get direct references
_refs[assetId] = asset.GetReferences(); var references = asset.GetReferences();
_refs[assetId] = references;
// Save reference to the cache
if (hasInfo)
{
byte[] rawData = new byte[references.Length * sizeof(Guid)];
if (rawData.Length != 0)
{
fixed (byte* rawDataPtr = rawData)
fixed (Guid* referencesPtr = references)
Unsafe.CopyBlock(rawDataPtr, referencesPtr, (uint)rawData.Length);
}
File.WriteAllBytes(cachePath, rawData);
}
} }
private void BuildGraph(AssetNode node, int level, bool reverse) private void BuildGraph(AssetNode node, int level, bool reverse)