changeset 1153:99a8a7c9cce5

Optimize for the happy path when hitting caches.
author Martin Geisler <mg@daimi.au.dk>
date Fri, 27 Mar 2009 15:33:52 +0100
parents 295772b6b49a
children 950815ab873f
files viff/config.py viff/prss.py
diffstat 2 files changed, 12 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/viff/config.py	Fri Mar 27 15:30:06 2009 +0100
+++ b/viff/config.py	Fri Mar 27 15:33:52 2009 +0100
@@ -62,14 +62,13 @@
         Return a mapping from player subsets to :class:`viff.prss.PRF`
         instances.
         """
-        if (modulus in self.prfs_cache):
-            prfs = self.prfs_cache[modulus]
-        else:
-            prfs = {}
+        try:
+            return self.prfs_cache[modulus]
+        except KeyError:
+            self.prfs_cache[modulus] = prfs = {}
             for subset, key in self.keys.iteritems():
                 prfs[subset] = PRF(key, modulus)
-            self.prfs_cache[modulus] = prfs
-        return prfs
+            return prfs
 
     def dealer_prfs(self, modulus):
         """Retrieve dealer PRSS PRFs.
@@ -80,17 +79,16 @@
         Return a mapping from player subsets to :class:`viff.prss.PRF`
         instances.
         """
-        if (modulus in self.dealers_cache):
-            dealers = self.dealers_cache[modulus]
-        else:
-            dealers = {}
+        try:
+            return self.dealers_cache[modulus]
+        except KeyError:
+            self.dealers_cache[modulus] = dealers = {}
             for dealer, keys in self.dealer_keys.iteritems():
                 prfs = {}
                 for subset, key in keys.iteritems():
                     prfs[subset] = PRF(key, modulus)
                 dealers[dealer] = prfs
-            self.dealers_cache[modulus] = dealers
-        return dealers
+            return dealers
 
     def __repr__(self):
         """Simple string representation of the player."""
--- a/viff/prss.py	Fri Mar 27 15:30:06 2009 +0100
+++ b/viff/prss.py	Fri Mar 27 15:33:52 2009 +0100
@@ -79,9 +79,9 @@
     result = 0
     all = frozenset(range(1, n+1))
     for subset, share in rep_shares:
-        if ((field, n, j, subset) in _f_in_j_cache):
+        try:
             f_in_j = _f_in_j_cache[(field, n, j, subset)]
-        else:
+        except KeyError:
             points = [(field(x), 0) for x in all-subset]
             points.append((0, 1))
             f_in_j = shamir.recombine(points, j)