Subject: [PATCH] mingw: improve performance of mingw_unlink()

From: "Johannes Schindelin via GitGitGadget" <[email-masked]>
Date: Mon, 17 Aug 2020 10:37:02 +0000

From: Jeff Hostetler <[email-masked]>

Update mingw_unlink() to first try to delete the file with existing
permissions before trying to force it.

Windows throws an error when trying to delete a read-only file.  The
mingw_unlink() compatibility wrapper always tries to _wchmod(666) the
file before calling _wunlink() to avoid that error.  However, since
most files in the worktree are already writable, this is usually
wasted effort.

Update mingw_unlink() to just call DeleteFileW() directly and if that
succeeds return.  If that fails, fall back into the existing code path
to update the permissions and use _wunlink() to get the existing
error code mapping.

Signed-off-by: Jeff Hostetler <[email-masked]>
Signed-off-by: Johannes Schindelin <[email-masked]>
---
    Windows: improve checkout performance with large numbers of files
    
    Jeff Hostetler contributed this patch to Git for Windows a while ago
    after measuring ~15% improvement
    [https://github.com/microsoft/git/pull/264#issuecomment-617216583] with
    a very, very large worktree:
    
    > I ran a series of timing tests on the Office repo on my Z440 desktop.
    > This was a full enlistment at HEAD. I ran git sparse-checkout disable 
    > to populate the entire worktree and then git sparse-checkout init
    > --cone to delete everything except the files at root. I repeated that
    > sequence 3 times.
    >
    > Each init needed to delete about 2.1M files.
    >
    > git sparse-checkout init
    >
    > old: 636.1s, 638.3s, 637.2s, 637.2s new: 535.9s, 544.8s, 533.3s,
    > 538.0s
    >
    > In this example, run time was decreased by 15%.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-704%2Fdscho%2Funlink-perf-gfw-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-704/dscho/unlink-perf-gfw-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/704

 compat/mingw.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/compat/mingw.c b/compat/mingw.c
index 4454b3e67b..a00f331230 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -290,6 +290,9 @@ int mingw_unlink(const char *pathname)
 	if (xutftowcs_path(wpathname, pathname) < 0)
 		return -1;
 
+	if (DeleteFileW(wpathname))
+		return 0;
+
 	/* read-only files cannot be removed */
 	_wchmod(wpathname, 0666);
 	while ((ret = _wunlink(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {

base-commit: 878e727637ec5815ccb3301eb994a54df95b21b8
-- 
gitgitgadget