Skip to content

Commit 31bef62

Browse files
committed
Cache 'python_cext' functions directly.
1 parent e9ab5c4 commit 31bef62

File tree

13 files changed

+118
-50
lines changed

13 files changed

+118
-50
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,8 @@ PyObject* WriteULongMember(PyObject* object, Py_ssize_t offset, PyObject* value)
461461
return value;
462462
}
463463

464-
UPCALL_ID(__bool__);
465464
PyObject* WriteBoolMember(PyObject* object, Py_ssize_t offset, PyObject* value) {
466-
WriteMember(object, offset, UPCALL_O(native_to_java(value), _jls___bool__) == Py_True ? (char)1 : (char)0, char);
465+
WriteMember(object, offset, UPCALL_O(native_to_java(value), polyglot_from_string("__bool__", SRC_CS)) == Py_True ? (char)1 : (char)0, char);
467466
return value;
468467
}
469468

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ typedef struct {
7575
PyAPI_DATA(PyTypeObject) PyBuffer_Type;
7676
PyAPI_DATA(PyTypeObject) _PyExc_BaseException;
7777

78+
typedef void (*init_upcall)();
79+
7880
extern void *PY_TRUFFLE_CEXT;
7981
extern void *PY_BUILTIN;
8082
extern void *Py_NoValue;
83+
extern init_upcall upcalls[];
84+
extern unsigned init_upcall_n;
8185

8286
/* upcall functions for calling into Python */
8387
extern PyObject*(*PY_TRUFFLE_LANDING)(void *rcv, void* name, ...);
@@ -89,13 +93,6 @@ extern uint64_t (*PY_TRUFFLE_CEXT_LANDING_L)(void* name, ...);
8993
extern double (*PY_TRUFFLE_CEXT_LANDING_D)(void* name, ...);
9094
extern void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
9195

92-
#define UPCALL_ID(name) \
93-
static void* _jls_ ## name; \
94-
__attribute__((constructor)) \
95-
static void init_jls_ ## name(void) { \
96-
_jls_ ## name = polyglot_from_string(#name, SRC_CS); \
97-
}
98-
9996
/* Call function with return type 'PyObject *'; does polyglot cast and error handling */
10097
#define UPCALL_O(__recv__, __name__, ...) PY_TRUFFLE_LANDING((__recv__), __name__, ##__VA_ARGS__)
10198

@@ -138,6 +135,13 @@ extern void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
138135
/* Call function of 'python_cext' module with return type 'double'; no polyglot cast but error handling */
139136
#define UPCALL_CEXT_D(__name__, ...) (PY_TRUFFLE_CEXT_LANDING_D(__name__, ##__VA_ARGS__))
140137

138+
#define UPCALL_ID(name) \
139+
static void* _jls_ ## name; \
140+
__attribute__((constructor)) \
141+
static void init_upcall_ ## name(void) { \
142+
_jls_ ## name = polyglot_get_member(PY_TRUFFLE_CEXT, polyglot_from_string(#name, SRC_CS)); \
143+
}
144+
141145
#define as_char_pointer(obj) ((const char*)UPCALL_CEXT_PTR(polyglot_from_string("to_char_pointer", "ascii"), native_to_java(obj)))
142146
#define as_long(obj) ((long)polyglot_as_i64(polyglot_invoke(PY_TRUFFLE_CEXT, "to_long", to_java(obj))))
143147
#define as_long_long(obj) ((long long)polyglot_as_i64(polyglot_invoke(PY_TRUFFLE_CEXT, "PyLong_AsPrimitive", to_java(obj), 1, sizeof(long long), polyglot_from_string("long long", "utf-8"))))

graalpython/com.oracle.graal.python.cext/src/dictobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ int PyDict_DelItemString(PyObject *d, const char *key) {
114114
return UPCALL_CEXT_I(_jls_PyDict_DelItem, native_to_java(d), polyglot_from_string(key, SRC_CS));
115115
}
116116

117-
UPCALL_ID(update);
118117
int PyDict_Update(PyObject *a, PyObject *b) {
119-
PyObject* result = UPCALL_O(native_to_java(a), _jls_update, native_to_java(b));
118+
PyObject* result = UPCALL_O(native_to_java(a), polyglot_from_string("update", SRC_CS), native_to_java(b));
120119
if (PyErr_Occurred()) {
121120
return -1;
122121
} else {

graalpython/com.oracle.graal.python.cext/src/errors.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ void _PyErr_BadInternalCall(const char *filename, int lineno) {
4545
UPCALL_CEXT_VOID(_jls__PyErr_BadInternalCall, polyglot_from_string(filename, SRC_CS), lineno, native_to_java(NULL));
4646
}
4747

48+
UPCALL_ID(PyErr_CreateAndSetException);
4849
#undef PyErr_BadInternalCall
49-
UPCALL_ID(PyTruffle_Err_Format);
5050
void PyErr_BadInternalCall(void) {
5151
assert(0 && "bad argument to internal function");
52-
UPCALL_CEXT_VOID(_jls_PyTruffle_Err_Format, native_to_java(PyExc_SystemError), polyglot_from_string("bad argument to internal function", SRC_CS));
52+
UPCALL_CEXT_VOID(_jls_PyErr_CreateAndSetException, native_to_java(PyExc_SystemError), polyglot_from_string("bad argument to internal function", SRC_CS));
5353
}
5454
#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
5555

@@ -64,7 +64,6 @@ void PyErr_SetString(PyObject *exception, const char *string) {
6464
PyErr_SetObject(exception, value);
6565
}
6666

67-
UPCALL_ID(PyErr_CreateAndSetException);
6867
void PyErr_SetObject(PyObject *exception, PyObject *value) {
6968
UPCALL_CEXT_VOID(_jls_PyErr_CreateAndSetException, native_to_java(exception), native_to_java(value));
7069
}

graalpython/com.oracle.graal.python.cext/src/exceptions.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ void initialize_exceptions() {
121121
}
122122

123123

124-
UPCALL_ID(with_traceback);
125124
int PyException_SetTraceback(PyObject *self, PyObject *tb) {
126-
PyObject* result = UPCALL_O(native_to_java(self), _jls_with_traceback, native_to_java(tb));
125+
PyObject* result = UPCALL_O(native_to_java(self), polyglot_from_string("with_traceback", SRC_CS), native_to_java(tb));
127126
if (result == NULL) {
128127
return -1;
129128
} else {

graalpython/com.oracle.graal.python.cext/src/functionobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
PyTypeObject PyFunction_Type = PY_TRUFFLE_TYPE("function", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyFunctionObject));
4444

45-
UPCALL_ID(classmethod);
4645
PyObject* PyClassMethod_New(PyObject* method) {
47-
return UPCALL_O(PY_BUILTIN, _jls_classmethod, native_to_java(method));
46+
return UPCALL_O(PY_BUILTIN, polyglot_from_string("classmethod", SRC_CS), native_to_java(method));
4847
}

graalpython/com.oracle.graal.python.cext/src/import.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ PyObject* PyImport_Import(PyObject *name) {
4949
return UPCALL_CEXT_O(_jls_PyImport_ImportModule, native_to_java(name));
5050
}
5151

52-
UPCALL_ID(__import__);
5352
PyObject* PyImport_ImportModuleLevelObject(PyObject* name, PyObject* globals, PyObject* locals,
5453
PyObject* fromlist, int level) {
55-
return UPCALL_O(PY_BUILTIN, _jls___import__, native_to_java(name), native_to_java(globals),
54+
return UPCALL_O(PY_BUILTIN, polyglot_from_string("__import__", SRC_CS), native_to_java(name), native_to_java(globals),
5655
native_to_java(locals), native_to_java(fromlist), level);
5756
}
5857

graalpython/com.oracle.graal.python.cext/src/modsupport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
332332
case 'p':
333333
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
334334
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
335-
PyTruffle_WriteOut(output_idx, int, (UPCALL_I(native_to_java(arg), _jls___bool__)));
335+
PyTruffle_WriteOut(output_idx, int, (UPCALL_I(native_to_java(arg), polyglot_from_string("__bool__", SRC_CS))));
336336
break;
337337
case '(':
338338
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);

graalpython/com.oracle.graal.python.cext/src/object.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,29 +249,25 @@ PyObject* _PyObject_CallMethod_SizeT(PyObject* object, const char* method, const
249249
return UPCALL_CEXT_O(_jls_PyObject_CallMethod, native_to_java(object), polyglot_from_string(method, SRC_CS), native_to_java(args));
250250
}
251251

252-
UPCALL_ID(type);
253252
PyObject* PyObject_Type(PyObject* obj) {
254-
return UPCALL_O(PY_BUILTIN, _jls_type, native_to_java(obj));
253+
return UPCALL_O(PY_BUILTIN, polyglot_from_string("type", SRC_CS), native_to_java(obj));
255254
}
256255

257-
UPCALL_ID(__getitem__);
258256
PyObject* PyObject_GetItem(PyObject* obj, PyObject* key) {
259-
return UPCALL_O(native_to_java(obj), _jls___getitem__, native_to_java(key));
257+
return UPCALL_O(native_to_java(obj), polyglot_from_string("__getitem__", SRC_CS), native_to_java(key));
260258
}
261259

262260
UPCALL_ID(PyObject_SetItem);
263261
int PyObject_SetItem(PyObject* obj, PyObject* key, PyObject* value) {
264262
return UPCALL_CEXT_I(_jls_PyObject_SetItem, native_to_java(obj), native_to_java(key), native_to_java(value));
265263
}
266264

267-
UPCALL_ID(__format__);
268265
PyObject* PyObject_Format(PyObject* obj, PyObject* spec) {
269-
return UPCALL_O(native_to_java(obj), _jls___format__, native_to_java(spec));
266+
return UPCALL_O(native_to_java(obj), polyglot_from_string("__format__", SRC_CS), native_to_java(spec));
270267
}
271268

272-
UPCALL_ID(iter);
273269
PyObject* PyObject_GetIter(PyObject* obj) {
274-
return UPCALL_O(PY_BUILTIN, _jls_iter, native_to_java(obj));
270+
return UPCALL_O(PY_BUILTIN, polyglot_from_string("iter", SRC_CS), native_to_java(obj));
275271
}
276272

277273
UPCALL_ID(PyObject_IsInstance);
@@ -344,9 +340,8 @@ int PyObject_GenericSetAttr(PyObject* obj, PyObject* attr, PyObject* value) {
344340
return PyObject_SetAttr(obj, attr, value);
345341
}
346342

347-
UPCALL_ID(hash);
348343
Py_hash_t PyObject_Hash(PyObject* obj) {
349-
return UPCALL_I(PY_BUILTIN, _jls_hash, native_to_java(obj));
344+
return UPCALL_I(PY_BUILTIN, polyglot_from_string("hash", SRC_CS), native_to_java(obj));
350345
}
351346

352347
UPCALL_ID(PyObject_HashNotImplemented);

graalpython/com.oracle.graal.python.cext/src/pyhash.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@
4040
*/
4141
#include "capi.h"
4242

43-
UPCALL_ID(hash);
4443
Py_hash_t _Py_HashDouble(double value) {
45-
return UPCALL_L(PY_BUILTIN, _jls_hash, value);
44+
return UPCALL_L(PY_BUILTIN, polyglot_from_string("hash", SRC_CS), value);
4645
}
4746

4847
long _PyHASH_INF;
@@ -56,5 +55,5 @@ void initialize_hashes() {
5655
}
5756

5857
Py_hash_t _Py_HashBytes(const void *src, Py_ssize_t len) {
59-
return UPCALL_L(PY_BUILTIN, _jls_hash, polyglot_from_string(src, "ascii"));
58+
return UPCALL_L(PY_BUILTIN, polyglot_from_string("hash", SRC_CS), polyglot_from_string(src, "ascii"));
6059
}

0 commit comments

Comments
 (0)