Skip to content

Commit

Permalink
SuperSize: Make it faster to exit console
Browse files Browse the repository at this point in the history
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 <agrieve@chromium.org>
Reviewed-by: Samuel Huang <huangs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#892562}
  • Loading branch information
agrieve authored and Chromium LUCI CQ committed Jun 15, 2021
1 parent 2a547d6 commit c7a2e02
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions tools/binary_size/libsupersize/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""An interactive console for looking analyzing .size files."""

import argparse
import atexit
import code
import contextlib
import itertools
Expand All @@ -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."""
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit c7a2e02

Please sign in to comment.