[tor-commits] [stem/master] Skip autocompletion for non-interactive interpreter

atagar at torproject.org atagar at torproject.org
Sat Mar 2 00:28:19 UTC 2019


commit 0fc61ddb1df72762d1e833c3977c4dd8e9c06617
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Mar 1 11:13:10 2019 -0800

    Skip autocompletion for non-interactive interpreter
    
    Sadly I forget where it was pointed out, but invoking the control port via
    shell is a *lot* faster than stem...
    
      #!/bin/bash -e
    
      cmd="$@"
      pass="ControlPortPassword"
    
      function test_tor() {
          echo "$1" >&3
          sed "/^250 OK\r$/q" <&3
          echo QUIT >&3
          exec 3<&-
      }
    
      exec 3<>/dev/tcp/127.0.0.1/9051
      echo AUTHENTICATE \"$pass\" >&3
      read -u 3
      test_tor "$cmd"
    
      ====================
    
      atagar at morrigan:~$ time ./bench.sh 'GETINFO version' 1>/dev/null
    
      real  0m0.007s
      user  0m0.004s
      sys   0m0.003s
    
      ====================
    
      atagar at morrigan:~$ time tor-prompt --run 'GETINFO version' 1>/dev/null
    
      real  0m0.186s
      user  0m0.072s
      sys   0m0.030s
    
    Generally speaking this is expected. Spinning up an interpreter takes time. But
    in doing a quick investigation realized this is quite a bit slower than it
    needs to be...
    
      total tor-prompt runtime          0.186 seconds
      --------------------------------------------------------
      python interpreter startup        0.016 seconds (9%)
      import statements                 0.079 seconds (42%)
      check if tor is running           0.014 seconds (8%)
      connect to tor                    0.009 seconds (5%)
      autocompete setup                 0.065 seconds (34%)
      invoke tor controller command     0.003 seconds (2%)
    
    Autocompletion is only relevant when the user is presented with an interactive
    interpreter. If we're merely invoking a command it's pointless.
    
    So TL;DR: tor-prompt is now ~34% faster when used to invoke controller commands.
---
 docs/change_log.rst          |  4 ++++
 stem/interpreter/__init__.py | 10 +++++-----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index edf574c3..21b6c85e 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -77,6 +77,10 @@ The following are only available within Stem's `git repository
 
   * Added NetBSD to our `download page <download.html>`_
 
+ * **Interpreter**
+
+  * tor-prompt is now ~34% faster when used to non-interactively invoke commands
+
 .. _version_1.7:
 
 Version 1.7 (October 7th, 2018)
diff --git a/stem/interpreter/__init__.py b/stem/interpreter/__init__.py
index b10109e8..a4911a65 100644
--- a/stem/interpreter/__init__.py
+++ b/stem/interpreter/__init__.py
@@ -118,11 +118,6 @@ def main():
     sys.exit(1)
 
   with controller:
-    autocompleter = stem.interpreter.autocomplete.Autocompleter(controller)
-    readline.parse_and_bind('tab: complete')
-    readline.set_completer(autocompleter.complete)
-    readline.set_completer_delims('\n')
-
     interpreter = stem.interpreter.commands.ControlInterpreter(controller)
     showed_close_confirmation = False
 
@@ -158,6 +153,11 @@ def main():
         sys.exit(1)
 
     else:
+      autocompleter = stem.interpreter.autocomplete.Autocompleter(controller)
+      readline.parse_and_bind('tab: complete')
+      readline.set_completer(autocompleter.complete)
+      readline.set_completer_delims('\n')
+
       for line in msg('msg.startup_banner').splitlines():
         line_format = HEADER_BOLD_OUTPUT if line.startswith('  ') else HEADER_OUTPUT
         print(format(line, *line_format))





More information about the tor-commits mailing list