def define_hash_preservation(cls): class C: pass class D(C): pass c = C() d = D() h_d = compute_hash(d) # force to be cached on 'd', but not on 'c' h_t = compute_hash(("Hi", None, (7.5, 2, d))) S = lltype.GcStruct('S', ('x', lltype.Signed), ('a', lltype.Array(lltype.Signed))) s = lltype.malloc(S, 15, zero=True) h_s = compute_identity_hash(s) # varsized: hash not saved/restored # def f(): if compute_hash(c) != compute_identity_hash(c): return 12 if compute_hash(d) != h_d: return 13 if compute_hash(("Hi", None, (7.5, 2, d))) != h_t: return 14 c2 = C() h_c2 = compute_hash(c2) if compute_hash(c2) != h_c2: return 15 if compute_identity_hash(s) == h_s: return 16 # unlikely i = 0 while i < 6: rgc.collect() if compute_hash(c2) != h_c2: return i i += 1 return 42 return f
def seen_prebuilt_key(self, x): # In case we are an r_dict, we don't ask for the hash ourselves. # Note that if the custom hashing function ends up asking for # the hash of x, then it must use compute_hash() itself, so it # works out. if not self.dictkey.custom_eq_hash: compute_hash(x)
def _compute_hash(self, space, x): from rpython.rlib.rarithmetic import intmask if not self.fields and self.subdtype is None: endian = self.byteorder if endian == NPY.NATIVE: endian = NPY.NATBYTE flags = 0 y = 0x345678 y = intmask((1000003 * y) ^ ord(self.kind[0])) y = intmask((1000003 * y) ^ ord(endian[0])) y = intmask((1000003 * y) ^ flags) y = intmask((1000003 * y) ^ self.elsize) if self.is_flexible(): y = intmask((1000003 * y) ^ self.alignment) return intmask((1000003 * x) ^ y) if self.fields: for name, (offset, subdtype) in self.fields.iteritems(): assert isinstance(subdtype, W_Dtype) y = intmask(1000003 * (0x345678 ^ compute_hash(name))) y = intmask(1000003 * (y ^ compute_hash(offset))) y = intmask(1000003 * (y ^ subdtype._compute_hash(space, 0x345678))) x = intmask(x ^ y) if self.subdtype is not None: for s in self.shape: x = intmask((1000003 * x) ^ compute_hash(s)) x = self.base._compute_hash(space, x) return x
def hash_value(k): if isinstance(k, values.W_Fixnum): return compute_hash(k.value) if isinstance(k, values.W_Character): return ord(k.value) else: return compute_hash(k)
def test_hash_preservation(self): class C: pass class D(C): pass c = C() d = D() compute_hash(d) # force to be cached on 'd', but not on 'c' # def fn(): d2 = D() return str((compute_hash(d2), current_object_addr_as_int(d2), compute_hash(c), compute_hash(d), compute_hash(("Hi", None, (7.5, 2, d))))) f = self.getcompiled(fn) res = f() res = eval(res) # xxx the next line is too precise, checking the exact implementation assert res[0] == ~res[1] assert res[2] != compute_hash(c) # likely assert res[3] == compute_hash(d) assert res[4] == compute_hash(("Hi", None, (7.5, 2, d)))
def test_hash_preservation(self): class C: pass class D(C): pass c = C() d = D() compute_hash(d) # force to be cached on 'd', but not on 'c' # def fn(): d2 = D() return str((compute_hash(d2), current_object_addr_as_int(d2), compute_hash(c), compute_hash(d), compute_hash(("Hi", None, (7.5, 2))))) f = self.getcompiled(fn, []) res = f() # xxx the next line is too precise, checking the exact implementation res = [int(a) for a in res[1:-1].split(",")] if res[0] != res[1]: assert res[0] == -res[1] - 1 assert res[2] != compute_hash(c) # likely assert res[3] != compute_hash(d) # likely *not* preserved assert res[4] == compute_hash(("Hi", None, (7.5, 2)))
def fn(): d2 = D() return str((compute_hash(d2), current_object_addr_as_int(d2), compute_hash(c), compute_hash(d), compute_hash(("Hi", None, (7.5, 2, d)))))
def hash_value(k): if isinstance(k, values.W_Fixnum): return compute_hash(k.value) if isinstance(k, values.W_Character): return ord(k.value) else: return compute_hash(k)
def compute_hash(self, space, x): from rpython.rlib.rarithmetic import intmask if self.fields is None and self.subdtype is None: endian = self.byteorder if endian == NPY.NATIVE: endian = NPY.NATBYTE flags = 0 y = 0x345678 for v in (ord(self.kind[0]), ord(endian[0]), flags, self.elsize, self.alignment): y = intmask((1000003 * y) ^ v) return intmask((1000003 * x) ^ y) if self.fields is not None: fields = self.fields.items() DescrFieldSort(fields).sort() for name, (offset, subdtype) in fields: assert isinstance(subdtype, W_Dtype) x = intmask((1000003 * x) ^ compute_hash(name)) x = subdtype.compute_hash(space, x) x = intmask((1000003 * x) ^ compute_hash(offset)) if self.subdtype is not None: for s in self.shape: x = intmask((1000003 * x) ^ compute_hash(s)) x = self.base.compute_hash(space, x) return x
def seen_prebuilt_key(self, x): # In case we are an r_dict, we don't ask for the hash ourselves. # Note that if the custom hashing function ends up asking for # the hash of x, then it must use compute_hash() itself, so it # works out. if not self.dictkey.custom_eq_hash: compute_hash(x)
def test_hash_preservation(self): class C: pass class D(C): pass c = C() d = D() compute_hash(d) # force to be cached on 'd', but not on 'c' # def fn(): d2 = D() return str((compute_hash(d2), current_object_addr_as_int(d2), compute_hash(c), compute_hash(d), compute_hash(("Hi", None, (7.5, 2, d))))) f = self.getcompiled(fn) res = f() res = eval(res) # xxx the next line is too precise, checking the exact implementation assert res[0] == ~res[1] assert res[2] != compute_hash(c) # likely assert res[3] == compute_hash(d) assert res[4] == compute_hash(("Hi", None, (7.5, 2, d)))
def _compute_hash(self, space, x): from rpython.rlib.rarithmetic import intmask if not self.fields and self.subdtype is None: endian = self.byteorder if endian == NPY.NATIVE: endian = NPY.NATBYTE flags = 0 y = 0x345678 y = intmask((1000003 * y) ^ ord(self.kind[0])) y = intmask((1000003 * y) ^ ord(endian[0])) y = intmask((1000003 * y) ^ flags) y = intmask((1000003 * y) ^ self.elsize) if self.is_flexible(): y = intmask((1000003 * y) ^ self.alignment) return intmask((1000003 * x) ^ y) if self.fields: for name, (offset, subdtype) in self.fields.iteritems(): assert isinstance(subdtype, W_Dtype) y = intmask(1000003 * (0x345678 ^ compute_hash(name))) y = intmask(1000003 * (y ^ compute_hash(offset))) y = intmask(1000003 * (y ^ subdtype._compute_hash(space, 0x345678))) x = intmask(x ^ y) if self.subdtype is not None: for s in self.shape: x = intmask((1000003 * x) ^ compute_hash(s)) x = self.base._compute_hash(space, x) return x
def f(i): d = {i + .0: 5, i + .5: 6} total = 0 for k, v, h in iteritems_with_hash(d): total += k * h * v total -= (i + 0.0) * compute_hash(i + 0.0) * 5 total -= (i + 0.5) * compute_hash(i + 0.5) * 6 return total
def f(i): d = {i + .0: 5, i + .5: 6} total = 0 for k, h in iterkeys_with_hash(d): total += k * h total -= (i + 0.0) * compute_hash(i + 0.0) total -= (i + 0.5) * compute_hash(i + 0.5) return total
def f(i): d = {i + .0: 5, i + .5: 6} total = 0 for k, h in iterkeys_with_hash(d): total += k * h total -= (i + 0.0) * compute_hash(i + 0.0) total -= (i + 0.5) * compute_hash(i + 0.5) return total
def f(i): d = {i + .0: 5, i + .5: 6} total = 0 for k, v, h in iteritems_with_hash(d): total += k * h * v total -= (i + 0.0) * compute_hash(i + 0.0) * 5 total -= (i + 0.5) * compute_hash(i + 0.5) * 6 return total
def f(i): d = {i + .5: 42, i + .6: -612} delitem_with_hash(d, i + .5, compute_hash(i + .5)) try: delitem_with_hash(d, i + .5, compute_hash(i + .5)) except KeyError: pass else: raise AssertionError return 0
def f(i): d = {i + .5: 42, i + .6: -612} delitem_with_hash(d, i + .5, compute_hash(i + .5)) try: delitem_with_hash(d, i + .5, compute_hash(i + .5)) except KeyError: pass else: raise AssertionError return 0
def fetch(n): if n == 0: return d1.get("foo", -1) if n == 1: return g.v1.get("foo", -1) if n == 2: return compute_hash("foo") if n == 3: return d2.get(u"foo", -1) if n == 4: return g.v2.get(u"foo", -1) if n == 5: return compute_hash(u"foo") if n == 6: return d2.get(u"\u1234\u5678", -1) if n == 7: return g.v2.get(u"\u1234\u5678", -1) if n == 8: return compute_hash(u"\u1234\u5678") assert 0
def eq_hash_code(v): t = type(v) if t is values.W_Fixnum: return v if t is values.W_Flonum: hash = objectmodel.compute_hash(v.value) elif t is values.W_Character: hash = objectmodel.compute_hash(v.value) else: hash = objectmodel.compute_hash(v) return values.W_Fixnum(hash)
def eq_hash_code(v): t = type(v) if t is values.W_Fixnum: return v if t is values.W_Flonum: hash = objectmodel.compute_hash(v.value) elif t is values.W_Character: hash = objectmodel.compute_hash(v.value) else: hash = objectmodel.compute_hash(v) return values.W_Fixnum(hash)
def getstrhash(self, op, mode): from rpython.jit.metainterp.optimizeopt import vstring if mode is vstring.mode_string: s = self._unpack_str(vstring.mode_string) if s is None: return None return ConstInt(compute_hash(s)) else: s = self._unpack_str(vstring.mode_unicode) if s is None: return None return ConstInt(compute_hash(s))
def getstrhash(self, op, mode): from rpython.jit.metainterp.optimizeopt import vstring if mode is vstring.mode_string: s = self._unpack_str(vstring.mode_string) if s is None: return None return ConstInt(compute_hash(s)) else: s = self._unpack_str(vstring.mode_unicode) if s is None: return None return ConstInt(compute_hash(s))
def __hash__(self): if USE_RPYTHON_CODE: from rpython.rlib.objectmodel import compute_hash hash_str = compute_hash(self.__repr__()) else: hash_str = hash(self.__repr__()) return hash_str
def next(self): i = self.i if i >= len(self.list1): raise StopIteration self.i = i + 1 key = self.list1[i] return (key, self.list2[i], objectmodel.compute_hash(key))
def find_map_attr(self, name, index): # attr cache space = self.space cache = space.fromcache(MapAttrCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 attrs_as_int = objectmodel.current_object_addr_as_int(self) # ^^^Note: see comment in typeobject.py for # _pure_lookup_where_with_method_cache() # unrolled hash computation for 2-tuple c1 = 0x345678 c2 = 1000003 hash_name = objectmodel.compute_hash(name) hash_selector = intmask((c2 * ((c2 * c1) ^ hash_name)) ^ index) product = intmask(attrs_as_int * hash_selector) attr_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: same comment too cached_attr = cache.attrs[attr_hash] if cached_attr is self: cached_name = cache.names[attr_hash] cached_index = cache.indexes[attr_hash] if cached_name == name and cached_index == index: attr = cache.cached_attrs[attr_hash] if space.config.objspace.std.withmethodcachecounter: cache.hits[name] = cache.hits.get(name, 0) + 1 return attr attr = self._find_map_attr(name, index) cache.attrs[attr_hash] = self cache.names[attr_hash] = name cache.indexes[attr_hash] = index cache.cached_attrs[attr_hash] = attr if space.config.objspace.std.withmethodcachecounter: cache.misses[name] = cache.misses.get(name, 0) + 1 return attr
def _find_map_attr_cache(self, selector): space = self.space cache = space.fromcache(MapAttrCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 attrs_as_int = objectmodel.current_object_addr_as_int(self) # ^^^Note: see comment in typeobject.py for # _pure_lookup_where_with_method_cache() hash_selector = objectmodel.compute_hash(selector) product = intmask(attrs_as_int * hash_selector) attr_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: same comment too cached_attr = cache.attrs[attr_hash] if cached_attr is self: cached_selector = cache.selectors[attr_hash] if cached_selector == selector: attr = cache.cached_attrs[attr_hash] if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.hits[name] = cache.hits.get(name, 0) + 1 return attr attr = self._find_map_attr(selector) cache.attrs[attr_hash] = self cache.selectors[attr_hash] = selector cache.cached_attrs[attr_hash] = attr if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.misses[name] = cache.misses.get(name, 0) + 1 return attr
def descr_hash(self, space): mult = 1000003 x = 0x345678 z = typelen for i in iter_n: value = getattr(self, 'value%s' % i) if typetuple[i] == object: y = space.int_w(space.hash(value)) elif typetuple[i] == int: # mimic cpythons behavior of a hash value of -2 for -1 y = value if y == -1: y = -2 elif typetuple[i] == float: # get the correct hash for float which is an # integer & other less frequent cases from pypy.objspace.std.floatobject import _hash_float y = _hash_float(space, value) else: y = compute_hash(value) x = (x ^ y) * mult z -= 1 mult += 82520 + z + z x += 97531 return space.wrap(intmask(x))
def descr_hash(self, space): mult = 1000003 x = 0x345678 z = typelen for i in iter_n: value = getattr(self, 'value%s' % i) if typetuple[i] == object: y = space.int_w(space.hash(value)) elif typetuple[i] == int: # mimic cpythons behavior of a hash value of -2 for -1 y = value if y == -1: y = -2 elif typetuple[i] == float: # get the correct hash for float which is an # integer & other less frequent cases from pypy.objspace.std.floatobject import _hash_float y = _hash_float(space, value) else: y = compute_hash(value) x = (x ^ y) * mult z -= 1 mult += 82520 + z + z x += 97531 return space.newint(intmask(x))
def _find_map_attr_cache(self, name, index): space = self.space cache = space.fromcache(MapAttrCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 attrs_as_int = objectmodel.current_object_addr_as_int(self) # ^^^Note: see comment in typeobject.py for # _pure_lookup_where_with_method_cache() # unrolled hash computation for 2-tuple c1 = 0x345678 c2 = 1000003 hash_name = objectmodel.compute_hash(name) hash_selector = intmask((c2 * ((c2 * c1) ^ hash_name)) ^ index) product = intmask(attrs_as_int * hash_selector) attr_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: same comment too cached_attr = cache.attrs[attr_hash] if cached_attr is self: cached_name = cache.names[attr_hash] cached_index = cache.indexes[attr_hash] if cached_name == name and cached_index == index: attr = cache.cached_attrs[attr_hash] if space.config.objspace.std.withmethodcachecounter: cache.hits[name] = cache.hits.get(name, 0) + 1 return attr attr = self._find_map_attr(name, index) cache.attrs[attr_hash] = self cache.names[attr_hash] = name cache.indexes[attr_hash] = index cache.cached_attrs[attr_hash] = attr if space.config.objspace.std.withmethodcachecounter: cache.misses[name] = cache.misses.get(name, 0) + 1 return attr
def _find_map_attr_cache(self, selector): space = self.space cache = space.fromcache(MapAttrCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 attrs_as_int = objectmodel.current_object_addr_as_int(self) # ^^^Note: see comment in typeobject.py for # _pure_lookup_where_with_method_cache() hash_selector = objectmodel.compute_hash(selector) product = intmask(attrs_as_int * hash_selector) attr_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: same comment too cached_attr = cache.attrs[attr_hash] if cached_attr is self: cached_selector = cache.selectors[attr_hash] if cached_selector == selector: attr = cache.cached_attrs[attr_hash] if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.hits[name] = cache.hits.get(name, 0) + 1 return attr attr = self._find_map_attr(selector) cache.attrs[attr_hash] = self cache.selectors[attr_hash] = selector cache.cached_attrs[attr_hash] = attr if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.misses[name] = cache.misses.get(name, 0) + 1 return attr
def hash(self): value = 0x345678 value = (1000003 * value) ^ self.prefix.hash() for item in self.pathseq: value = (1000003 * value) ^ compute_hash(item) value = value ^ len(self.pathseq) return intmask(value)
def next(self): i = self.i if i >= len(self.list1): raise StopIteration self.i = i + 1 key = self.list1[i] return (key, self.list2[i], objectmodel.compute_hash(key))
def f(n): x = u'' while n > 13: myjitdriver.can_enter_jit(n=n, x=x) myjitdriver.jit_merge_point(n=n, x=x) x += unichr(n) n -= 1 return compute_hash(x)
def __hash__(self): if USE_RPYTHON_CODE: from rpython.rlib.objectmodel import compute_hash hash_str = compute_hash(self.__state_to_string(useHash=True)) else: hash_str = hash(self.__repr__()) #print "XXX" + self.__repr__() + str(hash_str) + "XXX" return hash_str
def descr_hash(self, space): if self._hash == -1: self._check_released(space) if not self.view.readonly: raise oefmt(space.w_ValueError, "cannot hash writable memoryview object") self._hash = compute_hash(self.view.as_str()) return space.newint(self._hash)
def descr_code__hash__(self): space = self.space result = compute_hash(self.co_name) result ^= self.co_argcount result ^= self.co_nlocals result ^= self.co_flags result ^= self.co_firstlineno result ^= compute_hash(self.co_code) for name in self.co_varnames: result ^= compute_hash(name) for name in self.co_freevars: result ^= compute_hash(name) for name in self.co_cellvars: result ^= compute_hash(name) w_result = space.wrap(intmask(result)) for w_name in self.co_names_w: w_result = space.xor(w_result, space.hash(w_name)) for w_const in self.co_consts_w: w_result = space.xor(w_result, space.hash(w_const)) return w_result
def descr_code__hash__(self): space = self.space result = compute_hash(self.co_name) result ^= self.co_argcount result ^= self.co_nlocals result ^= self.co_flags result ^= self.co_firstlineno result ^= compute_hash(self.co_code) for name in self.co_varnames: result ^= compute_hash(name) for name in self.co_freevars: result ^= compute_hash(name) for name in self.co_cellvars: result ^= compute_hash(name) w_result = space.wrap(intmask(result)) for w_name in self.co_names_w: w_result = space.xor(w_result, space.hash(w_name)) for w_const in self.co_consts_w: w_result = space.xor(w_result, space.hash(w_const)) return w_result
def f(n): x = u'' while n > 13: myjitdriver.can_enter_jit(n=n, x=x) myjitdriver.jit_merge_point(n=n, x=x) x += unichr(n) n -= 1 return compute_hash(x)
def test_compute_hash_across_translation(self): class Foo(object): pass q = Foo() def f(i): assert compute_hash(None) == 0 assert compute_hash(i) == h_42 assert compute_hash(i + 1.0) == h_43_dot_0 assert compute_hash((i + 3) / 6.0) == h_7_dot_5 assert compute_hash("Hello" + str(i)) == h_Hello42 if i == 42: p = None else: p = Foo() assert compute_hash(p) == h_None assert compute_hash(("world", None, i, 7.5)) == h_tuple assert compute_hash(q) == h_q return i * 2 h_42 = compute_hash(42) h_43_dot_0 = compute_hash(43.0) h_7_dot_5 = compute_hash(7.5) h_Hello42 = compute_hash("Hello42") h_None = compute_hash(None) h_tuple = compute_hash(("world", None, 42, 7.5)) h_q = compute_hash(q) res = self.interpret(f, [42]) assert res == 84
def test_compute_hash_across_translation(self): class Foo(object): pass q = Foo() def f(i): assert compute_hash(None) == 0 assert compute_hash(i) == h_42 assert compute_hash(i + 1.0) == h_43_dot_0 assert compute_hash((i + 3) / 6.0) == h_7_dot_5 assert compute_hash("Hello" + str(i)) == h_Hello42 if i == 42: p = None else: p = Foo() assert compute_hash(p) == h_None assert compute_hash(("world", None, i, 7.5)) == h_tuple assert compute_hash(q) == h_q return i * 2 h_42 = compute_hash(42) h_43_dot_0 = compute_hash(43.0) h_7_dot_5 = compute_hash(7.5) h_Hello42 = compute_hash("Hello42") h_None = compute_hash(None) h_tuple = compute_hash(("world", None, 42, 7.5)) h_q = compute_hash(q) res = self.interpret(f, [42]) assert res == 84
def f(n): x = '' while n > 13: myjitdriver.can_enter_jit(n=n, x=x) myjitdriver.jit_merge_point(n=n, x=x) #print len(x), x x += chr(n) n -= 1 return compute_hash(x)
def f(n): x = '' while n > 13: myjitdriver.can_enter_jit(n=n, x=x) myjitdriver.jit_merge_point(n=n, x=x) #print len(x), x x += chr(n) n -= 1 return compute_hash(x)
def fn(n): from rpython.rlib import rsiphash rsiphash.enable_siphash24() #assert str(n) not in prebuilt_d <- this made the test pass, # before the fix which was that iterkeys_with_hash() # didn't do the initial rehashing on its own for key, h in objectmodel.iterkeys_with_hash(prebuilt_d): print key, h assert h == compute_hash(key) return 42
def hash_fn(this): mult = 1000003 x = 0x345678 z = len(this) for item in this: y = compute_hash(item) x = (x ^ y) * mult z -= 1 mult += 82520 + z + z x += 97531 return intmask(x)
def _test_hash_string(self, algo): s = "hello" u = u"world" v = u"\u1234\u2318+\u2bcd\u2102" hash_s = compute_hash(s) hash_u = compute_hash(u) hash_v = compute_hash(v) assert hash_s == compute_hash(u"hello") # same hash because it's assert hash_u == compute_hash("world") # a latin-1 unicode # def fn(length): if algo == "siphash24": from rpython.rlib import rsiphash rsiphash.enable_siphash24() assert length >= 1 return str(( compute_hash(s), compute_hash(u), compute_hash(v), compute_hash(s[0] + s[1:length]), compute_hash(u[0] + u[1:length]), compute_hash(v[0] + v[1:length]), )) assert fn(5) == str((hash_s, hash_u, hash_v, hash_s, hash_u, hash_v)) f = self.getcompiled(fn, [int]) res = f(5) res = [int(a) for a in res[1:-1].split(",")] if algo == "fnv": assert res[0] == hash_s assert res[1] == hash_u assert res[2] == hash_v else: assert res[0] != hash_s assert res[1] != hash_u assert res[2] != hash_v assert res[3] == res[0] assert res[4] == res[1] assert res[5] == res[2]
def f(i): assert compute_hash(None) == 0 assert compute_hash(i) == h_42 assert compute_hash(i + 1.0) == h_43_dot_0 assert compute_hash((i + 3) / 6.0) == h_7_dot_5 assert compute_hash("Hello" + str(i)) == h_Hello42 if i == 42: p = None else: p = Foo() assert compute_hash(p) == h_None assert compute_hash(("world", None, i, 7.5)) == h_tuple assert compute_hash(q) == h_q return i * 2
def f(i): assert compute_hash(None) == 0 assert compute_hash(i) == h_42 assert compute_hash(i + 1.0) == h_43_dot_0 assert compute_hash((i + 3) / 6.0) == h_7_dot_5 assert compute_hash("Hello" + str(i)) == h_Hello42 if i == 42: p = None else: p = Foo() assert compute_hash(p) == h_None assert compute_hash(("world", None, i, 7.5)) == h_tuple assert compute_hash(q) == h_q return i * 2
def equal_hash_code(v): # only for improper path cache entries if isinstance(v, values.W_Cons): if v.is_proper_list(): return values.W_Fixnum.ZERO nm = v.car() p = v.cdr() if isinstance(nm, values_string.W_String) and \ isinstance(p, values.W_Path) and \ isinstance(p.path, str): return values.W_Fixnum(objectmodel.compute_hash((nm.tostring(), p.path))) return values.W_Fixnum.ZERO
def test_compute_hash(): from rpython.rlib.objectmodel import _hash_string, _hash_float, _hash_tuple assert compute_hash("Hello") == _hash_string("Hello") assert compute_hash(7) == 7 assert compute_hash(-3.5) == _hash_float(-3.5) assert compute_hash(None) == 0 assert compute_hash(("world", None, 7)) == _hash_tuple(("world", None, 7)) # class Foo(object): def __hash__(self): return 42 foo = Foo() h = compute_hash(foo) assert h == object.__hash__(foo) assert h == getattr(foo, '__precomputed_identity_hash') assert compute_hash(None) == 0
def fn(length): if algo == "siphash24": from rpython.rlib import rsiphash rsiphash.enable_siphash24() assert length >= 1 return str(( compute_hash(s), compute_hash(u), compute_hash(v), compute_hash(s[0] + s[1:length]), compute_hash(u[0] + u[1:length]), compute_hash(v[0] + v[1:length]), ))
def test_compute_hash(): from rpython.rlib.objectmodel import _hash_string, _hash_float, _hash_tuple assert compute_hash("Hello") == _hash_string("Hello") assert compute_hash(7) == 7 assert compute_hash(-3.5) == _hash_float(-3.5) assert compute_hash(None) == 0 assert compute_hash(("world", None, 7)) == _hash_tuple(("world", None, 7)) # class Foo(object): def __hash__(self): return 42 foo = Foo() h = compute_hash(foo) assert h == object.__hash__(foo) assert compute_hash(None) == 0
def _pure_lookup_where_with_method_cache(w_self, name, version_tag): space = w_self.space cache = space.fromcache(MethodCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 version_tag_as_int = current_object_addr_as_int(version_tag) # ^^^Note: if the version_tag object is moved by a moving GC, the # existing method cache entries won't be found any more; new # entries will be created based on the new address. The # assumption is that the version_tag object won't keep moving all # the time - so using the fast current_object_addr_as_int() instead # of a slower solution like hash() is still a good trade-off. hash_name = compute_hash(name) product = intmask(version_tag_as_int * hash_name) method_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: we used to just take product>>SHIFT2, but on 64-bit # platforms SHIFT2 is really large, and we loose too much information # that way (as shown by failures of the tests that typically have # method names like 'f' who hash to a number that has only ~33 bits). cached_version_tag = cache.versions[method_hash] if cached_version_tag is version_tag: cached_name = cache.names[method_hash] if cached_name is name: tup = cache.lookup_where[method_hash] if space.config.objspace.std.withmethodcachecounter: cache.hits[name] = cache.hits.get(name, 0) + 1 # print "hit", w_self, name return tup tup = w_self._lookup_where_all_typeobjects(name) cache.versions[method_hash] = version_tag cache.names[method_hash] = name cache.lookup_where[method_hash] = tup if space.config.objspace.std.withmethodcachecounter: cache.misses[name] = cache.misses.get(name, 0) + 1 # print "miss", w_self, name return tup
def _pure_lookup_where_with_method_cache(w_self, name, version_tag): space = w_self.space cache = space.fromcache(MethodCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp SHIFT1 = SHIFT2 - 5 version_tag_as_int = current_object_addr_as_int(version_tag) # ^^^Note: if the version_tag object is moved by a moving GC, the # existing method cache entries won't be found any more; new # entries will be created based on the new address. The # assumption is that the version_tag object won't keep moving all # the time - so using the fast current_object_addr_as_int() instead # of a slower solution like hash() is still a good trade-off. hash_name = compute_hash(name) product = intmask(version_tag_as_int * hash_name) method_hash = (r_uint(product) ^ (r_uint(product) << SHIFT1)) >> SHIFT2 # ^^^Note2: we used to just take product>>SHIFT2, but on 64-bit # platforms SHIFT2 is really large, and we loose too much information # that way (as shown by failures of the tests that typically have # method names like 'f' who hash to a number that has only ~33 bits). cached_version_tag = cache.versions[method_hash] if cached_version_tag is version_tag: cached_name = cache.names[method_hash] if cached_name is name: tup = cache.lookup_where[method_hash] if space.config.objspace.std.withmethodcachecounter: cache.hits[name] = cache.hits.get(name, 0) + 1 # print "hit", w_self, name return tup tup = w_self._lookup_where_all_typeobjects(name) cache.versions[method_hash] = version_tag cache.names[method_hash] = name cache.lookup_where[method_hash] = tup if space.config.objspace.std.withmethodcachecounter: cache.misses[name] = cache.misses.get(name, 0) + 1 # print "miss", w_self, name return tup
def f(): if compute_hash(c) != compute_identity_hash(c): return 12 if compute_hash(d) != h_d: return 13 if compute_hash(("Hi", None, (7.5, 2, d))) != h_t: return 14 c2 = C() h_c2 = compute_hash(c2) if compute_hash(c2) != h_c2: return 15 if compute_identity_hash(s) == h_s: return 16 # unlikely i = 0 while i < 6: rgc.collect() if compute_hash(c2) != h_c2: return i i += 1 return 42
def hash_equal(self, info=None): return objectmodel.compute_hash(self) # default implementation
def fn(): d2 = D() return str((compute_hash(d2), current_object_addr_as_int(d2), compute_hash(c), compute_hash(d), compute_hash(("Hi", None, (7.5, 2, d)))))
def f(i): d = {i + .5: 42, i + .6: -612} return getitem_with_hash(d, i + .5, compute_hash(i + .5))
def f(i): d = {} setitem_with_hash(d, i + .5, compute_hash(i + .5), 42) setitem_with_hash(d, i + .6, compute_hash(i + .6), -612) return d[i + .5]