comparison mercurial/ui.py @ 30965:8b83b626fb1e

ui: remove urllib2 from being imported early Before this change, urllib2 was brought in when constructing the ui object, and that added ~5ms on my Linux workstation to the hg startup time for every command, bringing the time for 'HGRCPATH=/dev/null hg root' from 46ms to 40ms. Now, we construct a single proxy object per initial ui creation (so that if the ui is copied they share the object), but defer the actual instantiation of it and the import of urllib2 until it's needed. # no-check-commit
author Kyle Lippincott <spectral@google.com>
date Sun, 12 Feb 2017 03:06:38 -0800
parents f61c5680a862
children e92daf156d5c
comparison
equal deleted inserted replaced
30964:b7e073ae44c4 30965:8b83b626fb1e
91 # 91 #
92 # blackbox = 92 # blackbox =
93 # color = 93 # color =
94 # pager =""", 94 # pager =""",
95 } 95 }
96
97
98 class httppasswordmgrdbproxy(object):
99 """Delays loading urllib2 until it's needed."""
100 def __init__(self):
101 self._mgr = None
102
103 def _get_mgr(self):
104 if self._mgr is None:
105 self._mgr = urlreq.httppasswordmgrwithdefaultrealm()
106 return self._mgr
107
108 def add_password(self, *args, **kwargs):
109 return self._get_mgr().add_password(*args, **kwargs)
110
111 def find_user_password(self, *args, **kwargs):
112 return self._get_mgr().find_user_password(*args, **kwargs)
113
96 114
97 class ui(object): 115 class ui(object):
98 def __init__(self, src=None): 116 def __init__(self, src=None):
99 """Create a fresh new ui object if no src given 117 """Create a fresh new ui object if no src given
100 118
143 self.fin = util.stdin 161 self.fin = util.stdin
144 162
145 # shared read-only environment 163 # shared read-only environment
146 self.environ = encoding.environ 164 self.environ = encoding.environ
147 165
148 self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm() 166 self.httppasswordmgrdb = httppasswordmgrdbproxy()
149 167
150 allowed = self.configlist('experimental', 'exportableenviron') 168 allowed = self.configlist('experimental', 'exportableenviron')
151 if '*' in allowed: 169 if '*' in allowed:
152 self._exportableenviron = self.environ 170 self._exportableenviron = self.environ
153 else: 171 else:
170 188
171 def resetstate(self): 189 def resetstate(self):
172 """Clear internal state that shouldn't persist across commands""" 190 """Clear internal state that shouldn't persist across commands"""
173 if self._progbar: 191 if self._progbar:
174 self._progbar.resetstate() # reset last-print time of progress bar 192 self._progbar.resetstate() # reset last-print time of progress bar
175 self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm() 193 self.httppasswordmgrdb = httppasswordmgrdbproxy()
176 194
177 def formatter(self, topic, opts): 195 def formatter(self, topic, opts):
178 return formatter.formatter(self, topic, opts) 196 return formatter.formatter(self, topic, opts)
179 197
180 def _trusted(self, fp, f): 198 def _trusted(self, fp, f):