diff -r 5d44197c208b -r e3501546f7e4 mercurial/profiling.py --- a/mercurial/profiling.py Sun Aug 14 18:25:22 2016 -0700 +++ b/mercurial/profiling.py Sun Aug 14 17:51:12 2016 -0700 @@ -143,3 +143,20 @@ val = val.replace('%', '%%') ui.log('profile', val) fp.close() + +@contextlib.contextmanager +def maybeprofile(ui): + """Profile if enabled, else do nothing. + + This context manager can be used to optionally profile if profiling + is enabled. Otherwise, it does nothing. + + The purpose of this context manager is to make calling code simpler: + just use a single code path for calling into code you may want to profile + and this function determines whether to start profiling. + """ + if ui.configbool('profiling', 'enabled'): + with profile(ui): + yield + else: + yield