Skip to content

Commit

Permalink
Fix file timestamps when using RefCache
Browse files Browse the repository at this point in the history
  • Loading branch information
safesparrow committed Nov 4, 2023
1 parent 653a543 commit 059ad4e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions MSBuild.CompilerCache/CompilerCacheLocate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public override bool Execute()
var (inMemoryRefCache, fileHashCache) = GetInMemoryRefCache();
var locator = new LocatorAndPopulator(inMemoryRefCache, fileHashCache);
var inputs = GatherInputs();
void LogTime(string name) => Log.LogWarning($"[{sw.ElapsedMilliseconds}ms] {name}");
void LogTime(string name) => Log.LogMessage($"[{sw.ElapsedMilliseconds}ms] {name}");
var results = locator.Locate(inputs, Log, LogTime);
if (results.PopulateCache)
{
Guid = guid.ToString();
BuildEngine4.RegisterTaskObject(guid.ToString(), locator, RegisteredTaskObjectLifetime.Build, false);
Log.LogWarning($"Locate - registered {nameof(LocatorAndPopulator)} object at Guid key {guid}");
Log.LogMessage($"Locate - registered {nameof(LocatorAndPopulator)} object at Guid key {guid}");
}
RunCompilation = results.RunCompilation;
PopulateCache = results.PopulateCache;
Expand Down
2 changes: 1 addition & 1 deletion MSBuild.CompilerCache/CompilerCachePopulateCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Execute()
var locator = _locator as LocatorAndPopulator ??
throw new Exception("Cached result is of unexpected type");
var sw = Stopwatch.StartNew();
void LogTime(string name) => Log.LogWarning($"[{sw.ElapsedMilliseconds}ms] {name}");
void LogTime(string name) => Log.LogMessage($"[{sw.ElapsedMilliseconds}ms] {name}");
var results = locator.PopulateCache(Log, LogTime);
return true;
}
Expand Down
29 changes: 18 additions & 11 deletions MSBuild.CompilerCache/LocatorAndPopulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ internal static LocalInputs CalculateLocalInputs(DecomposedCompilerProps decompo
{
return dlls.Select(dll =>
{
var fileInfo = new FileInfo(dll);
var lastWrite = fileInfo.LastWriteTimeUtc;
var length = fileInfo.Length;
var allRefData = GetAllRefData(dll, refCache, fileHashCache, hasher);
return AllRefDataToExtract(allRefData, assemblyName, trimmingConfig.IgnoreInternalsIfPossible);
var data = AllRefDataToExtract(allRefData, assemblyName, trimmingConfig.IgnoreInternalsIfPossible);
return data with { Info = data.Info with {LastWriteTimeUtc = lastWrite, Length = length} };
}).ToArray();
})
.ToImmutableArray()
Expand All @@ -256,16 +260,17 @@ private static LocalFileExtract GetLocalFileExtract(FileInfo fileInfo, FileHashC
return new LocalFileExtract(Info: fileHash, Hash: hashString);
}

private static LocalFileExtract GetLocalFileExtract(FileHashCacheKey fileHash, IFileHashCache fileHashCache)
public static string CreateHashString(FileHashCacheKey fileHashCacheKey)
{
var hashString = fileHashCache.Get(fileHash);
if (hashString == null)
{
var bytes = File.ReadAllBytes(fileHash.FullName);
hashString = Utils.BytesToHashHex(bytes);
fileHashCache.Set(fileHash, hashString);
}
return new LocalFileExtract(Info: fileHash, Hash: hashString);
var bytes = File.ReadAllBytes(fileHashCacheKey.FullName);
return Utils.BytesToHashHex(bytes);
}

private static LocalFileExtract GetLocalFileExtract(FileHashCacheKey fileKey, IFileHashCache fileHashCache)
{
var hashString = fileHashCache.GetOrSet(fileKey, CreateHashString);

return new LocalFileExtract(Info: fileKey, Hash: hashString);
}

private static CompilationMetadata GetCompilationMetadata(DateTime postCompilationTimeUtc) =>
Expand Down Expand Up @@ -415,7 +420,7 @@ void RefasmCompiledDlls()
var toBeCached = trimmer.GenerateRefData(ImmutableArray.Create(bytes));
var fileInfo = new FileInfo(dll.LocalPath);
var fileCacheKey = FileHashCacheKey.FromFileInfo(fileInfo);
var hashString = _fileHashCache.Get(fileCacheKey);
var hashString = _fileHashCache.GetOrSet(fileCacheKey, CreateHashString);
var extract = new LocalFileExtract(fileCacheKey, hashString);
var name = Path.GetFileNameWithoutExtension(fileInfo.Name);
var cacheKey = BuildRefCacheKey(name, hashString);
Expand Down Expand Up @@ -500,6 +505,8 @@ await JsonSerializer.SerializeAsync(fs, metadata,
saveInputsTask.GetAwaiter().GetResult();

var tempZipPath = baseTmpDir.CombineAsFile($"{hashForFileName}.zip");
// TODO Create zip archive from in-memory data rather than files on disk

ZipFile.CreateFromDirectory(outputsDir.FullName, tempZipPath.FullName,
CompressionLevel.NoCompression, includeBaseDirectory: false);
return tempZipPath;
Expand Down

0 comments on commit 059ad4e

Please sign in to comment.