pycompat: export queue module instead of symbols in module (API)
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 05 May 2018 18:35:16 -0700
changeset 37844 8fb9985382be
parent 37843 670eb4fa1b86
child 37845 b4b7427b5786
pycompat: export queue module instead of symbols in module (API) Previously, pycompat and util re-exported individual symbols from the queue module. This had the side-effect of forcing the loading of the queue module whenever pycompat/util was imported. These symbols aren't used very often. So importing the module to get a handle on the symbols is wasteful. This commit changes pycompat so it no longer exports the individual symbols in the queue module. Instead, we make the imported module a "public" symbol. We drop the individual symbol aliases from the util module. All consumers are updated to use pycompat.queue.* instead. This change makes 300 invocations of `hg log -r. -T '{rev}\n'` a little faster: before: 18.44s after: 17.87s Differential Revision: https://phab.mercurial-scm.org/D3441
contrib/check-code.py
contrib/perf.py
mercurial/pycompat.py
mercurial/util.py
mercurial/vfs.py
mercurial/worker.py
--- a/contrib/check-code.py	Sat May 05 18:41:51 2018 -0700
+++ b/contrib/check-code.py	Sat May 05 18:35:16 2018 -0700
@@ -340,7 +340,8 @@
     (r'\butil\.Abort\b', "directly use error.Abort"),
     (r'^@(\w*\.)?cachefunc', "module-level @cachefunc is risky, please avoid"),
     (r'^import atexit', "don't use atexit, use ui.atexit"),
-    (r'^import Queue', "don't use Queue, use util.queue + util.empty"),
+    (r'^import Queue', "don't use Queue, use pycompat.queue.Queue + "
+                       "pycompat.queue.Empty"),
     (r'^import cStringIO', "don't use cStringIO.StringIO, use util.stringio"),
     (r'^import urllib', "don't use urllib, use util.urlreq/util.urlerr"),
     (r'^import SocketServer', "don't use SockerServer, use util.socketserver"),
--- a/contrib/perf.py	Sat May 05 18:41:51 2018 -0700
+++ b/contrib/perf.py	Sat May 05 18:35:16 2018 -0700
@@ -71,6 +71,16 @@
     import inspect
     getargspec = inspect.getargspec
 
+try:
+    # 4.7+
+    queue = pycompat.queue.Queue
+except (AttributeError, ImportError):
+    # <4.7.
+    try:
+        queue = pycompat.queue
+    except (AttributeError, ImportError):
+        queue = util.queue
+
 # for "historical portability":
 # define util.safehasattr forcibly, because util.safehasattr has been
 # available since 1.9.3 (or 94b200a11cf7)
@@ -1029,7 +1039,7 @@
                 else:
                     mdiff.textdiff(*pair)
     else:
-        q = util.queue()
+        q = queue()
         for i in xrange(threads):
             q.put(None)
         ready = threading.Condition()
--- a/mercurial/pycompat.py	Sat May 05 18:41:51 2018 -0700
+++ b/mercurial/pycompat.py	Sat May 05 18:35:16 2018 -0700
@@ -23,7 +23,7 @@
     import cookielib
     import cPickle as pickle
     import httplib
-    import Queue as _queue
+    import Queue as queue
     import SocketServer as socketserver
     import xmlrpclib
 
@@ -36,16 +36,13 @@
     import http.cookiejar as cookielib
     import http.client as httplib
     import pickle
-    import queue as _queue
+    import queue as queue
     import socketserver
     import xmlrpc.client as xmlrpclib
 
     def future_set_exception_info(f, exc_info):
         f.set_exception(exc_info[0])
 
-empty = _queue.Empty
-queue = _queue.Queue
-
 def identity(a):
     return a
 
--- a/mercurial/util.py	Sat May 05 18:41:51 2018 -0700
+++ b/mercurial/util.py	Sat May 05 18:35:16 2018 -0700
@@ -60,10 +60,8 @@
 b85encode = base85.b85encode
 
 cookielib = pycompat.cookielib
-empty = pycompat.empty
 httplib = pycompat.httplib
 pickle = pycompat.pickle
-queue = pycompat.queue
 safehasattr = pycompat.safehasattr
 socketserver = pycompat.socketserver
 bytesio = pycompat.bytesio
--- a/mercurial/vfs.py	Sat May 05 18:41:51 2018 -0700
+++ b/mercurial/vfs.py	Sat May 05 18:35:16 2018 -0700
@@ -568,7 +568,7 @@
         ui.debug('starting %d threads for background file closing\n' %
                  threadcount)
 
-        self._queue = util.queue(maxsize=maxqueue)
+        self._queue = pycompat.queue.Queue(maxsize=maxqueue)
         self._running = True
 
         for i in range(threadcount):
@@ -600,7 +600,7 @@
                 except Exception as e:
                     # Stash so can re-raise from main thread later.
                     self._threadexception = e
-            except util.empty:
+            except pycompat.queue.Empty:
                 if not self._running:
                     break
 
--- a/mercurial/worker.py	Sat May 05 18:41:51 2018 -0700
+++ b/mercurial/worker.py	Sat May 05 18:35:16 2018 -0700
@@ -235,7 +235,7 @@
                             # iteration.
                             if self._interrupted:
                                 return
-                    except util.empty:
+                    except pycompat.queue.Empty:
                         break
             except Exception as e:
                 # store the exception such that the main thread can resurface
@@ -262,8 +262,8 @@
                 return
 
     workers = _numworkers(ui)
-    resultqueue = util.queue()
-    taskqueue = util.queue()
+    resultqueue = pycompat.queue.Queue()
+    taskqueue = pycompat.queue.Queue()
     # partition work to more pieces than workers to minimize the chance
     # of uneven distribution of large tasks between the workers
     for pargs in partition(args, workers * 20):