Commit 7c821ad1 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

improve DomainTuple and DomainObject hashing

parent cfbd598e
Pipeline #18358 passed with stage
in 5 minutes and 53 seconds
......@@ -41,7 +41,7 @@ class DomainObject(with_metaclass(
"""
def __init__(self):
self._ignore_for_hash = ['_ignore_for_hash']
self._needed_for_hash = []
@abc.abstractmethod
def __repr__(self):
......@@ -50,9 +50,8 @@ class DomainObject(with_metaclass(
def __hash__(self):
# Extract the identifying parts from the vars(self) dict.
result_hash = 0
for key in sorted(vars(self).keys()):
if key not in self._ignore_for_hash:
result_hash ^= vars(self)[key].__hash__() ^ int(hash(key)//117)
for key in self._needed_for_hash:
result_hash ^= hash(vars(self)[key])
return result_hash
def __eq__(self, x):
......@@ -73,10 +72,9 @@ class DomainObject(with_metaclass(
return True
if not isinstance(x, type(self)):
return False
for key in list(vars(self).keys()):
if key not in self._ignore_for_hash:
if vars(self)[key] != vars(x)[key]:
return False
for key in self._needed_for_hash:
if vars(self)[key] != vars(x)[key]:
return False
return True
def __ne__(self, x):
......
......@@ -20,6 +20,8 @@ from functools import reduce
from .domain_object import DomainObject
class DomainTuple(object):
_tupleCache = {}
def __init__(self, domain):
self._dom = self._parse_domain(domain)
self._axtuple = self._get_axes_tuple()
......@@ -40,7 +42,13 @@ class DomainTuple(object):
def make(domain):
if isinstance(domain, DomainTuple):
return domain
return DomainTuple(domain)
domain = DomainTuple._parse_domain(domain)
obj = DomainTuple._tupleCache.get(domain)
if obj is not None:
return obj
obj = DomainTuple(domain)
DomainTuple._tupleCache[domain] = obj
return obj
@staticmethod
def _parse_domain(domain):
......
......@@ -82,7 +82,7 @@ class GLSpace(Space):
def __init__(self, nlat, nlon=None):
super(GLSpace, self).__init__()
self._ignore_for_hash += ["_wgt"]
self._needed_for_hash += ["_nlat", "_nlon"]
self._nlat = self._parse_nlat(nlat)
self._nlon = self._parse_nlon(nlon)
......
......@@ -80,6 +80,7 @@ class HPSpace(Space):
def __init__(self, nside):
super(HPSpace, self).__init__()
self._needed_for_hash += ["_nside"]
self._nside = self._parse_nside(nside)
# ---Mandatory properties and methods---
......
......@@ -85,6 +85,7 @@ class LMSpace(Space):
def __init__(self, lmax):
super(LMSpace, self).__init__()
self._needed_for_hash += ["_lmax"]
self._lmax = self._parse_lmax(lmax)
def __repr__(self):
......
......@@ -144,7 +144,7 @@ class PowerSpace(Space):
def __init__(self, harmonic_partner, binbounds=None):
super(PowerSpace, self).__init__()
self._ignore_for_hash += ['_pindex', '_kindex', '_rho']
self._needed_for_hash += ['_harmonic_partner', '_binbounds']
if not (isinstance(harmonic_partner, Space) and
harmonic_partner.harmonic):
......
......@@ -82,6 +82,7 @@ class RGSpace(Space):
def __init__(self, shape, distances=None, harmonic=False):
super(RGSpace, self).__init__()
self._needed_for_hash += ["_distances", "_shape", "_harmonic"]
self._harmonic = bool(harmonic)
self._shape = self._parse_shape(shape)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment