Debian Codegraph @

rpython/rlib/_rweakvaldict.py

raw symbols SCIP
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.error import TyperError
6 from rpython.rtyper.rclass import getinstancerepr
7 from rpython.rtyper.rmodel import Repr
8 from rpython.rlib.rweakref import RWeakValueDictionary
9 from rpython.rlib import jit
10
11
12 class WeakValueDictRepr(Repr):
13 def __init__(self, rtyper, r_key):
14 self.rtyper = rtyper
15 self.r_key = r_key
16
17 fasthashfn = r_key.get_ll_fasthash_function()
18 self.ll_keyhash = r_key.get_ll_hash_function()
19 ll_keyeq = lltype.staticAdtMethod(r_key.get_ll_eq_function())
20
21 def ll_valid(entries, i):
22 value = entries[i].value
23 return bool(value) and bool(weakref_deref(rclass.OBJECTPTR, value))
24
25 def ll_everused(entries, i):
26 return bool(entries[i].value)
27
28 def ll_hash(entries, i):
29 return fasthashfn(entries[i].key)
30
31 entrymeths = {
32 'allocate': lltype.typeMethod(rdict._ll_malloc_entries),
33 'delete': rdict._ll_free_entries,
34 'valid': ll_valid,
35 'everused': ll_everused,
36 'hash': ll_hash,
37 }
38 WEAKDICTENTRY = lltype.Struct("weakdictentry",
39 ("key", r_key.lowleveltype),
40 ("value", llmemory.WeakRefPtr))
41 WEAKDICTENTRYARRAY = lltype.GcArray(WEAKDICTENTRY,
42 adtmeths=entrymeths,
43 hints={'weakarray': 'value'})
44 # NB. the 'hints' is not used so far ^^^
45
46 dictmeths = {
47 'll_get': self.ll_get,
48 'll_set': self.ll_set,
49 'keyeq': ll_keyeq,
50 'paranoia': False,
51 }
52
53 self.WEAKDICT = lltype.GcStruct(
54 "weakvaldict",
55 ("num_items", lltype.Signed),
56 ("resize_counter", lltype.Signed),
57 ("entries", lltype.Ptr(WEAKDICTENTRYARRAY)),
58 adtmeths=dictmeths)
59
60 self.lowleveltype = lltype.Ptr(self.WEAKDICT)
61 self.dict_cache = {}
62
63 def convert_const(self, weakdict):
64 if weakdict is None:
65 return lltype.nullptr(self.WEAKDICT)
66 if not isinstance(weakdict, RWeakValueDictionary):
67 raise TyperError("expected an RWeakValueDictionary: %r" % (
68 weakdict,))
69 try:
70 key = Constant(weakdict)
71 return self.dict_cache[key]
72 except KeyError:
73 self.setup()
74 l_dict = self.ll_new_weakdict()
75 self.dict_cache[key] = l_dict
76 bk = self.rtyper.annotator.bookkeeper
77 classdef = bk.getuniqueclassdef(weakdict._valueclass)
78 r_value = getinstancerepr(self.rtyper, classdef)
79 any_value = False
80 for dictkey, dictvalue in weakdict._dict.items():
81 llkey = self.r_key.convert_const(dictkey)
82 llvalue = r_value.convert_const(dictvalue)
83 if llvalue:
84 llvalue = lltype.cast_pointer(rclass.OBJECTPTR, llvalue)
85 self.ll_set_nonnull(l_dict, llkey, llvalue)
86 any_value = True
87 if any_value:
88 l_dict.resize_counter = -1
89 return l_dict
90
91 def rtype_method_get(self, hop):
92 v_d, v_key = hop.inputargs(self, self.r_key)
93 hop.exception_cannot_occur()
94 v_result = hop.gendirectcall(self.ll_get, v_d, v_key)
95 v_result = hop.genop("cast_pointer", [v_result],
96 resulttype=hop.r_result.lowleveltype)
97 return v_result
98
99 def rtype_method_set(self, hop):
100 r_object = getinstancerepr(self.rtyper, None)
101 v_d, v_key, v_value = hop.inputargs(self, self.r_key, r_object)
102 hop.exception_cannot_occur()
103 if hop.args_s[2].is_constant() and hop.args_s[2].const is None:
104 hop.gendirectcall(self.ll_set_null, v_d, v_key)
105 else:
106 hop.gendirectcall(self.ll_set, v_d, v_key, v_value)
107
108
109 # ____________________________________________________________
110
111 @jit.dont_look_inside
112 def ll_new_weakdict(self):
113 d = lltype.malloc(self.WEAKDICT)
114 d.entries = self.WEAKDICT.entries.TO.allocate(rdict.DICT_INITSIZE)
115 d.num_items = 0
116 d.resize_counter = rdict.DICT_INITSIZE * 2
117 return d
118
119 @jit.dont_look_inside
120 def ll_get(self, d, llkey):
121 if d.resize_counter < 0:
122 self.ll_weakdict_rehash_after_translation(d)
123 hash = self.ll_keyhash(llkey)
124 i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK
125 #llop.debug_print(lltype.Void, i, 'get')
126 valueref = d.entries[i].value
127 if valueref:
128 return weakref_deref(rclass.OBJECTPTR, valueref)
129 else:
130 return lltype.nullptr(rclass.OBJECTPTR.TO)
131
132 @jit.dont_look_inside
133 def ll_set(self, d, llkey, llvalue):
134 if llvalue:
135 self.ll_set_nonnull(d, llkey, llvalue)
136 else:
137 self.ll_set_null(d, llkey)
138
139 @jit.dont_look_inside
140 def ll_set_nonnull(self, d, llkey, llvalue):
141 if d.resize_counter < 0:
142 self.ll_weakdict_rehash_after_translation(d)
143 hash = self.ll_keyhash(llkey)
144 valueref = weakref_create(llvalue) # GC effects here, before the rest
145 i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK
146 everused = d.entries.everused(i)
147 d.entries[i].key = llkey
148 d.entries[i].value = valueref
149 #llop.debug_print(lltype.Void, i, 'stored')
150 if not everused:
151 d.resize_counter -= 3
152 if d.resize_counter <= 0:
153 #llop.debug_print(lltype.Void, 'RESIZE')
154 self.ll_weakdict_resize(d)
155
156 @jit.dont_look_inside
157 def ll_set_null(self, d, llkey):
158 if d.resize_counter < 0:
159 self.ll_weakdict_rehash_after_translation(d)
160 hash = self.ll_keyhash(llkey)
161 i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK
162 if d.entries.everused(i):
163 # If the entry was ever used, clean up its key and value.
164 # We don't store a NULL value, but a dead weakref, because
165 # the entry must still be marked as everused().
166 d.entries[i].value = llmemory.dead_wref
167 if isinstance(self.r_key.lowleveltype, lltype.Ptr):
168 d.entries[i].key = self.r_key.convert_const(None)
169 else:
170 d.entries[i].key = self.r_key.convert_const(0)
171 #llop.debug_print(lltype.Void, i, 'zero')
172
173 def ll_weakdict_resize(self, d):
174 # first set num_items to its correct, up-to-date value
175 entries = d.entries
176 num_items = 0
177 for i in range(len(entries)):
178 if entries.valid(i):
179 num_items += 1
180 d.num_items = num_items
181 rdict.ll_dict_resize(d)
182
183 def ll_weakdict_rehash_after_translation(self, d):
184 # recompute all hashes. See comment in rordereddict.py,
185 # ll_dict_rehash_after_translation().
186 entries = d.entries
187 for i in range(len(entries)):
188 self.ll_keyhash(entries[i].key)
189 self.ll_weakdict_resize(d)
190 assert d.resize_counter >= 0
191
192 def specialize_make_weakdict(hop):
193 hop.exception_cannot_occur()
194 v_d = hop.gendirectcall(hop.r_result.ll_new_weakdict)
195 return v_d
196