43
43
import com .oracle .graal .python .lib .PyObjectRichCompareBool ;
44
44
import com .oracle .truffle .api .CompilerDirectives ;
45
45
46
+ /**
47
+ * Implements a Hamt following a similar internal structure to the one in cpython <a href=
48
+ * "https://github.com/python/cpython/blob/main/Python/hamt.c#L9-L251">https://github.com/python/cpython/blob/main/Python/hamt.c#L9-L251</a>
49
+ * There is a lot of room for optimization, but since most HAMTs which will occur in a python
50
+ * program are quite small, as they are used exclusively for contextvars, it is probably not worth
51
+ * spending extra effort on until we can benchmark sync web frameworks, and later async frameworks.
52
+ *
53
+ * It may make sense to use a sealed interface here eventually, rather than dispatching manually
54
+ * with instanceof.
55
+ *
56
+ * Care must be taken when using right shift (>>), as it sign-extends, It is generally needed to use
57
+ * bitmasking, unlike in the cpython implementation/original paper
58
+ */
59
+
46
60
public final class Hamt {
47
61
48
62
@ CompilerDirectives .TruffleBoundary
@@ -116,7 +130,7 @@ private static int bitmapToIdx(int bitmap, int position) {
116
130
}
117
131
118
132
private static BitmapPart bitmapPartsForPair (TreePart one , int hashOne , TreePart two , int hashTwo , int hashShift ) {
119
- assert hashOne != hashTwo : "cannot work with colliding nodes " ;
133
+ assert hashOne != hashTwo : "cannot work with colliding parts " ;
120
134
int oneIdx = hashIdx (hashOne , hashShift );
121
135
int twoIdx = hashIdx (hashTwo , hashShift );
122
136
if (oneIdx == twoIdx ) {
@@ -125,7 +139,7 @@ private static BitmapPart bitmapPartsForPair(TreePart one, int hashOne, TreePart
125
139
return new BitmapPart (oneIdx > twoIdx ? new TreePart []{one , two } : new TreePart []{two , one }, idxToBit (twoIdx ) | idxToBit (oneIdx ));
126
140
}
127
141
128
- private static TreePart nodeWithEntry (TreePart original , Entry newEntry , int hashShift ) {
142
+ private static TreePart partWithEntry (TreePart original , Entry newEntry , int hashShift ) {
129
143
assert hashShift <= 25 ;
130
144
if (original == null ) {
131
145
return newEntry ;
@@ -175,7 +189,7 @@ private static TreePart nodeWithEntry(TreePart original, Entry newEntry, int has
175
189
} else {
176
190
TreePart [] toReplaceIn = existing .elems .clone ();
177
191
TreePart toReplace = toReplaceIn [sparseIdx ];
178
- TreePart newPart = nodeWithEntry (toReplace , newEntry , hashShift + 5 );
192
+ TreePart newPart = partWithEntry (toReplace , newEntry , hashShift + 5 );
179
193
toReplaceIn [sparseIdx ] = newPart ;
180
194
return new BitmapPart (toReplaceIn , existing .bitmap );
181
195
}
@@ -185,7 +199,7 @@ private static TreePart nodeWithEntry(TreePart original, Entry newEntry, int has
185
199
int position = hashIdx (newEntry .hash , hashShift );
186
200
TreePart [] toReplaceIn = existing .elems .clone ();
187
201
TreePart toReplace = toReplaceIn [position ];
188
- TreePart newPart = nodeWithEntry (toReplace , newEntry , hashShift + 5 );
202
+ TreePart newPart = partWithEntry (toReplace , newEntry , hashShift + 5 );
189
203
toReplaceIn [position ] = newPart ;
190
204
return new ArrayPart (toReplaceIn );
191
205
}
@@ -205,7 +219,7 @@ private static TreePart nodeWithEntry(TreePart original, Entry newEntry, int has
205
219
}
206
220
207
221
public Hamt withEntry (Entry newEntry ) {
208
- TreePart root = nodeWithEntry (this .root , newEntry , 0 );
222
+ TreePart root = partWithEntry (this .root , newEntry , 0 );
209
223
return new Hamt (root );
210
224
}
211
225
@@ -320,7 +334,7 @@ private static TreePart partWithoutKey(TreePart root, Object key, int hash, int
320
334
return bitmapWithoutKey (existing , key , hash , hashShift );
321
335
}
322
336
if (root instanceof ArrayPart ) {
323
- ArrayPart existing = (ArrayPart ) root ;
337
+ ArrayPart existing = (ArrayPart ) root ;
324
338
int position = hashIdx (hash , hashShift );
325
339
TreePart replacement = partWithoutKey (existing .elems [position ], key , hash , hashShift + 5 );
326
340
if (replacement == null ) {
0 commit comments