Skip to content

Commit 52d9340

Browse files
committed
[GR-11651] Improve performance for calling native class methods.
PullRequest: graalpython/196
2 parents 9b14be3 + 9b90427 commit 52d9340

File tree

15 files changed

+313
-90
lines changed

15 files changed

+313
-90
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
code = """
41+
#include "Python.h"
42+
43+
typedef struct {
44+
PyObject_HEAD;
45+
} NativeCustomObject;
46+
47+
PyObject* nc_classmethod(PyObject* class, PyObject* args) {
48+
return PyTuple_GET_ITEM(args, 0);
49+
}
50+
51+
static struct PyMethodDef nc_methods[] = {
52+
{"customClassMethod", nc_classmethod, METH_VARARGS | METH_CLASS, ""},
53+
{NULL, NULL, 0, NULL}
54+
};
55+
56+
static PyTypeObject NativeCustomType = {
57+
PyVarObject_HEAD_INIT(NULL, 0)
58+
"NativeCustomType.NativeCustomType",
59+
sizeof(NativeCustomObject), /* tp_basicsize */
60+
0, /* tp_itemsize */
61+
0, /* tp_dealloc */
62+
0,
63+
0,
64+
0,
65+
0, /* tp_reserved */
66+
0,
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
Py_TPFLAGS_DEFAULT,
77+
"",
78+
0, /* tp_traverse */
79+
0, /* tp_clear */
80+
0, /* tp_richcompare */
81+
0, /* tp_weaklistoffset */
82+
0, /* tp_iter */
83+
0, /* tp_iternext */
84+
nc_methods, /* tp_methods */
85+
NULL, /* tp_members */
86+
0, /* tp_getset */
87+
0, /* tp_base */
88+
0, /* tp_dict */
89+
0, /* tp_descr_get */
90+
0, /* tp_descr_set */
91+
0, /* tp_dictoffset */
92+
0, /* tp_init */
93+
PyType_GenericAlloc, /* tp_alloc */
94+
PyType_GenericNew, /* tp_new */
95+
PyObject_Del, /* tp_free */
96+
};
97+
98+
static PyModuleDef c_classmethod_module = {
99+
PyModuleDef_HEAD_INIT,
100+
"c_classmethod_module",
101+
"",
102+
-1,
103+
NULL, NULL, NULL, NULL, NULL
104+
};
105+
106+
PyMODINIT_FUNC
107+
PyInit_c_classmethod(void)
108+
{
109+
PyObject* m;
110+
111+
if (PyType_Ready(&NativeCustomType) < 0)
112+
return NULL;
113+
114+
m = PyModule_Create(&c_classmethod_module);
115+
if (m == NULL)
116+
return NULL;
117+
118+
Py_INCREF(&NativeCustomType);
119+
PyModule_AddObject(m, "NativeCustomType", (PyObject *)&NativeCustomType);
120+
return m;
121+
}
122+
123+
"""
124+
125+
126+
ccompile("c_classmethod", code)
127+
from c_classmethod import NativeCustomType
128+
129+
def count(num):
130+
total = 0
131+
for i in range(num):
132+
total += NativeCustomType.customClassMethod(i)
133+
return total
134+
135+
136+
def measure(num):
137+
result = count(num)
138+
print("result = " + str(result))
139+
140+
141+
def __benchmark__(num=1000000):
142+
measure(num)
143+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
class CustomType:
41+
@classmethod
42+
def customClassMethod(clazz, *args):
43+
return args[0]
44+
45+
def count(num):
46+
total = 0
47+
for i in range(num):
48+
total += CustomType.customClassMethod(i)
49+
return total
50+
51+
52+
def measure(num):
53+
result = count(num)
54+
print("result = " + str(result))
55+
56+
57+
def __benchmark__(num=1000000):
58+
measure(num)
59+

graalpython/com.oracle.graal.python.cext/include/truffle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void *truffle_managed_from_handle(void *nativeHandle);
5252
bool truffle_is_handle_to_managed(void *nativeHandle);
5353
void *truffle_assign_managed(void *dst, void *managed);
5454
void *truffle_deref_handle_for_managed(void *managed);
55+
bool truffle_cannot_be_handle(void *nativeHandle);
5556

