sparse: use vfs.tryread()
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 06 Jul 2017 10:58:45 -0700
changeset 33296 ee616196227c
parent 33295 c72e9c61d2b1
child 33297 ba5d89774db6
sparse: use vfs.tryread() vfs.exists() followed by a file read is an anti-pattern because it incurs an extra stat() to test for file presence. vfs.tryread() returns empty string on missing file and avoids the stat().
hgext/sparse.py
--- a/hgext/sparse.py	Sat Jul 01 11:56:39 2017 -0700
+++ b/hgext/sparse.py	Thu Jul 06 10:58:45 2017 -0700
@@ -445,13 +445,13 @@
             """Returns the include/exclude patterns specified by the
             given rev.
             """
-            if not self.vfs.exists('sparse'):
+            raw = self.vfs.tryread('sparse')
+            if not raw:
                 return set(), set(), []
             if rev is None:
                 raise error.Abort(_("cannot parse sparse patterns from " +
                     "working copy"))
 
-            raw = self.vfs.read('sparse')
             includes, excludes, profiles = self.readsparseconfig(raw)
 
             ctx = self[rev]
@@ -623,8 +623,8 @@
 
         def gettemporaryincludes(self):
             existingtemp = set()
-            if self.vfs.exists('tempsparse'):
-                raw = self.vfs.read('tempsparse')
+            raw = self.vfs.tryread('tempsparse')
+            if raw:
                 existingtemp.update(raw.split('\n'))
             return existingtemp
 
@@ -784,8 +784,8 @@
     try:
         oldsparsematch = repo.sparsematch()
 
-        if repo.vfs.exists('sparse'):
-            raw = repo.vfs.read('sparse')
+        raw = repo.vfs.tryread('sparse')
+        if raw:
             oldinclude, oldexclude, oldprofiles = map(
                 set, repo.readsparseconfig(raw))
         else:
@@ -845,9 +845,7 @@
                 repo.dirstate.parents() if node != nullid]
 
         # read current configuration
-        raw = ''
-        if repo.vfs.exists('sparse'):
-            raw = repo.vfs.read('sparse')
+        raw = repo.vfs.tryread('sparse')
         oincludes, oexcludes, oprofiles = repo.readsparseconfig(raw)
         includes, excludes, profiles = map(
                 set, (oincludes, oexcludes, oprofiles))
@@ -898,9 +896,7 @@
 
 def _clear(ui, repo, files, force=False):
     with repo.wlock():
-        raw = ''
-        if repo.vfs.exists('sparse'):
-            raw = repo.vfs.read('sparse')
+        raw = repo.vfs.tryread('sparse')
         includes, excludes, profiles = repo.readsparseconfig(raw)
 
         if includes or excludes: