Skip to content

Commit b56497a

Browse files
committed
[GR-38880] Performance improvements to hpy
PullRequest: graalpython/2272
2 parents a80e486 + 913a706 commit b56497a

File tree

2 files changed

+100
-55
lines changed
  • graalpython

2 files changed

+100
-55
lines changed

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

Lines changed: 95 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,21 @@ static uint64_t boxDouble(double value) {
214214

215215
static void *(*original_AsStruct)(HPyContext *ctx, HPy h);
216216
static HPy (*original_Dup)(HPyContext *ctx, HPy h);
217-
static HPy (*original_FloatFromDouble)(HPyContext *ctx, double v);
218-
static double (*original_FloatAsDouble)(HPyContext *ctx, HPy h);
219-
static long (*original_LongAsLong)(HPyContext *ctx, HPy h);
220-
static double (*original_LongAsDouble)(HPyContext *ctx, HPy h);
221-
static HPy (*original_LongFromLong)(HPyContext *ctx, long l);
222-
static int (*original_ListCheck)(HPyContext *ctx, HPy h);
223-
static int (*original_NumberCheck)(HPyContext *ctx, HPy h);
217+
static HPy (*original_Float_FromDouble)(HPyContext *ctx, double v);
218+
static double (*original_Float_AsDouble)(HPyContext *ctx, HPy h);
219+
static long (*original_Long_AsLong)(HPyContext *ctx, HPy h);
220+
static unsigned long (*original_Long_AsUnsignedLong)(HPyContext *ctx, HPy h);
221+
static double (*original_Long_AsDouble)(HPyContext *ctx, HPy h);
222+
static HPy (*original_Long_FromLong)(HPyContext *ctx, long l);
223+
static HPy (*original_Long_FromUnsignedLong)(HPyContext *ctx, unsigned long l);
224+
static HPy (*original_Long_FromLongLong)(HPyContext *ctx, long long l);
225+
static HPy (*original_Long_FromUnsignedLongLong)(HPyContext *ctx, unsigned long long l);
226+
static int (*original_List_Check)(HPyContext *ctx, HPy h);
227+
static int (*original_Number_Check)(HPyContext *ctx, HPy h);
224228
static int (*original_TypeCheck)(HPyContext *ctx, HPy h, HPy type);
225229
static void (*original_Close)(HPyContext *ctx, HPy h);
226-
static HPy (*original_UnicodeFromWideChar)(HPyContext *ctx, const wchar_t *arr, HPy_ssize_t size);
227-
static HPy (*original_TupleFromArray)(HPyContext *ctx, HPy *items, HPy_ssize_t nitems);
230+
static HPy (*original_Unicode_FromWideChar)(HPyContext *ctx, const wchar_t *arr, HPy_ssize_t size);
231+
static HPy (*original_Tuple_FromArray)(HPyContext *ctx, HPy *items, HPy_ssize_t nitems);
228232
static int (*original_Is)(HPyContext *ctx, HPy a, HPy b);
229233

230234
static int augment_Is(HPyContext *ctx, HPy a, HPy b) {
@@ -249,45 +253,81 @@ static void *augment_AsStruct(HPyContext *ctx, HPy h) {
249253
}
250254
}
251255

252-
static HPy augment_FloatFromDouble(HPyContext *ctx, double v) {
256+
static HPy augment_Float_FromDouble(HPyContext *ctx, double v) {
253257
return toPtr(boxDouble(v));
254258
}
255259

256-
static double augment_FloatAsDouble(HPyContext *ctx, HPy h) {
260+
static double augment_Float_AsDouble(HPyContext *ctx, HPy h) {
257261
uint64_t bits = toBits(h);
258262
if (isBoxedDouble(bits)) {
259263
return unboxDouble(bits);
260264
} else if (isBoxedInt(bits)) {
261265
return unboxInt(bits);
262266
} else {
263-
return original_FloatAsDouble(ctx, h);
267+
return original_Float_AsDouble(ctx, h);
264268
}
265269
}
266270

267-
static long augment_LongAsLong(HPyContext *ctx, HPy h) {
271+
static long augment_Long_AsLong(HPyContext *ctx, HPy h) {
268272
uint64_t bits = toBits(h);
269273
if (isBoxedInt(bits)) {
270274
return unboxInt(bits);
271275
} else {
272-
return original_LongAsLong(ctx, h);
276+
return original_Long_AsLong(ctx, h);
273277
}
274278
}
275279

276-
static double augment_LongAsDouble(HPyContext *ctx, HPy h) {
280+
static unsigned long augment_Long_AsUnsignedLong(HPyContext *ctx, HPy h) {
277281
uint64_t bits = toBits(h);
278282
if (isBoxedInt(bits)) {
279283
return unboxInt(bits);
280284
} else {
281-
return original_LongAsDouble(ctx, h);
285+
return original_Long_AsUnsignedLong(ctx, h);
282286
}
283287
}
284288

285-
static HPy augment_LongFromLong(HPyContext *ctx, long l) {
286-
int32_t i = (int32_t) l;
287-
if (l == i) {
289+
static double augment_Long_AsDouble(HPyContext *ctx, HPy h) {
290+
uint64_t bits = toBits(h);
291+
if (isBoxedInt(bits)) {
292+
return unboxInt(bits);
293+
} else {
294+
return original_Long_AsDouble(ctx, h);
295+
}
296+
}
297+
298+
static HPy augment_Long_FromLong(HPyContext *ctx, long l) {
299+
int32_t i = (int32_t) l;
300+
if (l == i) {
301+
return toPtr(boxInt(i));
302+
} else {
303+
return original_Long_FromLong(ctx, l);
304+
}
305+
}
306+
307+
static HPy augment_Long_FromUnsignedLong(HPyContext *ctx, unsigned long l) {
308+
int32_t i = (int32_t) l;
309+
if (l == i) {
310+
return toPtr(boxInt(i));
311+
} else {
312+
return original_Long_FromUnsignedLong(ctx, l);
313+
}
314+
}
315+
316+
static HPy augment_Long_FromLongLong(HPyContext *ctx, long long l) {
317+
int32_t i = (int32_t) l;
318+
if (l == i) {
319+
return toPtr(boxInt(i));
320+
} else {
321+
return original_Long_FromLongLong(ctx, l);
322+
}
323+
}
324+
325+
static HPy augment_Long_FromUnsignedLongLong(HPyContext *ctx, unsigned long long l) {
326+
int32_t i = (int32_t) l;
327+
if (l == i) {
288328
return toPtr(boxInt(i));
289329
} else {
290-
return original_LongFromLong(ctx, l);
330+
return original_Long_FromUnsignedLongLong(ctx, l);
291331
}
292332
}
293333

@@ -325,12 +365,12 @@ static HPy augment_Dup(HPyContext *ctx, HPy h) {
325365
}
326366
}
327367

328-
static int augment_NumberCheck(HPyContext *ctx, HPy obj) {
368+
static int augment_Number_Check(HPyContext *ctx, HPy obj) {
329369
uint64_t bits = toBits(obj);
330370
if (isBoxedDouble(bits) || isBoxedInt(bits)) {
331371
return true;
332372
} else {
333-
return original_NumberCheck(ctx, obj);
373+
return original_Number_Check(ctx, obj);
334374
}
335375
}
336376

@@ -354,18 +394,18 @@ static int augment_TypeCheck(HPyContext *ctx, HPy obj, HPy type) {
354394
return original_TypeCheck(ctx, obj, type);
355395
}
356396

357-
static int augment_ListCheck(HPyContext *ctx, HPy obj) {
397+
static int augment_List_Check(HPyContext *ctx, HPy obj) {
358398
uint64_t bits = toBits(obj);
359399
if (isBoxedHandle(bits)) {
360-
return original_ListCheck(ctx, obj);
400+
return original_List_Check(ctx, obj);
361401
} else {
362402
return false;
363403
}
364404
}
365405

366406
#define MAX_UNICODE 0x10ffff
367407

368-
static HPy augment_UnicodeFromWideChar(HPyContext *ctx, const wchar_t *u, HPy_ssize_t size) {
408+
static HPy augment_Unicode_FromWideChar(HPyContext *ctx, const wchar_t *u, HPy_ssize_t size) {
369409
if (u == NULL && size != 0) {
370410
return HPy_NULL;
371411
}
@@ -408,7 +448,7 @@ static HPy augment_UnicodeFromWideChar(HPyContext *ctx, const wchar_t *u, HPy_ss
408448
(*jniEnv)->ReleasePrimitiveArrayCritical(jniEnv, jCharArray, content, 0);
409449
return DO_UPCALL_HPY(CONTEXT_INSTANCE(ctx), UnicodeFromJCharArray, jCharArray);
410450
} else {
411-
return original_UnicodeFromWideChar(ctx, u, size);
451+
return original_Unicode_FromWideChar(ctx, u, size);
412452
}
413453
}
414454

@@ -418,7 +458,7 @@ _HPy_HIDDEN HPy upcallTupleFromArray(HPyContext *ctx, HPy *items, HPy_ssize_t ni
418458
return DO_UPCALL_HPY(CONTEXT_INSTANCE(ctx), TupleFromArray, jLongArray, steal);
419459
}
420460

421-
static HPy augment_TupleFromArray(HPyContext *ctx, HPy *items, HPy_ssize_t nitems) {
461+
static HPy augment_Tuple_FromArray(HPyContext *ctx, HPy *items, HPy_ssize_t nitems) {
422462
return upcallTupleFromArray(ctx, items, nitems, JNI_FALSE);
423463
}
424464

@@ -430,49 +470,49 @@ void initDirectFastPaths(HPyContext *context) {
430470
LOG("%p", context);
431471
context->name = "HPy Universal ABI (GraalVM backend, JNI)";
432472

433-
original_FloatFromDouble = context->ctx_Float_FromDouble;
434-
context->ctx_Float_FromDouble = augment_FloatFromDouble;
473+
#define AUGMENT(name) \
474+
original_ ## name = context->ctx_ ## name; \
475+
context->ctx_ ## name = augment_ ## name;
476+
477+
AUGMENT(Float_FromDouble);
478+
479+
AUGMENT(Float_AsDouble);
480+
481+
AUGMENT(Long_AsLong);
435482

436-
original_FloatAsDouble = context->ctx_Float_AsDouble;
437-
context->ctx_Float_AsDouble = augment_FloatAsDouble;
483+
AUGMENT(Long_AsUnsignedLong);
438484

439-
original_LongAsLong = context->ctx_Long_AsLong;
440-
context->ctx_Long_AsLong = augment_LongAsLong;
485+
AUGMENT(Long_AsDouble);
441486

442-
original_LongAsDouble = context->ctx_Long_AsDouble;
443-
context->ctx_Long_AsDouble = augment_LongAsDouble;
487+
AUGMENT(Long_FromLong);
444488

445-
original_LongFromLong = context->ctx_Long_FromLong;
446-
context->ctx_Long_FromLong = augment_LongFromLong;
489+
AUGMENT(Long_FromUnsignedLong);
447490

448-
original_Close = context->ctx_Close;
449-
context->ctx_Close = augment_Close;
491+
AUGMENT(Long_FromLongLong);
450492

451-
original_AsStruct = context->ctx_AsStruct;
452-
context->ctx_AsStruct = augment_AsStruct;
493+
AUGMENT(Long_FromUnsignedLongLong);
494+
495+
AUGMENT(Close);
496+
497+
AUGMENT(AsStruct);
453498

454499
context->ctx_AsStructLegacy = augment_AsStruct;
455500

456-
original_Dup = context->ctx_Dup;
457-
context->ctx_Dup = augment_Dup;
501+
AUGMENT(Dup);
502+
503+
AUGMENT(Number_Check);
458504

459-
original_NumberCheck = context->ctx_Number_Check;
460-
context->ctx_Number_Check = augment_NumberCheck;
505+
AUGMENT(TypeCheck);
461506

462-
original_TypeCheck = context->ctx_TypeCheck;
463-
context->ctx_TypeCheck = augment_TypeCheck;
507+
AUGMENT(List_Check);
464508

465-
original_ListCheck = context->ctx_List_Check;
466-
context->ctx_List_Check = augment_ListCheck;
509+
AUGMENT(Unicode_FromWideChar);
467510

468-
original_UnicodeFromWideChar = context->ctx_Unicode_FromWideChar;
469-
context->ctx_Unicode_FromWideChar = augment_UnicodeFromWideChar;
511+
AUGMENT(Tuple_FromArray);
470512

471-
original_TupleFromArray = context->ctx_Tuple_FromArray;
472-
context->ctx_Tuple_FromArray = augment_TupleFromArray;
513+
AUGMENT(Is);
473514

474-
original_Is = context->ctx_Is;
475-
context->ctx_Is = augment_Is;
515+
#undef AUGMENT
476516
}
477517

478518
void setHPyContextNativeSpace(HPyContext *context, void** nativeSpace) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,11 @@ public final long ctxGetItemi(long hCollection, long lidx) {
16211621
return GraalHPyBoxing.boxHandle(createHandle(lresult).getId(this, ConditionProfile.getUncached()));
16221622
} else if (storage instanceof ObjectSequenceStorage) {
16231623
Object result = ((ObjectSequenceStorage) storage).getItemNormalized(idx);
1624+
if (result instanceof Integer) {
1625+
return GraalHPyBoxing.boxInt((int) result);
1626+
} else if (result instanceof Double) {
1627+
return GraalHPyBoxing.boxDouble((double) result);
1628+
}
16241629
return GraalHPyBoxing.boxHandle(createHandle(result).getId(this, ConditionProfile.getUncached()));
16251630
}
16261631
// TODO: other storages...

0 commit comments

Comments
 (0)