commit a91cf041a0c5b3ad9a81a26162794d382a2a10f9 Author: aagbsn aagbsn@extc.org Date: Thu May 24 16:34:26 2012 -0700
4297 - Cache dynamic filter functions --- lib/bridgedb/Filters.py | 38 ++++++++++++++++++++++++++------------ 1 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/lib/bridgedb/Filters.py b/lib/bridgedb/Filters.py index 72e9004..fd79704 100644 --- a/lib/bridgedb/Filters.py +++ b/lib/bridgedb/Filters.py @@ -4,21 +4,35 @@
from ipaddr import IPv6Address, IPv4Address
+funcs = {} + def filterAssignBridgesToRing(hmac, numRings, assignedRing): - def f(bridge): - digest = hmac(bridge.getID()) - pos = long( digest[:8], 16 ) - which = pos % numRings - if which == assignedRing: return True - return False - return f + ruleset = frozenset([hmac, numRings, assignedRing]) + try: + return funcs[ruleset] + except KeyError: + def f(bridge): + digest = hmac(bridge.getID()) + pos = long( digest[:8], 16 ) + which = pos % numRings + if which == assignedRing: return True + return False + f.__name__ = "filterAssignBridgesToRing(%s, %s, %s)" % (hmac, numRings, + assignedRing) + funcs[ruleset] = f + return f
def filterBridgesByRules(rules): - def g(x): - r = [f(x) for f in rules] - if False in r: return False - return True - return g + ruleset = frozenset(rules) + try: + return funcs[ruleset] + except KeyError: + def g(x): + r = [f(x) for f in rules] + if False in r: return False + return True + funcs[ruleset] = g + return g
def filterBridgesByIP4(bridge): try:
tor-commits@lists.torproject.org