[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Caching ExitPolicy's can_exit_to() results
commit 32b9de7c8e2690d4642ac72019c7ed684e3b3576
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sun Jan 13 21:12:08 2013 -0800
Caching ExitPolicy's can_exit_to() results
That's odd. I wonder why I didn't add a cache here - can_exit_to() is a method
that's highly likely to be called repeatedly so if we can get contant time
lookups then great.
---
stem/exit_policy.py | 16 ++++++++++++----
1 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index e568deb..36c5c98 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -150,6 +150,7 @@ class ExitPolicy(object):
self._input_rules = rules # input rules, only kept until self._rules is set
self._is_allowed_default = True
self._summary_representation = None
+ self._can_exit_to_cache = {}
def can_exit_to(self, address = None, port = None):
"""
@@ -163,11 +164,17 @@ class ExitPolicy(object):
:returns: **True** if exiting to this destination is allowed, **False** otherwise
"""
- for rule in self._get_rules():
- if rule.is_match(address, port):
- return rule.is_accept
+ if not (address, port) in self._can_exit_to_cache:
+ result = self._is_allowed_default
- return self._is_allowed_default
+ for rule in self._get_rules():
+ if rule.is_match(address, port):
+ result = rule.is_accept
+ break
+
+ self._can_exit_to_cache[(address, port)] = result
+
+ return self._can_exit_to_cache[(address, port)]
def is_exiting_allowed(self):
"""
@@ -285,6 +292,7 @@ class ExitPolicy(object):
"""
self._is_allowed_default = is_allowed_default
+ self._can_exit_to_cache = {}
def _get_rules(self):
if self._rules is None:
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits