[tor-commits] [nyx/master] Helper for converting addresses to ints

atagar at torproject.org atagar at torproject.org
Tue Sep 22 17:08:40 UTC 2015


commit 000a39d09149955ce528dc60a9686546da0ca340
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Aug 1 17:10:52 2015 -0700

    Helper for converting addresses to ints
    
    We need an integer representation of ipv4 addresses for sorting. Our entity did
    this, but it's better to simply have a helper (simpler, and more cache hits).
    Might be moved again later.
---
 nyx/connections/conn_entry.py |   11 -----------
 nyx/connections/entries.py    |   18 ++++++++++++++++--
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/nyx/connections/conn_entry.py b/nyx/connections/conn_entry.py
index 381afec..021accb 100644
--- a/nyx/connections/conn_entry.py
+++ b/nyx/connections/conn_entry.py
@@ -70,17 +70,6 @@ class ConnectionLine(entries.ConnectionPanelLine):
     self.include_port = include_port
     self.include_expanded_addresses = include_expanded_addresses
 
-    # cached immutable values used for sorting
-
-    ip_value = 0
-
-    for comp in self.connection.remote_address.split('.'):
-      ip_value *= 255
-      ip_value += int(comp)
-
-    self.sort_address = ip_value
-    self.sort_port = self.connection.remote_port
-
   def get_locale(self, default = None):
     """
     Provides the two letter country code for the remote endpoint.
diff --git a/nyx/connections/entries.py b/nyx/connections/entries.py
index 7ea3321..6963123 100644
--- a/nyx/connections/entries.py
+++ b/nyx/connections/entries.py
@@ -35,12 +35,26 @@ PORT_COUNT = 65536
 # sort value for scrubbed ip addresses
 
 SCRUBBED_IP_VAL = 255 ** 4
+ADDRESS_CACHE = {}
 
 
 def to_unix_time(dt):
   return (dt - datetime.datetime(1970, 1, 1)).total_seconds()
 
 
+def address_to_int(address):
+  if address not in ADDRESS_CACHE:
+    ip_value = 0
+
+    for comp in address.split('.'):
+      ip_value *= 255
+      ip_value += int(comp)
+
+    ADDRESS_CACHE[address] = ip_value
+
+  return ADDRESS_CACHE[address]
+
+
 class ConnectionPanelEntry:
   def __init__(self, connection_type, start_time):
     self.lines = []
@@ -127,9 +141,9 @@ class ConnectionPanelEntry:
       if connection_line.is_private():
         return SCRUBBED_IP_VAL  # orders at the end
 
-      return connection_line.sort_address
+      return address_to_int(connection_line.connection.remote_address)
     elif attr == SortAttr.PORT:
-      return connection_line.sort_port
+      return connection_line.connection.remote_port
     elif attr == SortAttr.FINGERPRINT:
       return connection_line.get_fingerprint('UNKNOWN')
     elif attr == SortAttr.NICKNAME:





More information about the tor-commits mailing list