[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Cache decoded addresses



commit 5d1a643e2b0affff40de4c0a584f08c7227d2df4
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Sep 24 11:48:27 2017 -0700

    Cache decoded addresses
    
    Decoding addresses is somewhat costly, and repeated quite a bit.
---
 stem/util/proc.py | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/stem/util/proc.py b/stem/util/proc.py
index 884ee9ec..6f00311b 100644
--- a/stem/util/proc.py
+++ b/stem/util/proc.py
@@ -80,6 +80,7 @@ except AttributeError:
   CLOCK_TICKS = None
 
 IS_LITTLE_ENDIAN = sys.byteorder == 'little'
+ENCODED_ADDR = {}  # cache of encoded ips to their decoded version
 
 Stat = stem.util.enum.Enum(
   ('COMMAND', 'command'), ('CPU_UTIME', 'utime'),
@@ -467,29 +468,31 @@ def _decode_proc_address_encoding(addr, is_ipv6):
   """
 
   ip, port = addr.rsplit(b':', 1)
-
   port = int(port, 16)  # the port is represented as a two-byte hexadecimal number
 
-  if not is_ipv6:
-    ip_encoded = base64.b16decode(ip)[::-1] if IS_LITTLE_ENDIAN else base64.b16decode(ip)
-    ip = socket.inet_ntop(socket.AF_INET, ip_encoded)
-  else:
-    if IS_LITTLE_ENDIAN:
-      # Group into eight characters, then invert in pairs...
-      #
-      #   https://trac.torproject.org/projects/tor/ticket/18079#comment:24
+  if ip not in ENCODED_ADDR:
+    if not is_ipv6:
+      ip_encoded = base64.b16decode(ip)[::-1] if IS_LITTLE_ENDIAN else base64.b16decode(ip)
+      ENCODED_ADDR[ip] = socket.inet_ntop(socket.AF_INET, ip_encoded)
+    else:
+      ip_encoded = ip
+
+      if IS_LITTLE_ENDIAN:
+        # Group into eight characters, then invert in pairs...
+        #
+        #   https://trac.torproject.org/projects/tor/ticket/18079#comment:24
 
-      inverted = []
+        inverted = []
 
-      for i in range(4):
-        grouping = ip[8 * i:8 * (i + 1)]
-        inverted += [grouping[2 * i:2 * (i + 1)] for i in range(4)][::-1]
+        for i in range(4):
+          grouping = ip[8 * i:8 * (i + 1)]
+          inverted += [grouping[2 * i:2 * (i + 1)] for i in range(4)][::-1]
 
-      ip = b''.join(inverted)
+        ip_encoded = b''.join(inverted)
 
-    ip = stem.util.connection.expand_ipv6_address(socket.inet_ntop(socket.AF_INET6, base64.b16decode(ip)))
+      ENCODED_ADDR[ip] = stem.util.connection.expand_ipv6_address(socket.inet_ntop(socket.AF_INET6, base64.b16decode(ip_encoded)))
 
-  return (ip, port)
+  return (ENCODED_ADDR[ip], port)
 
 
 def _is_float(*value):

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits