Skip to content

Commit e52d523

Browse files
committed
HPy: native fast-path for immutable handles and native space
1 parent a4ff2b9 commit e52d523

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

graalpython/com.oracle.graal.python.jni/src/hpy_jni.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,21 @@ static int augment_Is(HPyContext *ctx, HPy a, HPy b) {
268268
if (bitsA == bitsB) {
269269
return 1;
270270
} else if (isBoxedHandle(bitsA) && isBoxedHandle(bitsB)) {
271-
return original_Is(ctx, a, b);
271+
// This code assumes that objects pointed by a handle <= SINGLETON_HANDLES_MAX
272+
// always get that same handle
273+
long unboxedA = unboxHandle(bitsA);
274+
long unboxedB = unboxHandle(bitsB);
275+
if (unboxedA <= SINGLETON_HANDLES_MAX) {
276+
return 0;
277+
} else if (unboxedB <= SINGLETON_HANDLES_MAX) {
278+
return 0;
279+
}
280+
// This code assumes that space[x] != NULL <=> objects pointed by x has native struct
281+
void** space = (void**)ctx->_private;
282+
if (space[unboxedA] == NULL && space[unboxedB] == NULL) {
283+
return original_Is(ctx, a, b);
284+
}
285+
return space[unboxedA] == space[unboxedB];
272286
} else {
273287
return 0;
274288
}

graalpython/com.oracle.graal.python.jni/src/hpy_jni.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
#define NAN_BOXING_MAX_HANDLE (0x000000007FFFFFFFllu)
6363
#define IMMUTABLE_HANDLES (0x0000000000000100llu)
6464

65+
// Some singleton Python objects are guaranteed to be always represented by
66+
// those handles, so that we do not have to upcall to unambiguously check if
67+
// a handle represents one of those
68+
#define SINGLETON_HANDLES_MAX (3)
69+
6570
#define isBoxedDouble(value) ((value) >= NAN_BOXING_BASE)
6671
#define isBoxedHandle(value) ((value) <= NAN_BOXING_MAX_HANDLE)
6772
#define isBoxedInt(value) (((value) & NAN_BOXING_MASK) == NAN_BOXING_INT)

0 commit comments

Comments
 (0)