Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

Read each file once in a build

Every action asked for the digest of every file it read, and each ask
looked at the file again: on a module of 10 libraries × 10 C++ files a
build with nothing to do asked 17,119 times about 386 files, the headers
of the standard library once per compilation.

A file's digest is now taken the first time it is asked for and kept
until the build writes the file: an action's outputs are forgotten when
it is redone (with its record) and after it ran, files from the remote
cache when they are written, as before. A build with nothing to do on
that module: 402 → 344 ms.
Commit
2fa6348dfb720d4a86fd50ba5e87e71ad2771b82
Author
Leitwolf11 ·
Parents
82ed59cd7f79

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Driver/Actions.cs    | 48 ++++++++++++++-----------------
 tests/Progmasoft.Tazel.Tests/FileTests.cs | 40 ++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 26 deletions(-)

diff --git a/src/Progmasoft.Tazel/Driver/Actions.cs b/src/Progmasoft.Tazel/Driver/Actions.cs
index 45dd149..8fb1c48 100644
--- a/src/Progmasoft.Tazel/Driver/Actions.cs
+++ b/src/Progmasoft.Tazel/Driver/Actions.cs
@@ -69,52 +69,41 @@ internal sealed class BuildAction
 }
 
 /// <summary>
-/// The SHA-256 of files, kept while a file keeps its size and modification time, so a build reads a file once and a
-/// file that is touched but not changed costs one more read.
+/// The SHA-256 of files, each read once in a build: what a file holds is taken the first time an action asks, and kept
+/// until a build writes it (<see cref="Forget"/>). A file most compilations include, a header of the standard library,
+/// is read and looked at once, not once per compilation that names it.
 /// </summary>
 internal sealed class Digests
 {
-    private readonly ConcurrentDictionary<string, (long Size, DateTime Modified, string Digest)> _known = new(StringComparer.Ordinal);
+    // A digest, or null for a file that was not there or could not be read.
+    private readonly ConcurrentDictionary<string, string?> _known = new(StringComparer.Ordinal);
 
     /// <summary>The SHA-256 of <paramref name="bytes"/> as lower-case hexadecimal.</summary>
     public static string Sha256(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes));
 
     /// <summary>The digest of <paramref name="file"/>, or null when it is not a readable regular file.</summary>
-    public string? Of(string file)
+    public string? Of(string file) => _known.GetOrAdd(file, Read);
+
+    /// <summary>Forgets <paramref name="file"/>, before or after a build writes it.</summary>
+    public void Forget(string file) => _known.TryRemove(file, out _);
+
+    private static string? Read(string file)
     {
-        var info = new FileInfo(file);
-        if (!info.Exists)
+        if (!File.Exists(file))
         {
             return null;
         }
 
-        var (size, modified) = (info.Length, info.LastWriteTimeUtc);
-        if (_known.TryGetValue(file, out var seen) && seen.Size == size && seen.Modified == modified)
-        {
-            return seen.Digest;
-        }
-
-        string digest;
         try
         {
             using var stream = File.OpenRead(file);
-            digest = Convert.ToHexStringLower(SHA256.HashData(stream));
-        }
-        catch (IOException)
-        {
-            return null;
+            return Convert.ToHexStringLower(SHA256.HashData(stream));
         }
-        catch (UnauthorizedAccessException)
+        catch (Exception error) when (error is IOException or UnauthorizedAccessException)
         {
             return null;
         }
-
-        _known[file] = (size, modified, digest);
-        return digest;
     }
-
-    /// <summary>Forgets <paramref name="file"/>, after an action wrote it.</summary>
-    public void Forget(string file) => _known.TryRemove(file, out _);
 }
 
 /// <summary>
@@ -172,7 +161,14 @@ internal sealed class ActionCache(Digests digests, bool enabled = true)
     }
 
     /// <summary>Forgets <paramref name="action"/>, so that a failed or stopped run is redone.</summary>
-    public void Forget(BuildAction action) => File.Delete(RecordOf(action));
+    public void Forget(BuildAction action)
+    {
+        File.Delete(RecordOf(action));
+        foreach (var output in action.Outputs)
+        {
+            digests.Forget(action.Full(output));
+        }
+    }
 
     private static string RecordOf(BuildAction action) => action.Full(action.Outputs[0]) + ".action";
 
diff --git a/tests/Progmasoft.Tazel.Tests/FileTests.cs b/tests/Progmasoft.Tazel.Tests/FileTests.cs
index 9b520e6..a1a75d1 100644
--- a/tests/Progmasoft.Tazel.Tests/FileTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/FileTests.cs
@@ -151,4 +151,44 @@ public sealed class ActionCacheTests
             Outputs = ["x"],
         }.Key);
     }
+
+    [Fact]
+    public void AFileIsReadOnceInABuildUntilItIsWritten()
+    {
+        using var module = new TemporaryModule();
+        var file = Path.Combine(module.Root, "a.txt");
+        var digests = new Digests();
+        Assert.Null(digests.Of(file));
+        module.Write("a.txt", "one");
+        // What was asked is kept: a file the build did not write is taken as it was when first asked about.
+        Assert.Null(digests.Of(file));
+        digests.Forget(file);
+        var one = digests.Of(file);
+        Assert.Equal(Digests.Sha256("one"u8.ToArray()), one);
+        module.Write("a.txt", "two");
+        Assert.Equal(one, digests.Of(file));
+        digests.Forget(file);
+        Assert.Equal(Digests.Sha256("two"u8.ToArray()), digests.Of(file));
+    }
+
+    [Fact]
+    public void ForgettingAnActionForgetsWhatItsOutputsHeld()
+    {
+        using var module = new TemporaryModule();
+        module.Write("in.txt", "one");
+        var action = new BuildAction
+        {
+            Description = "copy",
+            Command = ["cp", "in.txt", "out.txt"],
+            Directory = module.Root,
+            Inputs = ["in.txt"],
+            Outputs = ["out.txt"],
+        };
+        var digests = new Digests();
+        module.Write("out.txt", "old");
+        var old = digests.Of(Path.Combine(module.Root, "out.txt"));
+        new ActionCache(digests).Forget(action);
+        module.Write("out.txt", "new");
+        Assert.NotEqual(old, digests.Of(Path.Combine(module.Root, "out.txt")));
+    }
 }