Author: Armin Rigo <[email protected]>
Branch: rpython-hash
Changeset: r89822:7dccd4ef8d5d
Date: 2017-01-28 17:31 +0100
http://bitbucket.org/pypy/pypy/changeset/7dccd4ef8d5d/
Log: prebuilt RWeakValueDictionary can be non-empty. Force a rehashing
the first time we use them
diff --git a/rpython/rlib/_rweakvaldict.py b/rpython/rlib/_rweakvaldict.py
--- a/rpython/rlib/_rweakvaldict.py
+++ b/rpython/rlib/_rweakvaldict.py
@@ -76,12 +76,16 @@
bk = self.rtyper.annotator.bookkeeper
classdef = bk.getuniqueclassdef(weakdict._valueclass)
r_value = getinstancerepr(self.rtyper, classdef)
+ any_value = False
for dictkey, dictvalue in weakdict._dict.items():
llkey = self.r_key.convert_const(dictkey)
llvalue = r_value.convert_const(dictvalue)
if llvalue:
llvalue = lltype.cast_pointer(rclass.OBJECTPTR, llvalue)
self.ll_set_nonnull(l_dict, llkey, llvalue)
+ any_value = True
+ if any_value:
+ l_dict.resize_counter = -1
return l_dict
def rtype_method_get(self, hop):
@@ -114,6 +118,8 @@
@jit.dont_look_inside
def ll_get(self, d, llkey):
+ if d.resize_counter < 0:
+ self.ll_weakdict_resize(d) # initialize prebuilt dicts at runtime
hash = self.ll_keyhash(llkey)
i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK
#llop.debug_print(lltype.Void, i, 'get')
@@ -132,6 +138,8 @@
@jit.dont_look_inside
def ll_set_nonnull(self, d, llkey, llvalue):
+ if d.resize_counter < 0:
+ self.ll_weakdict_resize(d) # initialize prebuilt dicts at runtime
hash = self.ll_keyhash(llkey)
valueref = weakref_create(llvalue) # GC effects here, before the
rest
i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK
@@ -147,6 +155,8 @@
@jit.dont_look_inside
def ll_set_null(self, d, llkey):
+ if d.resize_counter < 0:
+ self.ll_weakdict_resize(d) # initialize prebuilt dicts at runtime
hash = self.ll_keyhash(llkey)
i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK
if d.entries.everused(i):
diff --git a/rpython/rlib/test/test_rweakvaldict.py
b/rpython/rlib/test/test_rweakvaldict.py
--- a/rpython/rlib/test/test_rweakvaldict.py
+++ b/rpython/rlib/test/test_rweakvaldict.py
@@ -3,6 +3,7 @@
from rpython.rlib import rgc
from rpython.rlib.rweakref import RWeakValueDictionary
from rpython.rtyper.test.test_llinterp import interpret
+from rpython.translator.c.test.test_genc import compile
class X(object):
pass
@@ -213,3 +214,18 @@
assert d.get(keys[3]) is None
f()
interpret(f, [])
+
+def test_translation_prebuilt():
+ class K:
+ pass
+ d = RWeakValueDictionary(K, X)
+ k1 = K(); k2 = K()
+ x1 = X(); x2 = X()
+ d.set(k1, x1)
+ d.set(k2, x2)
+ def f():
+ assert d.get(k1) is x1
+ assert d.get(k2) is x2
+ f()
+ fc = compile(f, [], gcpolicy="boehm", rweakref=True)
+ fc()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit