Skip to content

Commit 0b9af23

Browse files
committed
avoid tracking unboxed+immutable handles, bulk close trackers
1 parent 4b7b875 commit 0b9af23

File tree

3 files changed

+46
-58
lines changed

3 files changed

+46
-58
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
* return HPy_NULL;
8282
*/
8383

84-
#include "hpy.h"
84+
#include "hpy_jni.h"
8585

8686
static const HPy_ssize_t HPYTRACKER_INITIAL_CAPACITY = 5;
8787

@@ -155,13 +155,20 @@ tracker_resize(HPyContext *ctx, _HPyTracker_s *hp, HPy_ssize_t capacity)
155155
_HPy_HIDDEN int
156156
ctx_Tracker_Add(HPyContext *ctx, HPyTracker ht, HPy h)
157157
{
158-
_HPyTracker_s *hp = _ht2hp(ht);
159-
hp->handles[hp->length++] = h;
160-
if (hp->capacity <= hp->length) {
161-
if (tracker_resize(ctx, hp, hp->capacity * 2 - 1) < 0)
162-
return -1;
158+
uint64_t bits = toBits(h);
159+
if (!isBoxedHandle(bits)) {
160+
return 0;
161+
} else if (bits < IMMUTABLE_HANDLES) {
162+
return 0;
163+
} else {
164+
_HPyTracker_s *hp = _ht2hp(ht);
165+
hp->handles[hp->length++] = h;
166+
if (hp->capacity <= hp->length) {
167+
if (tracker_resize(ctx, hp, hp->capacity * 2 - 1) < 0)
168+
return -1;
169+
}
170+
return 0;
163171
}
164-
return 0;
165172
}
166173

167174
_HPy_HIDDEN void
@@ -176,9 +183,7 @@ ctx_Tracker_Close(HPyContext *ctx, HPyTracker ht)
176183
{
177184
_HPyTracker_s *hp = _ht2hp(ht);
178185
HPy_ssize_t i;
179-
for (i=0; i<hp->length; i++) {
180-
HPy_Close(ctx, hp->handles[i]);
181-
}
186+
upcallBulkClose(ctx, hp->handles, hp->length);
182187
free(hp->handles);
183188
free(hp);
184189
}

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

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,6 @@ static HPy ctx_ListNew_jni(HPyContext *ctx, HPy_ssize_t len) {
198198
//*************************
199199
// BOXING
200200

201-
#define NAN_BOXING_BASE (0x0007000000000000llu)
202-
#define NAN_BOXING_MASK (0xFFFF000000000000llu)
203-
#define NAN_BOXING_INT (0x0001000000000000llu)
204-
#define NAN_BOXING_INT_MASK (0x00000000FFFFFFFFllu)
205-
#define NAN_BOXING_MAX_HANDLE (0x000000007FFFFFFFllu)
206-
#define IMMUTABLE_HANDLES (0x0000000000000100llu)
207-
208-
static bool isBoxedDouble(uint64_t value) {
209-
return value >= NAN_BOXING_BASE;
210-
}
211-
212-
static bool isBoxedHandle(uint64_t value) {
213-
return value <= NAN_BOXING_MAX_HANDLE;
214-
}
215-
216-
static bool isBoxedInt(uint64_t value) {
217-
return (value & NAN_BOXING_MASK) == NAN_BOXING_INT;
218-
}
219-
220201
static double unboxDouble(uint64_t value) {
221202
uint64_t doubleBits = value - NAN_BOXING_BASE;
222203
return * ((double*) &doubleBits);
@@ -228,32 +209,6 @@ static uint64_t boxDouble(double value) {
228209
return doubleBits + NAN_BOXING_BASE;
229210
}
230211

231-
static uint64_t unboxHandle(uint64_t value) {
232-
return value;
233-
}
234-
235-
static uint64_t boxHandle(uint64_t handle) {
236-
return handle;
237-
}
238-
239-
static int32_t unboxInt(uint64_t value) {
240-
return (int32_t) (value - NAN_BOXING_INT);
241-
}
242-
243-
static uint64_t boxInt(int32_t value) {
244-
return (value & NAN_BOXING_INT_MASK) + NAN_BOXING_INT;
245-
}
246-
247-
static inline uint64_t toBits(HPy ptr) {
248-
/* return * ((uint64_t*) &ptr._i); */
249-
return (uint64_t) (ptr._i);
250-
}
251-
252-
static inline HPy toPtr(uint64_t ptr) {
253-
/* return * ((void**) &ptr); */
254-
return (HPy) { (HPy_ssize_t) ptr };
255-
}
256-
257212
//*************************
258213
// direct fast paths that handle certain calls on the native side:
259214

@@ -325,7 +280,7 @@ static HPy augment_LongFromLong(HPyContext *ctx, long l) {
325280

326281
#define MAX_UNCLOSED_HANDLES 32
327282
static int32_t unclosedHandleTop = 0;
328-
static uint64_t unclosedHandles[MAX_UNCLOSED_HANDLES] = { 0 };
283+
static HPy unclosedHandles[MAX_UNCLOSED_HANDLES];
329284

330285
static void augment_Close(HPyContext *ctx, HPy h) {
331286
uint64_t bits = toBits(h);
@@ -336,9 +291,9 @@ static void augment_Close(HPyContext *ctx, HPy h) {
336291
return;
337292
}
338293
if (unclosedHandleTop < MAX_UNCLOSED_HANDLES) {
339-
unclosedHandles[unclosedHandleTop++] = bits;
294+
unclosedHandles[unclosedHandleTop++] = h;
340295
} else {
341-
DO_UPCALL_VOID(CONTEXT_INSTANCE(ctx), BulkClose, unclosedHandles, unclosedHandleTop);
296+
upcallBulkClose(ctx, unclosedHandles, unclosedHandleTop);
342297
memset(unclosedHandles, 0, sizeof(uint64_t) * unclosedHandleTop);
343298
unclosedHandleTop = 0;
344299
}
@@ -454,6 +409,9 @@ static HPy augment_TupleFromArray(HPyContext *ctx, HPy *items, HPy_ssize_t nitem
454409
return upcallTupleFromArray(ctx, items, nitems, JNI_FALSE);
455410
}
456411

412+
_HPy_HIDDEN void upcallBulkClose(HPyContext *ctx, HPy *items, HPy_ssize_t nitems) {
413+
DO_UPCALL_VOID(CONTEXT_INSTANCE(ctx), BulkClose, items, nitems);
414+
}
457415

458416
void initDirectFastPaths(HPyContext *context) {
459417
LOG("%p", context);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,29 @@
4848
#include <hpy.h>
4949
#include <jni.h>
5050

51+
//*************************
52+
// BOXING
53+
54+
#define NAN_BOXING_BASE (0x0007000000000000llu)
55+
#define NAN_BOXING_MASK (0xFFFF000000000000llu)
56+
#define NAN_BOXING_INT (0x0001000000000000llu)
57+
#define NAN_BOXING_INT_MASK (0x00000000FFFFFFFFllu)
58+
#define NAN_BOXING_MAX_HANDLE (0x000000007FFFFFFFllu)
59+
#define IMMUTABLE_HANDLES (0x0000000000000100llu)
60+
61+
#define isBoxedDouble(value) ((value) >= NAN_BOXING_BASE)
62+
#define isBoxedHandle(value) ((value) <= NAN_BOXING_MAX_HANDLE)
63+
#define isBoxedInt(value) (((value) & NAN_BOXING_MASK) == NAN_BOXING_INT)
64+
65+
#define unboxHandle(value) (value)
66+
#define boxHandle(handle) (handle)
67+
68+
#define unboxInt(value) ((int32_t) ((value) - NAN_BOXING_INT))
69+
#define boxInt(value) ((((uint64_t) (value)) & NAN_BOXING_INT_MASK) + NAN_BOXING_INT)
70+
71+
#define toBits(ptr) ((uint64_t) ((ptr)._i))
72+
#define toPtr(ptr) ((HPy) { (HPy_ssize_t) (ptr) })
73+
5174
_HPy_HIDDEN HPy upcallTupleFromArray(HPyContext *ctx, HPy *items, HPy_ssize_t nitems, jboolean steal);
75+
76+
_HPy_HIDDEN void upcallBulkClose(HPyContext *ctx, HPy *items, HPy_ssize_t nitems);

0 commit comments

Comments
 (0)