From c7a2e02c22e5565ab96750136f521c4e55f3b8c2 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 15 Jun 2021 14:13:09 +0000 Subject: [PATCH] SuperSize: Make it faster to exit console Use os._exit() to skip atexit() and __exit__() functions. Saves 2-5 seconds of nothingness after you hit Ctrl-D to exit. Bug: None Change-Id: I69cc80fc8e56e42b7670db80222556738948f6af Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2956312 Commit-Queue: Andrew Grieve Reviewed-by: Samuel Huang Cr-Commit-Position: refs/heads/master@{#892562} --- tools/binary_size/libsupersize/console.py | 40 ++++++++++++----------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/tools/binary_size/libsupersize/console.py b/tools/binary_size/libsupersize/console.py index 7e7ad5cf64214d..3bdbb9af2ca0a5 100644 --- a/tools/binary_size/libsupersize/console.py +++ b/tools/binary_size/libsupersize/console.py @@ -5,7 +5,6 @@ """An interactive console for looking analyzing .size files.""" import argparse -import atexit import code import contextlib import itertools @@ -32,7 +31,6 @@ # Number of lines before using less for Print(). _THRESHOLD_FOR_PAGER = 50 - @contextlib.contextmanager def _LessPipe(): """Output to `less`. Yields a file object to write to.""" @@ -70,8 +68,19 @@ def _WriteToStream(lines, use_pager=None, to_file=None): describe.WriteLines(lines, sys.stdout.write) +@contextlib.contextmanager +def _ReadlineSession(): + history_file = os.path.join(os.path.expanduser('~'), + '.binary_size_query_history') + # Without initializing readline, arrow keys don't even work! + readline.parse_and_bind('tab: complete') + if os.path.exists(history_file): + readline.read_history_file(history_file) + yield + readline.write_history_file(history_file) + + class _Session(object): - _readline_initialized = False def __init__(self, size_infos, output_directory_finder, tool_prefix_finder): self._printed_variables = [] @@ -485,26 +494,12 @@ def keys(cls, super_keys=None): lines.append('*' * 80) return '\n'.join(lines) - - @classmethod - def _InitReadline(cls): - if cls._readline_initialized: - return - cls._readline_initialized = True - # Without initializing readline, arrow keys don't even work! - readline.parse_and_bind('tab: complete') - history_file = os.path.join(os.path.expanduser('~'), - '.binary_size_query_history') - if os.path.exists(history_file): - readline.read_history_file(history_file) - atexit.register(lambda: readline.write_history_file(history_file)) - def Eval(self, query): exec (query, self._variables) def GoInteractive(self): - _Session._InitReadline() - code.InteractiveConsole(self._variables).interact(self._CreateBanner()) + with _ReadlineSession(): + code.InteractiveConsole(self._variables).interact(self._CreateBanner()) def AddArguments(parser): @@ -552,3 +547,10 @@ def Run(args, on_config_error): else: logging.info('Entering interactive console.') session.GoInteractive() + + # Exit without running GC, which can save multiple seconds due the large + # number of objects created. It meants atexit and __del__ calls are not + # made, but this shouldn't matter for console. + sys.stdout.flush() + sys.stderr.flush() + os._exit(0)