Skip to content

Commit c6bfe44

Browse files
committed
Merge branch 'ls/hpyPerf' into mq/hpy-update-100322
2 parents c1a170a + 45ffd7e commit c6bfe44

File tree

5 files changed

+286
-355
lines changed

5 files changed

+286
-355
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ static JNIEnv* jniEnv;
7474
UPCALL(FloatFromDouble, SIG_DOUBLE, SIG_HPY) \
7575
UPCALL(FloatAsDouble, SIG_HPY, SIG_DOUBLE) \
7676
UPCALL(LongAsLong, SIG_HPY, SIG_LONG) \
77+
UPCALL(LongAsDouble, SIG_HPY, SIG_DOUBLE) \
7778
UPCALL(LongFromLong, SIG_LONG, SIG_HPY) \
7879
UPCALL(Dup, SIG_HPY, SIG_HPY) \
7980
UPCALL(GetItemi, SIG_HPY SIG_SIZE_T, SIG_HPY) \
8081
UPCALL(SetItemi, SIG_HPY SIG_SIZE_T SIG_HPY, SIG_INT) \
8182
UPCALL(SetItem, SIG_HPY SIG_HPY SIG_HPY, SIG_INT) \
8283
UPCALL(NumberCheck, SIG_HPY, SIG_INT) \
84+
UPCALL(TypeCheck, SIG_HPY SIG_HPY, SIG_INT) \
8385
UPCALL(Length, SIG_HPY, SIG_SIZE_T) \
8486
UPCALL(ListCheck, SIG_HPY, SIG_INT) \
8587
UPCALL(UnicodeFromWideChar, SIG_PTR SIG_SIZE_T, SIG_HPY) \
@@ -127,6 +129,10 @@ static long ctx_LongAsLong_jni(HPyContext *ctx, HPy h) {
127129
return DO_UPCALL_LONG(CONTEXT_INSTANCE(ctx), LongAsLong, HPY_UP(h));
128130
}
129131

132+
static double ctx_LongAsDouble_jni(HPyContext *ctx, HPy h) {
133+
return DO_UPCALL_DOUBLE(CONTEXT_INSTANCE(ctx), LongAsDouble, HPY_UP(h));
134+
}
135+
130136
static HPy ctx_LongFromLong_jni(HPyContext *ctx, long l) {
131137
return DO_UPCALL_HPY(CONTEXT_INSTANCE(ctx), LongFromLong, l);
132138
}
@@ -159,6 +165,10 @@ static int ctx_NumberCheck_jni(HPyContext *ctx, HPy obj) {
159165
return DO_UPCALL_INT(CONTEXT_INSTANCE(ctx), NumberCheck, HPY_UP(obj));
160166
}
161167

