Skip to content

Commit

Permalink
Remove precomputed cache part
Browse files Browse the repository at this point in the history
  • Loading branch information
Forgind committed Dec 21, 2020
1 parent 92e5b6d commit c45c079
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 210 deletions.
108 changes: 0 additions & 108 deletions src/Tasks.UnitTests/RARPrecomputedCache_Tests.cs

This file was deleted.

35 changes: 6 additions & 29 deletions src/Tasks/AssemblyDependency/ResolveAssemblyReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1859,44 +1859,21 @@ private void LogConflict(Reference reference, string fusionName, StringBuilder l
/// <summary>
/// Reads the state file (if present) into the cache. If not present, attempts to read from CacheInputPaths, then creates a new cache if necessary.
/// </summary>
internal void ReadStateFile(GetLastWriteTime getLastWriteTime, AssemblyTableInfo[] installedAssemblyTableInfo, Func<string, bool> fileExists = null)
internal void ReadStateFile(GetLastWriteTime getLastWriteTime, AssemblyTableInfo[] installedAssemblyTableInfo)
{
var deserializeOptions = new JsonSerializerOptions() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
deserializeOptions.Converters.Add(new SystemState.Converter());
try
{
_cache = JsonSerializer.Deserialize<SystemState>(File.ReadAllText(_stateFile), deserializeOptions);
}
catch (Exception)
{
// log message
}

if (_cache == null)
{
_cache = SystemState.DeserializePrecomputedCaches(AssemblyInformationCachePaths ?? Array.Empty<ITaskItem>(), Log, typeof(SystemState), getLastWriteTime, installedAssemblyTableInfo, fileExists);
}
else
{
_cache.SetGetLastWriteTime(getLastWriteTime);
_cache.SetInstalledAssemblyInformation(installedAssemblyTableInfo);
}
_cache = JsonSerializer.Deserialize<SystemState>(File.ReadAllText(_stateFile), SystemState.JsonSerializerOptions);
_cache.SetGetLastWriteTime(getLastWriteTime);
_cache.SetInstalledAssemblyInformation(installedAssemblyTableInfo);
}

/// <summary>
/// If CacheOutputPath is non-null, writes out a cache to that location. Otherwise, writes out the state file if a state name was supplied and the cache is dirty.
/// </summary>
internal void WriteStateFile()
{
if (!string.IsNullOrEmpty(AssemblyInformationCacheOutputPath))
{
_cache.SerializePrecomputedCache(AssemblyInformationCacheOutputPath, Log);
}
else if (!string.IsNullOrEmpty(_stateFile) && _cache.IsDirty)
if (!string.IsNullOrEmpty(_stateFile) && _cache.IsDirty)
{
var deserializeOptions = new JsonSerializerOptions() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
deserializeOptions.Converters.Add(new SystemState.Converter());
File.WriteAllText(_stateFile, JsonSerializer.Serialize<SystemState>(_cache, deserializeOptions));
File.WriteAllText(_stateFile, JsonSerializer.Serialize<SystemState>(_cache, SystemState.JsonSerializerOptions));
}
}
#endregion
Expand Down
97 changes: 24 additions & 73 deletions src/Tasks/SystemState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ internal sealed class SystemState
/// </summary>
private GetAssemblyRuntimeVersion getAssemblyRuntimeVersion;

/// <summary>
/// Options for serializing and deserializing SystemStates.
/// </summary>
private static JsonSerializerOptions jsonSerializerOptions;

internal static JsonSerializerOptions JsonSerializerOptions
{
get
{
if (jsonSerializerOptions == null)
{
jsonSerializerOptions = new JsonSerializerOptions() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
jsonSerializerOptions.Converters.Add(new SystemState.Converter());
}

return jsonSerializerOptions;
}
}

/// <summary>
/// Class that holds the current file state.
/// </summary>
Expand Down Expand Up @@ -673,80 +692,12 @@ out fileState.frameworkName
}

/// <summary>
/// Reads in cached data from stateFiles to build an initial cache. Avoids logging warnings or errors.
/// </summary>
internal static SystemState DeserializePrecomputedCaches(ITaskItem[] stateFiles, TaskLoggingHelper log, Type requiredReturnType, GetLastWriteTime getLastWriteTime, AssemblyTableInfo[] installedAssemblyTableInfo, Func<string, bool> fileExists)
{
SystemState retVal = new SystemState();
retVal.SetGetLastWriteTime(getLastWriteTime);
retVal.SetInstalledAssemblyInformation(installedAssemblyTableInfo);
retVal.isDirty = stateFiles.Length > 0;
HashSet<string> assembliesFound = new HashSet<string>();
fileExists ??= FileSystems.Default.FileExists;

foreach (ITaskItem stateFile in stateFiles)
{
// Verify that it's a real stateFile; log message but do not error if not
var deserializeOptions = new JsonSerializerOptions() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
deserializeOptions.Converters.Add(new SystemState.Converter());
SystemState sysBase = JsonSerializer.Deserialize<SystemState>(File.ReadAllText(stateFile.ToString()), deserializeOptions);
if (sysBase == null)
{
continue;
}

foreach (KeyValuePair<string, FileState> kvp in sysBase.instanceLocalFileStateCache)
{
string relativePath = kvp.Key;
if (!assembliesFound.Contains(relativePath))
{
FileState fileState = kvp.Value;
string fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(stateFile.ToString()), relativePath));
if (fileExists(fullPath))
{
// Correct file path and timestamp
fileState.LastModified = retVal.getLastWriteTime(fullPath);
retVal.instanceLocalFileStateCache[fullPath] = fileState;
assembliesFound.Add(relativePath);
}
}
}
}

return retVal;
}

/// <summary>
/// Modifies this object to be more portable across machines, then writes it to stateFile.
/// Cached implementation of GetDirectories.
/// </summary>
internal void SerializePrecomputedCache(string stateFile, TaskLoggingHelper log)
{
Dictionary<string, FileState> oldInstanceLocalFileStateCache = instanceLocalFileStateCache;
Dictionary<string, FileState> newInstanceLocalFileStateCache = new Dictionary<string, FileState>(instanceLocalFileStateCache.Count);
foreach (KeyValuePair<string, FileState> kvp in instanceLocalFileStateCache)
{
string relativePath = FileUtilities.MakeRelative(Path.GetDirectoryName(stateFile), kvp.Key);
newInstanceLocalFileStateCache[relativePath] = kvp.Value;
}
instanceLocalFileStateCache = newInstanceLocalFileStateCache;

if (FileUtilities.FileExistsNoThrow(stateFile))
{
log.LogWarningWithCodeFromResources("General.StateFileAlreadyPresent", stateFile);
}
JsonSerializerOptions options = new JsonSerializerOptions() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
options.Converters.Add(new SystemState.Converter());
File.WriteAllText(stateFile, JsonSerializer.Serialize(this, options));
instanceLocalFileStateCache = oldInstanceLocalFileStateCache;
}

/// <summary>
/// Cached implementation of GetDirectories.
/// </summary>
/// <param name="path"></param>
/// <param name="pattern"></param>
/// <returns></returns>
private string[] GetDirectories(string path, string pattern)
/// <param name="path"></param>
/// <param name="pattern"></param>
/// <returns></returns>
private string[] GetDirectories(string path, string pattern)
{
// Only cache the *. pattern. This is by far the most common pattern
// and generalized caching would require a call to Path.Combine which
Expand Down

0 comments on commit c45c079

Please sign in to comment.