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

[tor-commits] [stem/master] Caching parsed tor versions



commit 2ce003ef33c8693234f7dce562fdd3cff4e37e07
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Oct 8 00:15:51 2013 -0700

    Caching parsed tor versions
    
    Reusing parsed tor versions saves us an impressive 3.7% memory usage without
    any noticable impact on parsing times.
---
 stem/descriptor/networkstatus.py       |    2 +-
 stem/descriptor/router_status_entry.py |    2 +-
 stem/descriptor/server_descriptor.py   |    2 +-
 stem/version.py                        |   11 +++++++++++
 4 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 8d615fa..a2b07fb 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -712,7 +712,7 @@ class _DocumentHeader(object):
       elif keyword in ("client-versions", "server-versions"):
         for entry in value.split(","):
           try:
-            version_value = stem.version.Version(entry)
+            version_value = stem.version._get_version(entry)
 
             if keyword == 'client-versions':
               self.client_versions.append(version_value)
diff --git a/stem/descriptor/router_status_entry.py b/stem/descriptor/router_status_entry.py
index 733a066..4018b22 100644
--- a/stem/descriptor/router_status_entry.py
+++ b/stem/descriptor/router_status_entry.py
@@ -584,7 +584,7 @@ def _parse_v_line(desc, value, validate):
 
   if value.startswith("Tor "):
     try:
-      desc.version = stem.version.Version(value[4:])
+      desc.version = stem.version._get_version(value[4:])
     except ValueError as exc:
       if validate:
         raise ValueError("%s has a malformed tor version (%s): v %s" % (desc._name(), exc, value))
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 521d07a..8d11801 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -442,7 +442,7 @@ class ServerDescriptor(Descriptor):
           version_str, self.operating_system = platform_match.groups()
 
           try:
-            self.tor_version = stem.version.Version(version_str)
+            self.tor_version = stem.version._get_version(version_str)
           except ValueError:
             pass
       elif keyword == "published":
diff --git a/stem/version.py b/stem/version.py
index b529f86..68faa36 100644
--- a/stem/version.py
+++ b/stem/version.py
@@ -62,6 +62,12 @@ import re
 import stem.util.enum
 import stem.util.system
 
+try:
+  # added in python 3.2
+  from collections import lru_cache
+except ImportError:
+  from stem.util.lru_cache import lru_cache
+
 # cache for the get_system_tor_version function
 VERSION_CACHE = {}
 
@@ -115,6 +121,11 @@ def get_system_tor_version(tor_cmd = "tor"):
   return VERSION_CACHE[tor_cmd]
 
 
+@lru_cache()
+def _get_version(version_str):
+  return Version(version_str)
+
+
 class Version(object):
   """
   Comparable tor version. These are constructed from strings that conform to



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