168+
static int ctx_TypeCheck_jni(HPyContext *ctx, HPy obj, HPy type) {
169+
return DO_UPCALL_INT(CONTEXT_INSTANCE(ctx), TypeCheck, HPY_UP(obj), HPY_UP(type));
170+
}
171+
162172
static int ctx_ListCheck_jni(HPyContext *ctx, HPy obj) {
163173
return DO_UPCALL_INT(CONTEXT_INSTANCE(ctx), ListCheck, HPY_UP(obj));
164174
}
@@ -191,6 +201,7 @@ static HPy ctx_ListNew_jni(HPyContext *ctx, HPy_ssize_t len) {
191201
#define NAN_BOXING_INT (0x0001000000000000llu)
192202
#define NAN_BOXING_INT_MASK (0x00000000FFFFFFFFllu)
193203
#define NAN_BOXING_MAX_HANDLE (0x000000007FFFFFFFllu)
204+
#define IMMUTABLE_HANDLES (0x0000000000000100llu)
194205

195206
static bool isBoxedDouble(uint64_t value) {
196207
return value >= NAN_BOXING_BASE;
@@ -249,9 +260,11 @@ static HPy (*original_Dup)(HPyContext *ctx, HPy h);
249260
static HPy (*original_FloatFromDouble)(HPyContext *ctx, double v);
250261
static double (*original_FloatAsDouble)(HPyContext *ctx, HPy h);
251262
static long (*original_LongAsLong)(HPyContext *ctx, HPy h);
263+
static double (*original_LongAsDouble)(HPyContext *ctx, HPy h);
252264
static HPy (*original_LongFromLong)(HPyContext *ctx, long l);
253265
static int (*original_ListCheck)(HPyContext *ctx, HPy h);
254266
static int (*original_NumberCheck)(HPyContext *ctx, HPy h);
267+
static int (*original_TypeCheck)(HPyContext *ctx, HPy h, HPy type);
255268
static void (*original_Close)(HPyContext *ctx, HPy h);
256269
static HPy (*original_UnicodeFromWideChar)(HPyContext *ctx, const wchar_t *arr, HPy_ssize_t size);
257270

@@ -289,6 +302,15 @@ static long augment_LongAsLong(HPyContext *ctx, HPy h) {
289302
}
290303
}
291304

305+
static double augment_LongAsDouble(HPyContext *ctx, HPy h) {
306+
uint64_t bits = toBits(h);
307+
if (isBoxedInt(bits)) {
308+
return unboxInt(bits);
309+
} else {
310+
return original_LongAsDouble(ctx, h);
311+
}
312+
}
313+
292314
static HPy augment_LongFromLong(HPyContext *ctx, long l) {
293315
int32_t i = (int32_t) l;
294316
if (l == i) {
@@ -308,6 +330,9 @@ static void augment_Close(HPyContext *ctx, HPy h) {
308330
static HPy augment_Dup(HPyContext *ctx, HPy h) {
309331
uint64_t bits = toBits(h);
310332
if (isBoxedHandle(bits)) {
333+
if (bits < IMMUTABLE_HANDLES) {
334+
return h;
335+
}
311336
return original_Dup(ctx, h);
312337
} else {
313338
return h;
@@ -323,6 +348,26 @@ static int augment_NumberCheck(HPyContext *ctx, HPy obj) {
323348
}
324349
}
325350

351+
static int augment_TypeCheck(HPyContext *ctx, HPy obj, HPy type) {
352+
uint64_t bits = toBits(obj);
353+
if (isBoxedInt(bits)) {
354+
if (toBits(type) == toBits(ctx->h_LongType)) {
355+
return true;
356+
}
357+
if (toBits(type) == toBits(ctx->h_FloatType)) {
358+
return false;
359+
}
360+
} else if (isBoxedDouble(bits)) {
361+
if (toBits(type) == toBits(ctx->h_FloatType)) {
362+
return true;
363+
}
364+
if (toBits(type) == toBits(ctx->h_LongType)) {
365+
return false;
366+
}
367+
}
368+
return original_TypeCheck(ctx, obj, type);
369+
}
370+
326371
static int augment_ListCheck(HPyContext *ctx, HPy obj) {
327372
uint64_t bits = toBits(obj);
328373
if (isBoxedHandle(bits)) {
@@ -390,9 +435,12 @@ void initDirectFastPaths(HPyContext *context) {
390435

391436
original_FloatAsDouble = context->ctx_Float_AsDouble;
392437
context->ctx_Float_AsDouble = augment_FloatAsDouble;
393-
438+
394439
original_LongAsLong = context->ctx_Long_AsLong;
395440
context->ctx_Long_AsLong = augment_LongAsLong;
441+
442+
original_LongAsDouble = context->ctx_Long_AsDouble;
443+
context->ctx_Long_AsDouble = augment_LongAsDouble;
396444

397445
original_LongFromLong = context->ctx_Long_FromLong;
398446
context->ctx_Long_FromLong = augment_LongFromLong;
@@ -409,6 +457,9 @@ void initDirectFastPaths(HPyContext *context) {
409457
original_NumberCheck = context->ctx_Number_Check;
410458
context->ctx_Number_Check = augment_NumberCheck;
411459

460+
original_TypeCheck = context->ctx_TypeCheck;
461+
context->ctx_TypeCheck = augment_TypeCheck;
462+
412463
original_ListCheck = context->ctx_List_Check;
413464
context->ctx_List_Check = augment_ListCheck;
414465

@@ -437,11 +488,13 @@ JNIEXPORT jint JNICALL Java_com_oracle_graal_python_builtins_objects_cext_hpy_Gr
437488
context->ctx_Float_FromDouble = ctx_FloatFromDouble_jni;
438489
context->ctx_Float_AsDouble = ctx_FloatAsDouble_jni;
439490
context->ctx_Long_AsLong = ctx_LongAsLong_jni;
491+
context->ctx_Long_AsDouble = ctx_LongAsDouble_jni;
440492
context->ctx_AsStruct = ctx_AsStruct_jni;
441493
context->ctx_Close = ctx_Close_jni;
442494

443495
context->ctx_Dup = ctx_Dup_jni;
444496
context->ctx_Number_Check = ctx_NumberCheck_jni;
497+
context->ctx_TypeCheck = ctx_TypeCheck_jni;
445498
context->ctx_List_Check = ctx_ListCheck_jni;
446499

447500
context->ctx_Length = ctx_Length_jni;

0 commit comments

Comments
 (0)