Skip to content

Commit d8a890f

Browse files
author
Adam Hrbac
committed
Add a comment
1 parent 798f3c2 commit d8a890f

File tree

1 file changed

+20
-6
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars

1 file changed

+20
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/Hamt.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343
import com.oracle.graal.python.lib.PyObjectRichCompareBool;
4444
import com.oracle.truffle.api.CompilerDirectives;
4545

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+
4660
public final class Hamt {
4761

4862
@CompilerDirectives.TruffleBoundary
@@ -116,7 +130,7 @@ private static int bitmapToIdx(int bitmap, int position) {
116130
}
117131

118132
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";
120134
int oneIdx = hashIdx(hashOne, hashShift);
121135
int twoIdx = hashIdx(hashTwo, hashShift);
122136
if (oneIdx == twoIdx) {
@@ -125,7 +139,7 @@ private static BitmapPart bitmapPartsForPair(TreePart one, int hashOne, TreePart
125139
return new BitmapPart(oneIdx > twoIdx ? new TreePart[]{one, two} : new TreePart[]{two, one}, idxToBit(twoIdx) | idxToBit(oneIdx));
126140
}
127141

128-
private static TreePart nodeWithEntry(TreePart original, Entry newEntry, int hashShift) {
142+
private static TreePart partWithEntry(TreePart original, Entry newEntry, int hashShift) {
129143
assert hashShift <= 25;
130144
if (original == null) {
131145
return newEntry;
@@ -175,7 +189,7 @@ private static TreePart nodeWithEntry(TreePart original, Entry newEntry, int has
175189
} else {
176190
TreePart[] toReplaceIn = existing.elems.clone();
177191
TreePart toReplace = toReplaceIn[sparseIdx];
178-
TreePart newPart = nodeWithEntry(toReplace, newEntry, hashShift + 5);
192+
TreePart newPart = partWithEntry(toReplace, newEntry, hashShift + 5);
179193
toReplaceIn[sparseIdx] = newPart;
180194
return new BitmapPart(toReplaceIn, existing.bitmap);
181195
}
@@ -185,7 +199,7 @@ private static TreePart nodeWithEntry(TreePart original, Entry newEntry, int has
185199
int position = hashIdx(newEntry.hash, hashShift);
186200
TreePart[] toReplaceIn = existing.elems.clone();
187201
TreePart toReplace = toReplaceIn[position];
188-
TreePart newPart = nodeWithEntry(toReplace, newEntry, hashShift + 5);
202+
TreePart newPart = partWithEntry(toReplace, newEntry, hashShift + 5);
189203
toReplaceIn[position] = newPart;
190204
return new ArrayPart(toReplaceIn);
191205
}
@@ -205,7 +219,7 @@ private static TreePart nodeWithEntry(TreePart original, Entry newEntry, int has
205219
}
206220

207221
public Hamt withEntry(Entry newEntry) {
208-
TreePart root = nodeWithEntry(this.root, newEntry, 0);
222+
TreePart root = partWithEntry(this.root, newEntry, 0);
209223
return new Hamt(root);
210224
}
211225

@@ -320,7 +334,7 @@ private static TreePart partWithoutKey(TreePart root, Object key, int hash, int
320334
return bitmapWithoutKey(existing, key, hash, hashShift);
321335
}
322336
if (root instanceof ArrayPart) {
323-
ArrayPart existing = (ArrayPart) root;
337+
ArrayPart existing = (ArrayPart) root;
324338
int position = hashIdx(hash, hashShift);
325339
TreePart replacement = partWithoutKey(existing.elems[position], key, hash, hashShift + 5);
326340
if (replacement == null) {

0 commit comments

Comments
 (0)