git » git-arr » commit 5048b18

Only call mkdir if we are writing the file

author Alberto Bertogli
2025-05-18 20:08:38 UTC
committer Alberto Bertogli
2025-05-18 20:48:43 UTC
parent 52862dd6cddabb0d0f8137a0518406e9c8dfd855

Only call mkdir if we are writing the file

Today, when generating files, we unconditionally check if the directory
exists, and create it if not.

However, most of the time when re-generating we skip writing the file
altogether, which results in a lot of unnecessary checks.

This patch moves the directory creation right next to the file writing,
which results in a small speedup while regenerating.

git-arr +4 -4

diff --git a/git-arr b/git-arr
index 5322f7d..5fbf1d0 100755
--- a/git-arr
+++ b/git-arr
@@ -339,10 +339,6 @@ def generate(output: str, only=None):
     @utils.log_timing("path")
     def write_to(path: str, func_or_str, args=(), mtime=None):
         path = output + "/" + path
-        dirname = os.path.dirname(path)
-
-        if not os.path.exists(dirname):
-            os.makedirs(dirname)
 
         if mtime:
             path_mtime: Union[float, int] = 0
@@ -374,6 +370,10 @@ def generate(output: str, only=None):
                 print(path)
                 s = func_or_str(*args)
 
+        dirname = os.path.dirname(path)
+        if not os.path.exists(dirname):
+            os.makedirs(dirname)
+
         open(path, "w").write(s)
         if mtime:
             os.utime(path, (mtime, mtime))