Use caching for filesystem queries in BindingsGenerator

This commit is contained in:
2023-03-14 19:47:18 +02:00
parent 47e2c82551
commit 2f9e72e03e
3 changed files with 55 additions and 36 deletions

View File

@@ -253,7 +253,7 @@ namespace Flax.Build.Bindings
{ {
// Version // Version
writer.Write(CacheVersion); writer.Write(CacheVersion);
writer.Write(File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks); writer.Write(FileCache.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks);
// Build options // Build options
writer.Write(moduleOptions.IntermediateFolder); writer.Write(moduleOptions.IntermediateFolder);
@@ -270,7 +270,7 @@ namespace Flax.Build.Bindings
{ {
var headerFile = headerFiles[i]; var headerFile = headerFiles[i];
writer.Write(headerFile); writer.Write(headerFile);
writer.Write(File.GetLastWriteTime(headerFile).Ticks); writer.Write(FileCache.GetLastWriteTime(headerFile).Ticks);
} }
// Info // Info
@@ -281,7 +281,7 @@ namespace Flax.Build.Bindings
private static bool LoadCache(ref ModuleInfo moduleInfo, BuildOptions moduleOptions, List<string> headerFiles) private static bool LoadCache(ref ModuleInfo moduleInfo, BuildOptions moduleOptions, List<string> headerFiles)
{ {
var path = GetCachePath(moduleInfo.Module, moduleOptions); var path = GetCachePath(moduleInfo.Module, moduleOptions);
if (!File.Exists(path)) if (!FileCache.Exists(path))
return false; return false;
try try
{ {
@@ -292,7 +292,7 @@ namespace Flax.Build.Bindings
var version = reader.ReadInt32(); var version = reader.ReadInt32();
if (version != CacheVersion) if (version != CacheVersion)
return false; return false;
if (File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks != reader.ReadInt64()) if (FileCache.GetLastWriteTime(Assembly.GetExecutingAssembly().Location).Ticks != reader.ReadInt64())
return false; return false;
// Build options // Build options
@@ -320,7 +320,7 @@ namespace Flax.Build.Bindings
var headerFile = headerFiles[i]; var headerFile = headerFiles[i];
if (headerFile != reader.ReadString()) if (headerFile != reader.ReadString())
return false; return false;
if (File.GetLastWriteTime(headerFile).Ticks > reader.ReadInt64()) if (FileCache.GetLastWriteTime(headerFile).Ticks > reader.ReadInt64())
return false; return false;
} }

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace Flax.Build
{
/// <summary>
/// Cache filesystem related queries like File.Exists and File.GetLastWriteTime.
/// </summary>
public static class FileCache
{
private static Dictionary<string, FileInfo> fileInfoCache = new Dictionary<string, FileInfo>();
public static void FileRemoveFromCache(string path)
{
//fileInfoCache[path].Refresh();
fileInfoCache.Remove(path);
}
public static bool Exists(string path)
{
if (fileInfoCache.TryGetValue(path, out var fileInfo))
return fileInfo.Exists;
fileInfo = new FileInfo(path);
fileInfoCache.Add(path, fileInfo);
return fileInfo.Exists;
}
public static DateTime GetLastWriteTime(string path)
{
if (fileInfoCache.TryGetValue(path, out var fileInfo))
return fileInfo.LastWriteTime;
fileInfo = new FileInfo(path);
fileInfoCache.Add(path, fileInfo);
return fileInfo.LastWriteTime;
}
}
}

View File

@@ -17,8 +17,6 @@ namespace Flax.Build.NativeCpp
private static Dictionary<string, string[]> AllIncludesCache = new(); private static Dictionary<string, string[]> AllIncludesCache = new();
private static Dictionary<string, DateTime> DirectIncludesTimestampCache = new(); private static Dictionary<string, DateTime> DirectIncludesTimestampCache = new();
private static Dictionary<string, DateTime> AllIncludesTimestampCache = new(); private static Dictionary<string, DateTime> AllIncludesTimestampCache = new();
private static Dictionary<string, bool> FileExistsCache = new();
private static Dictionary<string, DateTime> FileTimestampCache = new();
private static readonly string IncludeToken = "include"; private static readonly string IncludeToken = "include";
private static string CachePath; private static string CachePath;
@@ -141,26 +139,6 @@ namespace Flax.Build.NativeCpp
} }
} }
private static bool FileExists(string path)
{
if (FileExistsCache.TryGetValue(path, out bool result))
return result;
result = File.Exists(path);
FileExistsCache.Add(path, result);
return result;
}
private static DateTime FileLastWriteTime(string path)
{
if (FileTimestampCache.TryGetValue(path, out DateTime result))
return result;
result = File.GetLastWriteTime(path);
FileTimestampCache.Add(path, result);
return result;
}
/// <summary> /// <summary>
/// Finds all included files by the source file (including dependencies). /// Finds all included files by the source file (including dependencies).
/// </summary> /// </summary>
@@ -176,7 +154,7 @@ namespace Flax.Build.NativeCpp
{ {
if (AllIncludesTimestampCache.TryGetValue(sourceFile, out var cachedTimestamp)) if (AllIncludesTimestampCache.TryGetValue(sourceFile, out var cachedTimestamp))
{ {
lastModified = FileLastWriteTime(sourceFile); lastModified = FileCache.GetLastWriteTime(sourceFile);
if (lastModified == cachedTimestamp) if (lastModified == cachedTimestamp)
return result; return result;
} }
@@ -185,7 +163,7 @@ namespace Flax.Build.NativeCpp
AllIncludesTimestampCache.Remove(sourceFile); AllIncludesTimestampCache.Remove(sourceFile);
} }
if (!FileExists(sourceFile)) if (!FileCache.Exists(sourceFile))
throw new Exception(string.Format("Cannot scan file \"{0}\" for includes because it does not exist.", sourceFile)); throw new Exception(string.Format("Cannot scan file \"{0}\" for includes because it does not exist.", sourceFile));
//using (new ProfileEventScope("FindAllIncludedFiles")) //using (new ProfileEventScope("FindAllIncludedFiles"))
@@ -231,7 +209,7 @@ namespace Flax.Build.NativeCpp
private static string[] GetDirectIncludes(string sourceFile) private static string[] GetDirectIncludes(string sourceFile)
{ {
if (!FileExists(sourceFile)) if (!FileCache.Exists(sourceFile))
return Array.Empty<string>(); return Array.Empty<string>();
DateTime? lastModified = null; DateTime? lastModified = null;
@@ -241,7 +219,7 @@ namespace Flax.Build.NativeCpp
{ {
if (DirectIncludesTimestampCache.TryGetValue(sourceFile, out var cachedTimestamp)) if (DirectIncludesTimestampCache.TryGetValue(sourceFile, out var cachedTimestamp))
{ {
lastModified = FileLastWriteTime(sourceFile); lastModified = FileCache.GetLastWriteTime(sourceFile);
if (lastModified == cachedTimestamp) if (lastModified == cachedTimestamp)
return result; return result;
} }
@@ -325,11 +303,11 @@ namespace Flax.Build.NativeCpp
// Relative to the workspace root // Relative to the workspace root
var includedFilePath = Path.Combine(Globals.Root, "Source", includedFile); var includedFilePath = Path.Combine(Globals.Root, "Source", includedFile);
if (!FileExists(includedFilePath)) if (!FileCache.Exists(includedFilePath))
{ {
// Relative to the source file // Relative to the source file
includedFilePath = Path.Combine(sourceFileFolder, includedFile); includedFilePath = Path.Combine(sourceFileFolder, includedFile);
if (!FileExists(includedFilePath)) if (!FileCache.Exists(includedFilePath))
{ {
// Relative to any of the included project workspaces // Relative to any of the included project workspaces
var project = Globals.Project; var project = Globals.Project;
@@ -337,7 +315,7 @@ namespace Flax.Build.NativeCpp
foreach (var reference in project.References) foreach (var reference in project.References)
{ {
includedFilePath = Path.Combine(reference.Project.ProjectFolderPath, "Source", includedFile); includedFilePath = Path.Combine(reference.Project.ProjectFolderPath, "Source", includedFile);
if (FileExists(includedFilePath)) if (FileCache.Exists(includedFilePath))
{ {
isValid = true; isValid = true;
break; break;
@@ -348,7 +326,7 @@ namespace Flax.Build.NativeCpp
if (!isValid && isLibraryInclude) if (!isValid && isLibraryInclude)
{ {
includedFilePath = Path.Combine(Globals.Root, "Source", "ThirdParty", includedFile); includedFilePath = Path.Combine(Globals.Root, "Source", "ThirdParty", includedFile);
if (FileExists(includedFilePath)) if (FileCache.Exists(includedFilePath))
{ {
isValid = true; isValid = true;
} }
@@ -375,7 +353,7 @@ namespace Flax.Build.NativeCpp
result = includedFiles.ToArray(); result = includedFiles.ToArray();
DirectIncludesCache.Add(sourceFile, result); DirectIncludesCache.Add(sourceFile, result);
if (!DirectIncludesTimestampCache.ContainsKey(sourceFile)) if (!DirectIncludesTimestampCache.ContainsKey(sourceFile))
DirectIncludesTimestampCache.Add(sourceFile, lastModified ?? FileLastWriteTime(sourceFile)); DirectIncludesTimestampCache.Add(sourceFile, lastModified ?? FileCache.GetLastWriteTime(sourceFile));
return result; return result;
} }
} }