comparison mercurial/profiling.py @ 29784:e3501546f7e4

profiling: add a context manager that no-ops if profiling isn't enabled And refactor dispatch.py to use it. As you can see, the resulting code is much simpler. I was tempted to inline _runcommand as part of writing this series. However, a number of extensions wrap _runcommand. So keeping it around is necessary (extensions can't easily wrap runcommand because it calls hooks before and after command execution).
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 14 Aug 2016 17:51:12 -0700
parents 5d44197c208b
children 88d3c1ab03a7
comparison
equal deleted inserted replaced
29783:5d44197c208b 29784:e3501546f7e4
141 # ui.log treats the input as a format string, 141 # ui.log treats the input as a format string,
142 # so we need to escape any % signs. 142 # so we need to escape any % signs.
143 val = val.replace('%', '%%') 143 val = val.replace('%', '%%')
144 ui.log('profile', val) 144 ui.log('profile', val)
145 fp.close() 145 fp.close()
146
147 @contextlib.contextmanager
148 def maybeprofile(ui):
149 """Profile if enabled, else do nothing.
150
151 This context manager can be used to optionally profile if profiling
152 is enabled. Otherwise, it does nothing.
153
154 The purpose of this context manager is to make calling code simpler:
155 just use a single code path for calling into code you may want to profile
156 and this function determines whether to start profiling.
157 """
158 if ui.configbool('profiling', 'enabled'):
159 with profile(ui):
160 yield
161 else:
162 yield