changeset 41408:a43acfa2b76d

keepalive: use collections.defaultdict for host map Cleaning up the code as part of debugging Python 3 issues. Differential Revision: https://phab.mercurial-scm.org/D5718
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 26 Jan 2019 13:39:18 -0800
parents 6e9bebb65ce7
children 1db94ebbc207
files mercurial/keepalive.py
diffstat 1 files changed, 8 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/keepalive.py	Sat Jan 26 10:57:17 2019 -0800
+++ b/mercurial/keepalive.py	Sat Jan 26 13:39:18 2019 -0800
@@ -84,6 +84,7 @@
 
 from __future__ import absolute_import, print_function
 
+import collections
 import errno
 import hashlib
 import socket
@@ -114,15 +115,13 @@
       """
     def __init__(self):
         self._lock = threading.Lock()
-        self._hostmap = {} # map hosts to a list of connections
+        self._hostmap = collections.defaultdict(list) # host -> [connection]
         self._connmap = {} # map connections to host
         self._readymap = {} # map connection to ready state
 
     def add(self, host, connection, ready):
         self._lock.acquire()
         try:
-            if host not in self._hostmap:
-                self._hostmap[host] = []
             self._hostmap[host].append(connection)
             self._connmap[connection] = host
             self._readymap[connection] = ready
@@ -155,19 +154,18 @@
         conn = None
         self._lock.acquire()
         try:
-            if host in self._hostmap:
-                for c in self._hostmap[host]:
-                    if self._readymap[c]:
-                        self._readymap[c] = 0
-                        conn = c
-                        break
+            for c in self._hostmap[host]:
+                if self._readymap[c]:
+                    self._readymap[c] = 0
+                    conn = c
+                    break
         finally:
             self._lock.release()
         return conn
 
     def get_all(self, host=None):
         if host:
-            return list(self._hostmap.get(host, []))
+            return list(self._hostmap[host])
         else:
             return dict(self._hostmap)