| 1 |
from rpython.flowspace.model import Constant |
| 2 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rdict |
| 3 |
from rpython.rtyper.lltypesystem.llmemory import weakref_create, weakref_deref |
| 4 |
from rpython.rtyper import rclass |
| 5 |
from rpython.rtyper.rclass import getinstancerepr |
| 6 |
from rpython.rtyper.rmodel import Repr |
| 7 |
from rpython.rlib.rweakref import RWeakKeyDictionary |
| 8 |
from rpython.rlib import jit |
| 9 |
from rpython.rlib.objectmodel import compute_identity_hash |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class WeakKeyDictRepr(Repr): |
| 20 |
def __init__(self, rtyper): |
| 21 |
self.rtyper = rtyper |
| 22 |
self.lowleveltype = lltype.Ptr(WEAKDICT) |
| 23 |
self.dict_cache = {} |
| 24 |
|
| 25 |
def convert_const(self, weakdict): |
| 26 |
if not isinstance(weakdict, RWeakKeyDictionary): |
| 27 |
raise TyperError("expected an RWeakKeyDictionary: %r" % ( |
| 28 |
weakdict,)) |
| 29 |
try: |
| 30 |
key = Constant(weakdict) |
| 31 |
return self.dict_cache[key] |
| 32 |
except KeyError: |
| 33 |
self.setup() |
| 34 |
if weakdict.length() != 0: |
| 35 |
raise TyperError("got a non-empty prebuilt RWeakKeyDictionary") |
| 36 |
l_dict = ll_new_weakdict() |
| 37 |
self.dict_cache[key] = l_dict |
| 38 |
return l_dict |
| 39 |
|
| 40 |
def rtype_method_get(self, hop): |
| 41 |
r_object = getinstancerepr(self.rtyper, None) |
| 42 |
v_d, v_key = hop.inputargs(self, r_object) |
| 43 |
hop.exception_cannot_occur() |
| 44 |
v_result = hop.gendirectcall(ll_get, v_d, v_key) |
| 45 |
v_result = hop.genop("cast_pointer", [v_result], |
| 46 |
resulttype=hop.r_result.lowleveltype) |
| 47 |
return v_result |
| 48 |
|
| 49 |
def rtype_method_set(self, hop): |
| 50 |
r_object = getinstancerepr(self.rtyper, None) |
| 51 |
v_d, v_key, v_value = hop.inputargs(self, r_object, r_object) |
| 52 |
hop.exception_cannot_occur() |
| 53 |
if hop.args_s[2].is_constant() and hop.args_s[2].const is None: |
| 54 |
hop.gendirectcall(ll_set_null, v_d, v_key) |
| 55 |
else: |
| 56 |
hop.gendirectcall(ll_set, v_d, v_key, v_value) |
| 57 |
|
| 58 |
def rtype_method_length(self, hop): |
| 59 |
v_d, = hop.inputargs(self) |
| 60 |
hop.exception_cannot_occur() |
| 61 |
return hop.gendirectcall(ll_length, v_d) |
| 62 |
|
| 63 |
|
| 64 |
def specialize_make_weakdict(hop): |
| 65 |
hop.exception_cannot_occur() |
| 66 |
v_d = hop.gendirectcall(ll_new_weakdict) |
| 67 |
return v_d |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
NULLVALUE = lltype.nullptr(rclass.OBJECTPTR.TO) |
| 73 |
WEAKDICTENTRY = lltype.Struct("weakdictentry", |
| 74 |
("key", llmemory.WeakRefPtr), |
| 75 |
("value", rclass.OBJECTPTR), |
| 76 |
("f_hash", lltype.Signed)) |
| 77 |
|
| 78 |
def ll_debugrepr(x): |
| 79 |
if x: |
| 80 |
h = compute_identity_hash(x) |
| 81 |
else: |
| 82 |
h = 0 |
| 83 |
return '<%x>' % (h,) |
| 84 |
|
| 85 |
def ll_valid(entries, i): |
| 86 |
key = entries[i].key |
| 87 |
if not key: |
| 88 |
return False |
| 89 |
elif weakref_deref(rclass.OBJECTPTR, key): |
| 90 |
return True |
| 91 |
else: |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
entries[i].value = NULLVALUE |
| 96 |
return False |
| 97 |
|
| 98 |
def ll_everused(entries, i): |
| 99 |
return bool(entries[i].key) |
| 100 |
|
| 101 |
entrymeths = { |
| 102 |
'allocate': lltype.typeMethod(rdict._ll_malloc_entries), |
| 103 |
'delete': rdict._ll_free_entries, |
| 104 |
'valid': ll_valid, |
| 105 |
'everused': ll_everused, |
| 106 |
'hash': rdict.ll_hash_from_cache, |
| 107 |
'no_direct_compare': True, |
| 108 |
} |
| 109 |
WEAKDICTENTRYARRAY = lltype.GcArray(WEAKDICTENTRY, |
| 110 |
adtmeths=entrymeths, |
| 111 |
hints={'weakarray': 'key'}) |
| 112 |
|
| 113 |
|
| 114 |
@jit.dont_look_inside |
| 115 |
def ll_new_weakdict(): |
| 116 |
d = lltype.malloc(WEAKDICT) |
| 117 |
d.entries = WEAKDICT.entries.TO.allocate(rdict.DICT_INITSIZE) |
| 118 |
d.num_items = 0 |
| 119 |
d.resize_counter = rdict.DICT_INITSIZE * 2 |
| 120 |
return d |
| 121 |
|
| 122 |
@jit.dont_look_inside |
| 123 |
def ll_get(d, llkey): |
| 124 |
hash = compute_identity_hash(llkey) |
| 125 |
i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
return d.entries[i].value |
| 133 |
|
| 134 |
@jit.dont_look_inside |
| 135 |
def ll_set(d, llkey, llvalue): |
| 136 |
if llvalue: |
| 137 |
ll_set_nonnull(d, llkey, llvalue) |
| 138 |
else: |
| 139 |
ll_set_null(d, llkey) |
| 140 |
|
| 141 |
@jit.dont_look_inside |
| 142 |
def ll_set_nonnull(d, llkey, llvalue): |
| 143 |
hash = compute_identity_hash(llkey) |
| 144 |
keyref = weakref_create(llkey) |
| 145 |
i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK |
| 146 |
everused = d.entries.everused(i) |
| 147 |
d.entries[i].key = keyref |
| 148 |
d.entries[i].value = llvalue |
| 149 |
d.entries[i].f_hash = hash |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
if not everused: |
| 154 |
d.resize_counter -= 3 |
| 155 |
if d.resize_counter <= 0: |
| 156 |
|
| 157 |
ll_weakdict_resize(d) |
| 158 |
|
| 159 |
@jit.dont_look_inside |
| 160 |
def ll_set_null(d, llkey): |
| 161 |
hash = compute_identity_hash(llkey) |
| 162 |
i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK |
| 163 |
if d.entries.everused(i): |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
d.entries[i].key = llmemory.dead_wref |
| 168 |
d.entries[i].value = NULLVALUE |
| 169 |
|
| 170 |
|
| 171 |
def ll_update_num_items(d): |
| 172 |
entries = d.entries |
| 173 |
num_items = 0 |
| 174 |
for i in range(len(entries)): |
| 175 |
if entries.valid(i): |
| 176 |
num_items += 1 |
| 177 |
d.num_items = num_items |
| 178 |
|
| 179 |
def ll_weakdict_resize(d): |
| 180 |
|
| 181 |
ll_update_num_items(d) |
| 182 |
rdict.ll_dict_resize(d) |
| 183 |
|
| 184 |
def ll_keyeq(d, weakkey1, realkey2): |
| 185 |
|
| 186 |
|
| 187 |
if not weakkey1: |
| 188 |
assert bool(realkey2) |
| 189 |
return False |
| 190 |
return weakref_deref(rclass.OBJECTPTR, weakkey1) == realkey2 |
| 191 |
|
| 192 |
@jit.dont_look_inside |
| 193 |
def ll_length(d): |
| 194 |
|
| 195 |
ll_update_num_items(d) |
| 196 |
|
| 197 |
return d.num_items |
| 198 |
|
| 199 |
dictmeths = { |
| 200 |
'll_get': ll_get, |
| 201 |
'll_set': ll_set, |
| 202 |
'keyeq': ll_keyeq, |
| 203 |
'paranoia': False, |
| 204 |
} |
| 205 |
|
| 206 |
WEAKDICT = lltype.GcStruct("weakkeydict", |
| 207 |
("num_items", lltype.Signed), |
| 208 |
("resize_counter", lltype.Signed), |
| 209 |
("entries", lltype.Ptr(WEAKDICTENTRYARRAY)), |
| 210 |
adtmeths=dictmeths) |