5657
/*
5758
* All function below here are deprecated and will be removed in a future release.

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,74 +66,74 @@ int PyNumber_Check(PyObject *o) {
6666
}
6767

6868
UPCALL_ID(PyNumber_UnaryOp);
69-
static PyObject * do_unaryop(PyObject *v, UnaryOp unaryop, char *unaryop_name) {
70-
return UPCALL_CEXT_O(_jls_PyNumber_UnaryOp, native_to_java(v), unaryop, polyglot_from_string(unaryop_name, SRC_CS));
69+
static PyObject * do_unaryop(PyObject *v, UnaryOp unaryop) {
70+
return UPCALL_CEXT_O(_jls_PyNumber_UnaryOp, native_to_java(v), unaryop);
7171
}
7272

7373
UPCALL_ID(PyNumber_BinOp);
74-
static PyObject * do_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_name) {
75-
return UPCALL_CEXT_O(_jls_PyNumber_BinOp, native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
74+
static PyObject * do_binop(PyObject *v, PyObject *w, BinOp binop) {
75+
return UPCALL_CEXT_O(_jls_PyNumber_BinOp, native_to_java(v), native_to_java(w), binop);
7676
}
7777

7878
UPCALL_ID(PyNumber_InPlaceBinOp);
79-
static PyObject * do_inplace_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_name) {
80-
return UPCALL_CEXT_O(_jls_PyNumber_InPlaceBinOp, native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
79+
static PyObject * do_inplace_binop(PyObject *v, PyObject *w, BinOp binop) {
80+
return UPCALL_CEXT_O(_jls_PyNumber_InPlaceBinOp, native_to_java(v), native_to_java(w), binop);
8181
}
8282

8383
PyObject * PyNumber_Add(PyObject *o1, PyObject *o2) {
84-
return do_binop(o1, o2, ADD, "+");
84+
return do_binop(o1, o2, ADD);
8585
}
8686

8787
PyObject * PyNumber_Subtract(PyObject *o1, PyObject *o2) {
88-
return do_binop(o1, o2, SUB, "-");
88+
return do_binop(o1, o2, SUB);
8989
}
9090

9191
PyObject * PyNumber_Multiply(PyObject *o1, PyObject *o2) {
92-
return do_binop(o1, o2, MUL, "*");
92+
return do_binop(o1, o2, MUL);
9393
}
9494

9595
PyObject * PyNumber_TrueDivide(PyObject *o1, PyObject *o2) {
96-
return do_binop(o1, o2, TRUEDIV, "/");
96+
return do_binop(o1, o2, TRUEDIV);
9797
}
9898

9999
PyObject * PyNumber_FloorDivide(PyObject *o1, PyObject *o2) {
100-
return do_binop(o1, o2, FLOORDIV, "//");
100+
return do_binop(o1, o2, FLOORDIV);
101101
}
102102

103103
PyObject * PyNumber_Remainder(PyObject *o1, PyObject *o2) {
104-
return do_binop(o1, o2, MOD, "%");
104+
return do_binop(o1, o2, MOD);
105105
}
106106

107107
PyObject * PyNumber_Lshift(PyObject *o1, PyObject *o2) {
108-
return do_binop(o1, o2, LSHIFT, "<<");
108+
return do_binop(o1, o2, LSHIFT);
109109
}
110110

111111
PyObject * PyNumber_Rshift(PyObject *o1, PyObject *o2) {
112-
return do_binop(o1, o2, RSHIFT, ">>");
112+
return do_binop(o1, o2, RSHIFT);
113113
}
114114

115115
PyObject * PyNumber_Or(PyObject *o1, PyObject *o2) {
116-
return do_binop(o1, o2, OR, "|");
116+
return do_binop(o1, o2, OR);
117117
}
118118

119119
PyObject * PyNumber_And(PyObject *o1, PyObject *o2) {
120-
return do_binop(o1, o2, AND, "&");
120+
return do_binop(o1, o2, AND);
121121
}
122122

123123
PyObject * PyNumber_Xor(PyObject *o1, PyObject *o2) {
124-
return do_binop(o1, o2, XOR, "^");
124+
return do_binop(o1, o2, XOR);
125125
}
126126

127127
PyObject * PyNumber_Positive(PyObject *o) {
128-
return do_unaryop(o, POS, "+");
128+
return do_unaryop(o, POS);
129129
}
130130

131131
PyObject * PyNumber_Negative(PyObject *o) {
132-
return do_unaryop(o, NEG, "-");
132+
return do_unaryop(o, NEG);
133133
}
134134

135135
PyObject * PyNumber_Invert(PyObject *o) {
136-
return do_unaryop(o, INVERT, "~");
136+
return do_unaryop(o, INVERT);
137137
}
138138

139139
UPCALL_ID(PyNumber_Index);
@@ -145,7 +145,7 @@ PyObject * PyNumber_Index(PyObject *o) {
145145
}
146146

147147
PyObject * PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2) {
148-
return do_inplace_binop(o1, o2, TRUEDIV, "/");
148+
return do_inplace_binop(o1, o2, TRUEDIV);
149149
}
150150

151151
Py_ssize_t PyNumber_AsSsize_t(PyObject *item, PyObject *err) {

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ uint64_t (*PY_TRUFFLE_CEXT_LANDING_L)(void* name, ...);
5353
double (*PY_TRUFFLE_CEXT_LANDING_D)(void* name, ...);
5454
void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
5555

56-
typedef void* (*cache_t)(uint64_t);
57-
static cache_t cache;
58-
59-
#define resolve_handle(__cache__, __addr__) (__cache__)(__addr__)
56+
cache_t cache;
6057

6158
__attribute__((constructor (__COUNTER__)))
6259
static void initialize_upcall_functions() {
@@ -198,28 +195,6 @@ static void initialize_capi() {
198195
initialize_bufferprocs();
199196
}
200197

201-
// Heuristic to test if some value is a pointer object
202-
// TODO we need a reliable solution for that
203-
#define IS_POINTER(__val__) (polyglot_is_value(__val__) && !polyglot_fits_in_i64(__val__))
204-
205-
MUST_INLINE
206-
void* native_to_java(PyObject* obj) {
207-
if (obj == NULL) {
208-
return Py_NoValue;
209-
} else if (obj == Py_None) {
210-
return Py_None;
211-
} else if (polyglot_is_string(obj)) {
212-
return obj;
213-
} else if (truffle_is_handle_to_managed(obj)) {
214-
return resolve_handle(cache, (uint64_t)obj);
215-
} else if (truffle_is_handle_to_managed(obj->ob_refcnt)) {
216-
return truffle_managed_from_handle(obj->ob_refcnt);
217-
} else if (IS_POINTER(obj->ob_refcnt)) {
218-
return obj->ob_refcnt;
219-
}
220-
return obj;
221-
}
222-
223198
void* native_to_java_exported(PyObject* obj) {
224199
return native_to_java(obj);
225200
}
@@ -240,12 +215,12 @@ PyObject* to_sulong(void *o) {
240215
/** to be used from Java code only; reads native 'ob_type' field */
241216
void* get_ob_type(PyObject* obj) {
242217
PyTypeObject* type = obj->ob_type;
243-
if (truffle_is_handle_to_managed(type)) {
218+
if (!truffle_cannot_be_handle(type)) {
244219
return resolve_handle(cache, (uint64_t)type);
245220
} else {
246221
// we have stored a handle to the Java class in ob_refcnt
247222
void* handle = (void*)((PyObject*)type)->ob_refcnt;
248-
if (truffle_is_handle_to_managed(handle)) {
223+
if (!truffle_cannot_be_handle(handle)) {
249224
return resolve_handle(cache, (uint64_t)handle);
250225
} else {
251226
// assume handle is a TruffleObject
@@ -273,15 +248,15 @@ uint64_t PyTruffle_Wchar_Size() {
273248
}
274249

275250
void* PyObjectHandle_ForJavaObject(void* cobj, unsigned long flags) {
276-
if (!truffle_is_handle_to_managed(cobj)) {
251+
if (truffle_cannot_be_handle(cobj)) {
277252
return truffle_deref_handle_for_managed(cobj);
278253
}
279254
return cobj;
280255
}
281256

282257
/** to be used from Java code only; only creates the deref handle */
283258
void* PyObjectHandle_ForJavaType(void* ptype) {
284-
if (!truffle_is_handle_to_managed(ptype)) {
259+
if (truffle_cannot_be_handle(ptype)) {
285260
return truffle_deref_handle_for_managed(ptype);
286261
}
287262
return ptype;

0 commit comments

Comments
 (0)