summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEeo Jun <141bytes@gmail.com>2016-09-26 19:05:59 +0800
committerEeo Jun <141bytes@gmail.com>2016-09-26 19:06:16 +0800
commit86de7a2960f47e400f7e440394716b8a40d2968d (patch)
treeb2663c037e64a2bd29ced9c490934a4fd67461a2
parent07e5b4653509d78350ce6dcb5529a6ae1e67d738 (diff)
fix memory explosion, much faster version.
-rw-r--r--frozendict/__init__.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/frozendict/__init__.py b/frozendict/__init__.py
index 9a63a65..8cd5fcb 100644
--- a/frozendict/__init__.py
+++ b/frozendict/__init__.py
@@ -1,6 +1,7 @@
import collections
-import operator
-import functools
+
+
+iteritems = getattr(dict, 'iteritems', dict.items)
class frozendict(collections.Mapping):
@@ -35,9 +36,10 @@ class frozendict(collections.Mapping):
def __hash__(self):
if self._hash is None:
- hashes = map(hash, self.items())
- self._hash = functools.reduce(operator.xor, hashes, 0)
-
+ h = 0
+ for key, value in iteritems(self._dict):
+ h ^= hash((key, value))
+ self._hash = h
return self._hash