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

fix(cadre-celeste): clear SQLite pool on Load for Windows and lower AbiFingerprint complexity

Motivation:
CI on Progmasoft/tazel#58 reported two issues: Windows failed temp-directory
cleanup because Microsoft.Data.Sqlite connection pooling kept a handle open
after CadreCelesteStore.Load, and Codacy flagged cyclomatic complexity > 10 in
AbiFingerprint.NormalizeHeaderTokens (12) and TrySkipComment (11).

Changes:
- Call SqliteConnection.ClearPool(conn) in CadreCelesteStore.Load before
  disposing the connection.
- Split AbiFingerprint.NormalizeHeaderTokens and TrySkipComment into focused
  helpers (StepNormalize, SkipLineComment, SkipBlockComment, NeedsSpaceSeparator,
  AppendNewlineIfNeeded), bringing all methods well below complexity 10.

Verification:
- Built Tazel.slnx -c Release: 0 warnings, 0 errors.
- Passed locally: CadreCelestePersistenceAndAbiTests (3/3).
Commit
e425aeec1f51fabcd03c6d9c2f7f93dddeedc513
Author
Leitwolf11 ·
Parents
f6a3d2219b1a

Files · Download .tar.gz

Changes

 .../AbiFingerprint.cs                              | 119 ++++++++++++---------
 .../CadreCelesteStore.cs                           |   1 +
 2 files changed, 69 insertions(+), 51 deletions(-)

diff --git a/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs b/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs
index 232bd59..5bafcb2 100644
--- a/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs
+++ b/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs
@@ -22,8 +22,7 @@ public static class AbiFingerprint
     public static ContentHash HashHeaderTokens(string source)
     {
         ArgumentNullException.ThrowIfNull(source);
-        var normalized = NormalizeHeaderTokens(source);
-        return ContentHash.FromString(normalized);
+        return ContentHash.FromString(NormalizeHeaderTokens(source));
     }
 
     /// <summary>
@@ -52,49 +51,58 @@ public static class AbiFingerprint
 
         while (i < source.Length)
         {
-            var ch = source[i];
-            if (TrySkipComment(source, ref i))
-            {
-                pendingSpace = sb.Length > 0 && sb[^1] != '\n';
-                continue;
-            }
+            i = StepNormalize(source, i, sb, ref pendingSpace);
+        }
 
-            if (ch == '\r' || ch == '\n')
-            {
-                i++;
-                if (sb.Length > 0 && sb[^1] != '\n')
-                {
-                    sb.Append('\n');
-                }
-
-                pendingSpace = false;
-                continue;
-            }
+        return sb.ToString().TrimEnd('\n', ' ');
+    }
 
-            if (char.IsWhiteSpace(ch))
-            {
-                i++;
-                pendingSpace = sb.Length > 0 && sb[^1] != '\n';
-                continue;
-            }
+    private static int StepNormalize(string source, int i, StringBuilder sb, ref bool pendingSpace)
+    {
+        var ch = source[i];
+        if (TrySkipComment(source, ref i))
+        {
+            pendingSpace = NeedsSpaceSeparator(sb);
+            return i;
+        }
 
-            if (pendingSpace)
-            {
-                sb.Append(' ');
-                pendingSpace = false;
-            }
+        if (ch is '\r' or '\n')
+        {
+            AppendNewlineIfNeeded(sb);
+            pendingSpace = false;
+            return i + 1;
+        }
 
-            if (ch is '"' or '\'')
-            {
-                CopyQuotedLiteral(source, ref i, ch, sb);
-                continue;
-            }
+        if (char.IsWhiteSpace(ch))
+        {
+            pendingSpace = NeedsSpaceSeparator(sb);
+            return i + 1;
+        }
 
-            sb.Append(ch);
-            i++;
+        if (pendingSpace)
+        {
+            sb.Append(' ');
+            pendingSpace = false;
         }
 
-        return sb.ToString().TrimEnd('\n', ' ');
+        if (ch is '"' or '\'')
+        {
+            CopyQuotedLiteral(source, ref i, ch, sb);
+            return i;
+        }
+
+        sb.Append(ch);
+        return i + 1;
+    }
+
+    private static bool NeedsSpaceSeparator(StringBuilder sb) => sb.Length > 0 && sb[^1] != '\n';
+
+    private static void AppendNewlineIfNeeded(StringBuilder sb)
+    {
+        if (NeedsSpaceSeparator(sb))
+        {
+            sb.Append('\n');
+        }
     }
 
     private static bool TrySkipComment(string source, ref int i)
@@ -106,30 +114,39 @@ public static class AbiFingerprint
 
         if (source[i + 1] == '/')
         {
-            i += 2;
-            while (i < source.Length && source[i] != '\n')
-            {
-                i++;
-            }
-
+            i = SkipLineComment(source, i + 2);
             return true;
         }
 
         if (source[i + 1] == '*')
         {
-            i += 2;
-            while (i + 1 < source.Length && !(source[i] == '*' && source[i + 1] == '/'))
-            {
-                i++;
-            }
-
-            i = i + 1 < source.Length ? i + 2 : source.Length;
+            i = SkipBlockComment(source, i + 2);
             return true;
         }
 
         return false;
     }
 
+    private static int SkipLineComment(string source, int i)
+    {
+        while (i < source.Length && source[i] != '\n')
+        {
+            i++;
+        }
+
+        return i;
+    }
+
+    private static int SkipBlockComment(string source, int i)
+    {
+        while (i + 1 < source.Length && !(source[i] == '*' && source[i + 1] == '/'))
+        {
+            i++;
+        }
+
+        return i + 1 < source.Length ? i + 2 : source.Length;
+    }
+
     private static void CopyQuotedLiteral(string source, ref int i, char quote, StringBuilder sb)
     {
         sb.Append(source[i++]);
diff --git a/src/Progmasoft.Tazel.CadreCeleste/CadreCelesteStore.cs b/src/Progmasoft.Tazel.CadreCeleste/CadreCelesteStore.cs
index 5d42437..3a4a11f 100644
--- a/src/Progmasoft.Tazel.CadreCeleste/CadreCelesteStore.cs
+++ b/src/Progmasoft.Tazel.CadreCeleste/CadreCelesteStore.cs
@@ -57,6 +57,7 @@ public static class CadreCelesteStore
         var (revision, green, red, pruned) = ReadMeta(conn);
         var edgesByNode = ReadEdges(conn);
         var nodes = ReadNodes(conn, edgesByNode);
+        SqliteConnection.ClearPool(conn);
         return new GraphSnapshot(revision, green, red, pruned, nodes);
     }