Skip to content

Commit 9b4b5d2

Browse files
committed
[GR-17731] Performance improvements for native wrappers
PullRequest: graalpython/617
2 parents dd6e5a3 + 0f3ac2d commit 9b4b5d2

28 files changed

+854
-163
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Copyright (c) 2019, 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+
int res = PyType_IsSubtype((PyTypeObject*)class, &PyUnicode_Type);
49+
return PyLong_FromLong(res);
50+
}
51+
52+
static struct PyMethodDef nc_methods[] = {
53+
{"customClassMethod", nc_classmethod, METH_VARARGS | METH_CLASS, ""},
54+
{NULL, NULL, 0, NULL}
55+
};
56+
57+
static PyTypeObject NativeCustomType = {
58+
PyVarObject_HEAD_INIT(NULL, 0)
59+
"NativeCustomType.NativeCustomType",
60+
sizeof(NativeCustomObject), /* tp_basicsize */
61+
0, /* tp_itemsize */
62+
0, /* tp_dealloc */
63+
0,
64+
0,
65+
0,
66+
0, /* tp_reserved */
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
0,
77+
Py_TPFLAGS_DEFAULT,
78+
0,
79+
0, /* tp_traverse */
80+
0, /* tp_clear */
81+
0, /* tp_richcompare */
82+
0, /* tp_weaklistoffset */
83+
0, /* tp_iter */
84+
0, /* tp_iternext */
85+
nc_methods, /* tp_methods */
86+
NULL, /* tp_members */
87+
0, /* tp_getset */
88+
0, /* tp_base */
89+
0, /* tp_dict */
90+
0, /* tp_descr_get */
91+
0, /* tp_descr_set */
92+
0, /* tp_dictoffset */
93+
0, /* tp_init */
94+
PyType_GenericAlloc, /* tp_alloc */
95+
PyType_GenericNew, /* tp_new */
96+
PyObject_Del, /* tp_free */
97+
};
98+
99+
static PyModuleDef c_classmethod_module = {
100+
PyModuleDef_HEAD_INIT,
101+
"c_classmethod_module",
102+
0,
103+
-1,
104+
NULL, NULL, NULL, NULL, NULL
105+
};
106+
107+
PyMODINIT_FUNC
108+
PyInit_c_classmethod(void)
109+
{
110+
PyObject* m;
111+
112+
if (PyType_Ready(&NativeCustomType) < 0)
113+
return NULL;
114+
115+
m = PyModule_Create(&c_classmethod_module);
116+
if (m == NULL)
117+
return NULL;
118+
119+
Py_INCREF(&NativeCustomType);
120+
PyModule_AddObject(m, "NativeCustomType", (PyObject *)&NativeCustomType);
121+
return m;
122+
}
123+
124+
"""
125+
126+
127+
ccompile("c_classmethod", code)
128+
from c_classmethod import NativeCustomType
129+
130+
def count(num):
131+
total = 0
132+
for i in range(num):
133+
total += NativeCustomType.customClassMethod(i)
134+
return total
135+
136+
137+
def measure(num):
138+
result = count(num)
139+
print("result = " + str(result))
140+
141+
142+
def __benchmark__(num=1000000):
143+
measure(num)
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Copyright (c) 2019, 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+
void** somememory = (void**)calloc(sizeof(PyObject*), 2);
49+
somememory[0] = class;
50+
somememory[1] = PyTuple_GET_ITEM(args, 0);
51+
int res = PyType_IsSubtype((PyTypeObject*)somememory[0], (PyTypeObject*)somememory[1]);
52+
res += PyType_IsSubtype((PyTypeObject*)somememory[1], (PyTypeObject*)somememory[0]);
53+
return PyLong_FromLong(res);
54+
}
55+
56+
static struct PyMethodDef nc_methods[] = {
57+
{"customClassMethod", nc_classmethod, METH_VARARGS | METH_CLASS, ""},
58+
{NULL, NULL, 0, NULL}
59+
};
60+
61+
static PyTypeObject NativeCustomType = {
62+
PyVarObject_HEAD_INIT(NULL, 0)
63+
"NativeCustomType.NativeCustomType",
64+
sizeof(NativeCustomObject), /* tp_basicsize */
65+
0, /* tp_itemsize */
66+
0, /* tp_dealloc */
67+
0,
68+
0,
69+
0,
70+
0, /* tp_reserved */
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
0,
77+
0,
78+
0,
79+
0,
80+
0,
81+
Py_TPFLAGS_DEFAULT,
82+
0,
83+
0, /* tp_traverse */
84+
0, /* tp_clear */
85+
0, /* tp_richcompare */
86+
0, /* tp_weaklistoffset */
87+
0, /* tp_iter */
88+
0, /* tp_iternext */
89+
nc_methods, /* tp_methods */
90+
NULL, /* tp_members */
91+
0, /* tp_getset */
92+
0, /* tp_base */
93+
0, /* tp_dict */
94+
0, /* tp_descr_get */
95+
0, /* tp_descr_set */
96+
0, /* tp_dictoffset */
97+
0, /* tp_init */
98+
PyType_GenericAlloc, /* tp_alloc */
99+
PyType_GenericNew, /* tp_new */
100+
PyObject_Del, /* tp_free */
101+
};
102+
103+
static PyModuleDef c_classmethod_module = {
104+
PyModuleDef_HEAD_INIT,
105+
"c_classmethod_module",
106+
0,
107+
-1,
108+
NULL, NULL, NULL, NULL, NULL
109+
};
110+
111+
PyMODINIT_FUNC
112+
PyInit_c_classmethod(void)
113+
{
114+
PyObject* m;
115+
116+
if (PyType_Ready(&NativeCustomType) < 0)
117+
return NULL;
118+
119+
m = PyModule_Create(&c_classmethod_module);
120+
if (m == NULL)
121+
return NULL;
122+
123+
Py_INCREF(&NativeCustomType);
124+
PyModule_AddObject(m, "NativeCustomType", (PyObject *)&NativeCustomType);
125+
return m;
126+
}
127+
128+
"""
129+
130+
131+
ccompile("c_classmethod", code)
132+
from c_classmethod import NativeCustomType
133+
134+
def count(num):
135+
total = 0
136+
classes = [str, object, type]
137+
for i in range(num):
138+
total += NativeCustomType.customClassMethod(classes[i % len(classes)])
139+
return total
140+
141+
142+
def measure(num):
143+
result = count(num)
144+
print("result = " + str(result))
145+
146+
147+
def __benchmark__(num=1000000):
148+
measure(num)

0 commit comments

Comments
 (0)