viff

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 diff
     1.1 --- a/viff/config.py	Fri Mar 27 15:30:06 2009 +0100
     1.2 +++ b/viff/config.py	Fri Mar 27 15:33:52 2009 +0100
     1.3 @@ -62,14 +62,13 @@
     1.4          Return a mapping from player subsets to :class:`viff.prss.PRF`
     1.5          instances.
     1.6          """
     1.7 -        if (modulus in self.prfs_cache):
     1.8 -            prfs = self.prfs_cache[modulus]
     1.9 -        else:
    1.10 -            prfs = {}
    1.11 +        try:
    1.12 +            return self.prfs_cache[modulus]
    1.13 +        except KeyError:
    1.14 +            self.prfs_cache[modulus] = prfs = {}
    1.15              for subset, key in self.keys.iteritems():
    1.16                  prfs[subset] = PRF(key, modulus)
    1.17 -            self.prfs_cache[modulus] = prfs
    1.18 -        return prfs
    1.19 +            return prfs
    1.20  
    1.21      def dealer_prfs(self, modulus):
    1.22          """Retrieve dealer PRSS PRFs.
    1.23 @@ -80,17 +79,16 @@
    1.24          Return a mapping from player subsets to :class:`viff.prss.PRF`
    1.25          instances.
    1.26          """
    1.27 -        if (modulus in self.dealers_cache):
    1.28 -            dealers = self.dealers_cache[modulus]
    1.29 -        else:
    1.30 -            dealers = {}
    1.31 +        try:
    1.32 +            return self.dealers_cache[modulus]
    1.33 +        except KeyError:
    1.34 +            self.dealers_cache[modulus] = dealers = {}
    1.35              for dealer, keys in self.dealer_keys.iteritems():
    1.36                  prfs = {}
    1.37                  for subset, key in keys.iteritems():
    1.38                      prfs[subset] = PRF(key, modulus)
    1.39                  dealers[dealer] = prfs
    1.40 -            self.dealers_cache[modulus] = dealers
    1.41 -        return dealers
    1.42 +            return dealers
    1.43  
    1.44      def __repr__(self):
    1.45          """Simple string representation of the player."""
     2.1 --- a/viff/prss.py	Fri Mar 27 15:30:06 2009 +0100
     2.2 +++ b/viff/prss.py	Fri Mar 27 15:33:52 2009 +0100
     2.3 @@ -79,9 +79,9 @@
     2.4      result = 0
     2.5      all = frozenset(range(1, n+1))
     2.6      for subset, share in rep_shares:
     2.7 -        if ((field, n, j, subset) in _f_in_j_cache):
     2.8 +        try:
     2.9              f_in_j = _f_in_j_cache[(field, n, j, subset)]
    2.10 -        else:
    2.11 +        except KeyError:
    2.12              points = [(field(x), 0) for x in all-subset]
    2.13              points.append((0, 1))
    2.14              f_in_j = shamir.recombine(points, j)