[pypy-commit] pypy keys_with_hash: Move away from ExtRegistryEntries
arigo
noreply at buildbot.pypy.org
Tue Sep 1 10:08:44 CEST 2015
Author: Armin Rigo <arigo at tunes.org>
Branch: keys_with_hash
Changeset: r79334:d2748862570b
Date: 2015-09-01 10:07 +0200
http://bitbucket.org/pypy/pypy/changeset/d2748862570b/
Log: Move away from ExtRegistryEntries
diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -369,7 +369,6 @@
return None # r_dict: can throw anything
return [] # else: no possible exception
-# also used for objectmodel.contains_with_hash()
def dict_contains(s_dct, s_element):
s_dct.dictdef.generalize_key(s_element)
if s_dct._is_empty():
@@ -455,6 +454,9 @@
def method_iteritems(self):
return SomeIterator(self, 'items')
+ def method_iterkeys_with_hash(self):
+ return SomeIterator(self, 'keys_with_hash')
+
def method_clear(self):
pass
@@ -467,6 +469,9 @@
self.dictdef.generalize_value(s_dfl)
return self.dictdef.read_value()
+ def method_contains_with_hash(self, s_key, s_hash):
+ return dict_contains(self, s_key)
+
@op.contains.register(SomeString)
@op.contains.register(SomeUnicodeString)
def contains_String(annotator, string, char):
diff --git a/rpython/rlib/objectmodel.py b/rpython/rlib/objectmodel.py
--- a/rpython/rlib/objectmodel.py
+++ b/rpython/rlib/objectmodel.py
@@ -790,53 +790,23 @@
def iterkeys_with_hash(d):
"""Iterates (key, hash) pairs without recomputing the hash."""
- assert not we_are_translated() # this code is only before translation
- for k in d:
- yield k, hash(k)
+ if not we_are_translated():
+ if isinstance(d, r_dict):
+ xxx
+ else:
+ return ((k, compute_hash(k)) for k in d)
+ return d.iterkeys_with_hash()
def contains_with_hash(d, key, h):
"""Same as 'key in d'. The extra argument is the hash. Use this only
if you got the hash just now from some other ..._with_hash() function."""
- assert not we_are_translated() # this code is only before translation
- assert hash(key) == h
- return key in d
-
-class Entry(ExtRegistryEntry):
- _about_ = iterkeys_with_hash
-
- def compute_result_annotation(self, s_d):
- from rpython.annotator.model import SomeDict, SomeIterator, s_None
- if isinstance(s_d, SomeDict):
- return SomeIterator(s_d, 'keys_with_hash')
- if s_None.contains(s_d):
- return None
- raise Exception("iterkeys_with_hash(x): x not a dict")
-
- def specialize_call(self, hop):
- from rpython.rtyper.lltypesystem.rdict import DictIteratorRepr
- hop.exception_cannot_occur()
- return DictIteratorRepr(hop.args_r[0], "keys_with_hash").newiter(hop)
-
-class Entry(ExtRegistryEntry):
- _about_ = contains_with_hash
-
- def compute_result_annotation(self, s_d, s_key, s_hash):
- from rpython.annotator.model import s_Bool, SomeDict, s_None
- from rpython.annotator.unaryop import dict_contains
- if isinstance(s_d, SomeDict):
- return dict_contains(s_d, s_key)
- if s_None.contains(s_d):
- return None
- raise Exception("contains_with_hash(x, ...): x not a dict")
-
- def specialize_call(self, hop):
- from rpython.rtyper.lltypesystem import lltype
- from rpython.rtyper.lltypesystem.rdict import ll_contains_with_hash
- r_dict = hop.args_r[0]
- v_dict, v_key, v_hash = hop.inputargs(r_dict, r_dict.key_repr,
- lltype.Signed)
- hop.exception_cannot_occur()
- return hop.gendirectcall(ll_contains_with_hash, v_dict, v_key, v_hash)
+ if not we_are_translated():
+ if isinstance(d, r_dict):
+ xxx
+ else:
+ assert compute_hash(key) == h
+ return key in d
+ return d.contains_with_hash(key, h)
# ____________________________________________________________
diff --git a/rpython/rtyper/lltypesystem/rdict.py b/rpython/rtyper/lltypesystem/rdict.py
--- a/rpython/rtyper/lltypesystem/rdict.py
+++ b/rpython/rtyper/lltypesystem/rdict.py
@@ -871,11 +871,6 @@
i = ll_dict_lookup(d, key, d.keyhash(key))
return not i & HIGHEST_BIT
-# for objectmodel.contains_with_hash()
-def ll_contains_with_hash(d, key, hash):
- i = ll_dict_lookup(d, key, hash)
- return not i & HIGHEST_BIT
-
POPITEMINDEX = lltype.Struct('PopItemIndex', ('nextindex', lltype.Signed))
global_popitem_index = lltype.malloc(POPITEMINDEX, zero=True, immortal=True)
diff --git a/rpython/rtyper/lltypesystem/rordereddict.py b/rpython/rtyper/lltypesystem/rordereddict.py
--- a/rpython/rtyper/lltypesystem/rordereddict.py
+++ b/rpython/rtyper/lltypesystem/rordereddict.py
@@ -335,6 +335,10 @@
hop.exception_cannot_occur()
return DictIteratorRepr(self, "items").newiter(hop)
+ def rtype_method_iterkeys_with_hash(self, hop):
+ hop.exception_cannot_occur()
+ return DictIteratorRepr(self, "keys_with_hash").newiter(hop)
+
def rtype_method_clear(self, hop):
v_dict, = hop.inputargs(self)
hop.exception_cannot_occur()
@@ -358,6 +362,13 @@
v_res = hop.gendirectcall(target, *v_args)
return self.recast_value(hop.llops, v_res)
+ def rtype_method_contains_with_hash(self, hop):
+ v_dict, v_key, v_hash = hop.inputargs(self, self.key_repr,
+ lltype.Signed)
+ hop.exception_is_here()
+ return hop.gendirectcall(ll_dict_contains_with_hash,
+ v_dict, v_key, v_hash)
+
class __extend__(pairtype(OrderedDictRepr, rmodel.Repr)):
def rtype_getitem((r_dict, r_key), hop):
@@ -1217,6 +1228,10 @@
i = d.lookup_function(d, key, d.keyhash(key), FLAG_LOOKUP)
return i >= 0
+def ll_dict_contains_with_hash(d, key, hash):
+ i = d.lookup_function(d, key, hash, FLAG_LOOKUP)
+ return i >= 0
+
def _ll_getnextitem(dic):
if dic.num_live_items == 0:
raise KeyError
More information about the pypy-commit
mailing list