Skip to content

Commit ed1460e

Browse files
committed
Add micro benchmark for native class instantiation.
1 parent e5e0370 commit ed1460e

File tree

3 files changed

+205
-1
lines changed

3 files changed

+205
-1
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+
int payload;
46+
} NativeTypeObject;
47+
48+
49+
static struct PyMethodDef NativeType_methods[] = {
50+
{NULL, NULL, 0, NULL}
51+
};
52+
53+
static PyTypeObject NativeType = {
54+
PyVarObject_HEAD_INIT(NULL, 0)
55+
"NativeType.NativeType",
56+
sizeof(NativeTypeObject), /* tp_basicsize */
57+
0, /* tp_itemsize */
58+
0, /* tp_dealloc */
59+
0,
60+
0,
61+
0,
62+
0, /* tp_reserved */
63+
0,
64+
0,
65+
0,
66+
0,
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
Py_TPFLAGS_DEFAULT,
74+
"",
75+
0, /* tp_traverse */
76+
0, /* tp_clear */
77+
0, /* tp_richcompare */
78+
0, /* tp_weaklistoffset */
79+
0, /* tp_iter */
80+
0, /* tp_iternext */
81+
NativeType_methods, /* tp_methods */
82+
NULL, /* tp_members */
83+
0, /* tp_getset */
84+
0, /* tp_base */
85+
0, /* tp_dict */
86+
0, /* tp_descr_get */
87+
0, /* tp_descr_set */
88+
0, /* tp_dictoffset */
89+
0, /* tp_init */
90+
PyType_GenericAlloc, /* tp_alloc */
91+
PyType_GenericNew, /* tp_new */
92+
PyObject_Del, /* tp_free */
93+
};
94+
95+
static PyModuleDef NativeTypemodule = {
96+
PyModuleDef_HEAD_INIT,
97+
"c_instantiation",
98+
"",
99+
-1,
100+
NULL, NULL, NULL, NULL, NULL
101+
};
102+
103+
PyMODINIT_FUNC
104+
PyInit_c_instantiation(void)
105+
{
106+
PyObject* m;
107+
108+
109+
if (PyType_Ready(&NativeType) < 0)
110+
return NULL;
111+
112+
113+
m = PyModule_Create(&NativeTypemodule);
114+
if (m == NULL)
115+
return NULL;
116+
117+
Py_INCREF(&NativeType);
118+
PyModule_AddObject(m, "NativeType", (PyObject *)&NativeType);
119+
return m;
120+
}
121+
122+
"""
123+
124+
125+
ccompile("c_instantiation", code)
126+
import c_instantiation
127+
128+
def iterate_list(ll, num):
129+
types = []
130+
for t in range(num):
131+
obj = c_instantiation.NativeType()
132+
types.append(type(obj))
133+
return types
134+
135+
136+
def measure(num):
137+
types = iterate_list(list(range(num)), num)
138+
print("last type: " + types[-1].__name__)
139+
140+
141+
def __benchmark__(num=1000000):
142+
measure(num)
143+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
payload = 0
42+
43+
def iterate_list(ll, num):
44+
types = []
45+
for t in range(num):
46+
obj = CustomType()
47+
types.append(type(obj))
48+
return types
49+
50+
51+
def measure(num):
52+
types = iterate_list(list(range(num)), num)
53+
print("last type: " + types[-1].__name__)
54+
55+
56+
def __benchmark__(num=1000000):
57+
measure(num)
58+

mx.graalpython/mx_graalpython_bench_param.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@
9595
'magic-bool': ITER_10 + ['100000000'],
9696
'c-magic-bool': ITER_10 + ['100000000'],
9797
'magic-iter': ITER_10 + ['10000000'],
98-
'c-magic-iter': ITER_10 + ['10000000']
98+
'c-magic-iter': ITER_10 + ['10000000'],
99+
'instantiation': ITER_10 + ['50000000'],
100+
'c-instantiation': ITER_10 + ['50000000']
101+
99102
}
100103

101104

0 commit comments

Comments
 (0)