Author: Alex Gaynor <alex.gay...@gmail.com> Branch: Changeset: r50517:7907ec2d1927 Date: 2011-12-14 15:30 -0500 http://bitbucket.org/pypy/pypy/changeset/7907ec2d1927/
Log: merged upstream diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py --- a/pypy/interpreter/baseobjspace.py +++ b/pypy/interpreter/baseobjspace.py @@ -190,8 +190,8 @@ def is_w(self, space, w_other): return self is w_other - def unique_id(self, space): - return space.wrap(compute_unique_id(self)) + def immutable_unique_id(self, space): + return None def str_w(self, space): w_msg = typed_unwrap_error_msg(space, "string", self) @@ -706,7 +706,10 @@ return w_two.is_w(self, w_one) def id(self, w_obj): - return w_obj.unique_id(self) + w_result = w_obj.immutable_unique_id(self) + if w_result is None: + w_result = self.wrap(compute_unique_id(w_obj)) + return w_result def hash_w(self, w_obj): """shortcut for space.int_w(space.hash(w_obj))""" diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py --- a/pypy/interpreter/typedef.py +++ b/pypy/interpreter/typedef.py @@ -54,7 +54,11 @@ # Hash support def default_identity_hash(space, w_obj): - return space.wrap(compute_identity_hash(w_obj)) + w_unique_id = w_obj.immutable_unique_id(space) + if w_unique_id is None: # common case + return space.wrap(compute_identity_hash(w_obj)) + else: + return space.hash(w_unique_id) # ____________________________________________________________ # diff --git a/pypy/jit/codewriter/jtransform.py b/pypy/jit/codewriter/jtransform.py --- a/pypy/jit/codewriter/jtransform.py +++ b/pypy/jit/codewriter/jtransform.py @@ -536,11 +536,15 @@ d = op.args[1].value.copy() assert d['flavor'] == 'raw' d.pop('flavor') + track_allocation = d.pop('track_allocation', True) if d: raise UnsupportedMallocFlags(d) - ARRAY = op.args[0].concretetype.TO - return self._do_builtin_call(op, 'raw_free', [op.args[0]], - extra = (ARRAY,), extrakey = ARRAY) + STRUCT = op.args[0].concretetype.TO + name = 'raw_free' + if not track_allocation: + name += '_no_track_allocation' + return self._do_builtin_call(op, name, [op.args[0]], + extra = (STRUCT,), extrakey = STRUCT) def rewrite_op_getarrayitem(self, op): ARRAY = op.args[0].concretetype.TO diff --git a/pypy/jit/codewriter/support.py b/pypy/jit/codewriter/support.py --- a/pypy/jit/codewriter/support.py +++ b/pypy/jit/codewriter/support.py @@ -605,7 +605,8 @@ def build_ll_1_raw_malloc_varsize(ARRAY): def _ll_1_raw_malloc_varsize(n): return lltype.malloc(ARRAY, n, flavor='raw', zero=zero, - add_memory_pressure=add_memory_pressure) + add_memory_pressure=add_memory_pressure, + track_allocation=track_allocation) return _ll_1_raw_malloc_varsize return build_ll_1_raw_malloc_varsize @@ -632,7 +633,8 @@ def build_ll_0_raw_malloc_fixedsize(STRUCT): def _ll_0_raw_malloc_fixedsize(): return lltype.malloc(STRUCT, flavor='raw', zero=zero, - add_memory_pressure=add_memory_pressure) + add_memory_pressure=add_memory_pressure, + track_allocation=track_allocation) return _ll_0_raw_malloc_fixedsize return build_ll_0_raw_malloc_fixedsize @@ -653,10 +655,19 @@ build_ll_0_raw_malloc_fixedsize_add_memory_pressure_no_track_allocation = ( build_raw_malloc_fixedsize_builder(add_memory_pressure=True, track_allocation=False)) - def build_ll_1_raw_free(ARRAY): - def _ll_1_raw_free(p): - lltype.free(p, flavor='raw') - return _ll_1_raw_free + def build_raw_free_builder(track_allocation=True): + def build_ll_1_raw_free(ARRAY): + def _ll_1_raw_free(p): + lltype.free(p, flavor='raw', + track_allocation=track_allocation) + return _ll_1_raw_free + return build_ll_1_raw_free + + build_ll_1_raw_free = ( + build_raw_free_builder()) + build_ll_1_raw_free_no_track_allocation = ( + build_raw_free_builder(track_allocation=False)) + class OOtypeHelpers: diff --git a/pypy/jit/codewriter/test/test_jtransform.py b/pypy/jit/codewriter/test/test_jtransform.py --- a/pypy/jit/codewriter/test/test_jtransform.py +++ b/pypy/jit/codewriter/test/test_jtransform.py @@ -590,6 +590,23 @@ assert op1.opname == '-live-' assert op1.args == [] +def test_raw_free(): + S = lltype.Struct('dummy', ('x', lltype.Signed)) + for flag in [True, False]: + flags = Constant({'flavor': 'raw', 'track_allocation': flag}, + lltype.Void) + op = SpaceOperation('free', [varoftype(lltype.Ptr(S)), flags], + varoftype(lltype.Void)) + tr = Transformer(FakeCPU(), FakeResidualCallControl()) + op0, op1 = tr.rewrite_operation(op) + assert op0.opname == 'residual_call_ir_v' + if flag: + pseudo_op_name = 'raw_free' + else: + pseudo_op_name = 'raw_free_no_track_allocation' + assert op0.args[0].value == pseudo_op_name # pseudo-function as a str + assert op1.opname == '-live-' + def test_rename_on_links(): v1 = Variable() v2 = Variable(); v2.concretetype = llmemory.Address diff --git a/pypy/objspace/std/complexobject.py b/pypy/objspace/std/complexobject.py --- a/pypy/objspace/std/complexobject.py +++ b/pypy/objspace/std/complexobject.py @@ -31,9 +31,9 @@ imag2 = float2longlong(imag2) return real1 == real2 and imag1 == imag2 - def unique_id(self, space): + def immutable_unique_id(self, space): if self.user_overridden_class: - return W_Object.unique_id(self, space) + return None from pypy.rlib.longlong2float import float2longlong from pypy.objspace.std.model import IDTAG_COMPLEX as tag real = space.float_w(space.getattr(self, space.wrap("real"))) diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py --- a/pypy/objspace/std/floatobject.py +++ b/pypy/objspace/std/floatobject.py @@ -34,9 +34,9 @@ two = float2longlong(space.float_w(w_other)) return one == two - def unique_id(self, space): + def immutable_unique_id(self, space): if self.user_overridden_class: - return W_Object.unique_id(self, space) + return None from pypy.rlib.longlong2float import float2longlong from pypy.objspace.std.model import IDTAG_FLOAT as tag val = float2longlong(space.float_w(self)) diff --git a/pypy/objspace/std/intobject.py b/pypy/objspace/std/intobject.py --- a/pypy/objspace/std/intobject.py +++ b/pypy/objspace/std/intobject.py @@ -26,9 +26,9 @@ return self is w_other return space.int_w(self) == space.int_w(w_other) - def unique_id(self, space): + def immutable_unique_id(self, space): if self.user_overridden_class: - return W_Object.unique_id(self, space) + return None from pypy.objspace.std.model import IDTAG_INT as tag b = space.bigint_w(self) b = b.lshift(3).or_(rbigint.fromint(tag)) diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py --- a/pypy/objspace/std/longobject.py +++ b/pypy/objspace/std/longobject.py @@ -18,9 +18,9 @@ return self is w_other return space.bigint_w(self).eq(space.bigint_w(w_other)) - def unique_id(self, space): + def immutable_unique_id(self, space): if self.user_overridden_class: - return W_Object.unique_id(self, space) + return None from pypy.objspace.std.model import IDTAG_LONG as tag b = space.bigint_w(self) b = b.lshift(3).or_(rbigint.fromint(tag)) diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py --- a/pypy/objspace/std/stringobject.py +++ b/pypy/objspace/std/stringobject.py @@ -32,9 +32,9 @@ return False return space.str_w(self) is space.str_w(w_other) - def unique_id(self, space): + def immutable_unique_id(self, space): if self.user_overridden_class: - return W_Object.unique_id(self, space) + return None return space.wrap(compute_unique_id(space.str_w(self))) diff --git a/pypy/objspace/std/test/test_obj.py b/pypy/objspace/std/test/test_obj.py --- a/pypy/objspace/std/test/test_obj.py +++ b/pypy/objspace/std/test/test_obj.py @@ -253,6 +253,12 @@ y = 2j assert id(x) != id(y) + def test_object_hash_immutable(self): + x = 42 + y = 40 + y += 2 + assert object.__hash__(x) == object.__hash__(y) + def test_isinstance_shortcut(): from pypy.objspace.std import objspace diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py --- a/pypy/objspace/std/unicodeobject.py +++ b/pypy/objspace/std/unicodeobject.py @@ -32,9 +32,9 @@ return False return space.unicode_w(self) is space.unicode_w(w_other) - def unique_id(self, space): + def immutable_unique_id(self, space): if self.user_overridden_class: - return W_Object.unique_id(self, space) + return None return space.wrap(compute_unique_id(space.unicode_w(self))) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit