commit 1920baff1ad557a2a556dcb0cb46609f50f3cde1 Author: Damian Johnson atagar@torproject.org Date: Tue Oct 8 00:37:11 2013 -0700
Compressing unparsed exit policy content
Exit policies constitute a rather substantial chunch of server descriptors. We already optimize our runtime by lazily parsing policies on demand. On reflection though, we can improve our memory usage quite a bit too by compressing the unparsed content. This drops our memory usage by roughly 6% without impacting runtime. --- stem/exit_policy.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/stem/exit_policy.py b/stem/exit_policy.py index 930c8bb..cc91124 100644 --- a/stem/exit_policy.py +++ b/stem/exit_policy.py @@ -56,6 +56,8 @@ exiting to a destination is permissible or not. For instance... ============ =========== """
+import zlib + import stem.prereq import stem.util.connection import stem.util.enum @@ -152,7 +154,16 @@ class ExitPolicy(object): # This is lazily evaluated so we don't need to actually parse the exit # policy if it's never used.
- self._input_rules = rules + is_all_str = True + + for rule in rules: + if not isinstance(rule, (bytes, unicode)): + is_all_str = False + + if rules and is_all_str: + self._input_rules = zlib.compress(','.join(rules)) + else: + self._input_rules = rules
# Result when no rules apply. According to the spec policies default to 'is # allowed', but our microdescriptor policy subclass might want to change @@ -287,7 +298,12 @@ class ExitPolicy(object): rules = [] is_all_accept, is_all_reject = True, True
- for rule in self._input_rules: + if isinstance(self._input_rules, str): + decompressed_rules = zlib.decompress(self._input_rules).split(',') + else: + decompressed_rules = self._input_rules + + for rule in decompressed_rules: if isinstance(rule, (bytes, unicode)): rule = ExitPolicyRule(rule.strip())
tor-commits@lists.torproject.org