diff mercurial/pathutil.py @ 44865:233ee525dcef

grep: reduce the cost of pathauditor checks when grepping working copy Running `time hg grep zxczxczxczxczxc -l` on mozilla-central: before: real 0m20,000s user 0m15,796s sys 0m4,189s after: real 0m10,903s user 0m8,964s sys 0m1,916s if vfs didn't call pathauditor at all: real 0m7,781s user 0m5,968s sys 0m1,790s Differential Revision: https://phab.mercurial-scm.org/D8582
author Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
date Mon, 25 May 2020 17:39:23 -0400
parents 11f284c8c5e4
children 89a2afe31e82
line wrap: on
line diff
--- a/mercurial/pathutil.py	Mon May 25 17:32:25 2020 -0400
+++ b/mercurial/pathutil.py	Mon May 25 17:39:23 2020 -0400
@@ -1,5 +1,6 @@
 from __future__ import absolute_import
 
+import contextlib
 import errno
 import os
 import posixpath
@@ -148,6 +149,19 @@
         except (OSError, error.Abort):
             return False
 
+    @contextlib.contextmanager
+    def cached(self):
+        if self._cached:
+            yield
+        else:
+            try:
+                self._cached = True
+                yield
+            finally:
+                self.audited.clear()
+                self.auditeddir.clear()
+                self._cached = False
+
 
 def canonpath(root, cwd, myname, auditor=None):
     '''return the canonical path of myname, given cwd and root