|
| 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 | + PyObject* it; |
| 46 | + uint64_t scale; |
| 47 | +} NativeCustomIterableObject; |
| 48 | +
|
| 49 | +
|
| 50 | +typedef struct { |
| 51 | + PyObject_HEAD; |
| 52 | + NativeCustomIterableObject* obj; |
| 53 | + uint64_t pos; |
| 54 | +} NativeCustomIteratorObject; |
| 55 | +
|
| 56 | +PyObject* ci_item(PyObject* self, Py_ssize_t i); |
| 57 | +int ci_init(PyObject* self, PyObject* args, PyObject* kwds); |
| 58 | +
|
| 59 | +PyObject* ci_iter(PyObject* self) { |
| 60 | + return ((NativeCustomIterableObject*)self)->it; |
| 61 | +} |
| 62 | +
|
| 63 | +PyObject* cit_iter(PyObject* self) { |
| 64 | + return self; |
| 65 | +} |
| 66 | +
|
| 67 | +PyObject* cit_next(PyObject* self) { |
| 68 | + NativeCustomIteratorObject* s = (NativeCustomIteratorObject*)self; |
| 69 | + return PyLong_FromLongLong(ci_item((PyObject*)(s->obj), (s->pos)++)); |
| 70 | +} |
| 71 | +
|
| 72 | +PyObject* ci_item(PyObject* self, Py_ssize_t i) { |
| 73 | + return ((NativeCustomIterableObject*)self)->scale * i; |
| 74 | +} |
| 75 | +
|
| 76 | +
|
| 77 | +static PySequenceMethods ci_sequence_methods = { |
| 78 | + 0, |
| 79 | + 0, |
| 80 | + 0, |
| 81 | + ci_item, |
| 82 | + 0, |
| 83 | + 0, |
| 84 | + 0, |
| 85 | + 0, |
| 86 | + 0, |
| 87 | + 0 |
| 88 | +}; |
| 89 | +
|
| 90 | +
|
| 91 | +static struct PyMethodDef ci_methods[] = { |
| 92 | + {NULL, NULL, 0, NULL} |
| 93 | +}; |
| 94 | +
|
| 95 | +static PyTypeObject CustomIteratorType = { |
| 96 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 97 | + "NativeCustomIterable.NativeCustomIterator", |
| 98 | + sizeof(NativeCustomIteratorObject), /* tp_basicsize */ |
| 99 | + 0, /* tp_itemsize */ |
| 100 | + 0, /* tp_dealloc */ |
| 101 | + 0, |
| 102 | + 0, |
| 103 | + 0, |
| 104 | + 0, /* tp_reserved */ |
| 105 | + 0, |
| 106 | + 0, |
| 107 | + 0, |
| 108 | + 0, |
| 109 | + 0, |
| 110 | + 0, |
| 111 | + 0, |
| 112 | + 0, |
| 113 | + 0, |
| 114 | + 0, |
| 115 | + Py_TPFLAGS_DEFAULT, |
| 116 | + "", |
| 117 | + 0, /* tp_traverse */ |
| 118 | + 0, /* tp_clear */ |
| 119 | + 0, /* tp_richcompare */ |
| 120 | + 0, /* tp_weaklistoffset */ |
| 121 | + cit_iter, /* tp_iter */ |
| 122 | + cit_next, /* tp_iternext */ |
| 123 | + ci_methods, /* tp_methods */ |
| 124 | + NULL, /* tp_members */ |
| 125 | + 0, /* tp_getset */ |
| 126 | + 0, /* tp_base */ |
| 127 | + 0, /* tp_dict */ |
| 128 | + 0, /* tp_descr_get */ |
| 129 | + 0, /* tp_descr_set */ |
| 130 | + 0, /* tp_dictoffset */ |
| 131 | + 0, /* tp_init */ |
| 132 | + PyType_GenericAlloc, /* tp_alloc */ |
| 133 | + PyType_GenericNew, /* tp_new */ |
| 134 | + PyObject_Del, /* tp_free */ |
| 135 | +}; |
| 136 | +
|
| 137 | +static PyTypeObject CustomIterableType = { |
| 138 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 139 | + "NativeCustomIterable.NativeCustomIterable", |
| 140 | + sizeof(NativeCustomIterableObject), /* tp_basicsize */ |
| 141 | + 0, /* tp_itemsize */ |
| 142 | + 0, /* tp_dealloc */ |
| 143 | + 0, |
| 144 | + 0, |
| 145 | + 0, |
| 146 | + 0, /* tp_reserved */ |
| 147 | + 0, |
| 148 | + 0, |
| 149 | + &ci_sequence_methods, |
| 150 | + 0, |
| 151 | + 0, |
| 152 | + 0, |
| 153 | + 0, |
| 154 | + 0, |
| 155 | + 0, |
| 156 | + 0, |
| 157 | + Py_TPFLAGS_DEFAULT, |
| 158 | + "", |
| 159 | + 0, /* tp_traverse */ |
| 160 | + 0, /* tp_clear */ |
| 161 | + 0, /* tp_richcompare */ |
| 162 | + 0, /* tp_weaklistoffset */ |
| 163 | + ci_iter, /* tp_iter */ |
| 164 | + 0, /* tp_iternext */ |
| 165 | + ci_methods, /* tp_methods */ |
| 166 | + NULL, /* tp_members */ |
| 167 | + 0, /* tp_getset */ |
| 168 | + 0, /* tp_base */ |
| 169 | + 0, /* tp_dict */ |
| 170 | + 0, /* tp_descr_get */ |
| 171 | + 0, /* tp_descr_set */ |
| 172 | + 0, /* tp_dictoffset */ |
| 173 | + ci_init, /* tp_init */ |
| 174 | + PyType_GenericAlloc, /* tp_alloc */ |
| 175 | + PyType_GenericNew, /* tp_new */ |
| 176 | + PyObject_Del, /* tp_free */ |
| 177 | +}; |
| 178 | +
|
| 179 | +int ci_init(PyObject* self, PyObject* args, PyObject* kwds) { |
| 180 | + static char *kwlist[] = {"scale", NULL}; |
| 181 | + Py_ssize_t n = 0; |
| 182 | +
|
| 183 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n", kwlist, &n)) { |
| 184 | + return NULL; |
| 185 | + } |
| 186 | + NativeCustomIterableObject* tself = (NativeCustomIterableObject*)self; |
| 187 | + tself->scale = n + 1; |
| 188 | +
|
| 189 | + PyObject *argList = PyTuple_New(0); |
| 190 | + PyObject *obj = PyObject_CallObject((PyObject *) &CustomIteratorType, argList); |
| 191 | + ((NativeCustomIteratorObject*)obj)->obj = tself; |
| 192 | + tself->it = obj; |
| 193 | +
|
| 194 | + return 0; |
| 195 | +} |
| 196 | +
|
| 197 | +static PyModuleDef custom_iterable_module = { |
| 198 | + PyModuleDef_HEAD_INIT, |
| 199 | + "c_custom_iterable_module", |
| 200 | + "", |
| 201 | + -1, |
| 202 | + NULL, NULL, NULL, NULL, NULL |
| 203 | +}; |
| 204 | +
|
| 205 | +PyMODINIT_FUNC |
| 206 | +PyInit_c_custom_iterable_module(void) |
| 207 | +{ |
| 208 | + PyObject* m; |
| 209 | +
|
| 210 | + if (PyType_Ready(&CustomIterableType) < 0) |
| 211 | + return NULL; |
| 212 | +
|
| 213 | + if (PyType_Ready(&CustomIteratorType) < 0) |
| 214 | + return NULL; |
| 215 | +
|
| 216 | + m = PyModule_Create(&custom_iterable_module); |
| 217 | + if (m == NULL) |
| 218 | + return NULL; |
| 219 | +
|
| 220 | + Py_INCREF(&CustomIterableType); |
| 221 | + Py_INCREF(&CustomIteratorType); |
| 222 | + PyModule_AddObject(m, "NativeCustomIterable", (PyObject *)&CustomIterableType); |
| 223 | + PyModule_AddObject(m, "NativeCustomIterator", (PyObject *)&CustomIteratorType); |
| 224 | + return m; |
| 225 | +} |
| 226 | +
|
| 227 | +""" |
| 228 | + |
| 229 | + |
| 230 | +ccompile("c_custom_iterable_module", code) |
| 231 | +import c_custom_iterable_module |
| 232 | + |
| 233 | +def count(num): |
| 234 | + idxObj = c_custom_iterable_module.NativeCustomIterable(num % 11) |
| 235 | + for t in range(num): |
| 236 | + it = iter(idxObj) |
| 237 | + val0 = next(it) |
| 238 | + val1 = next(it) |
| 239 | + val2 = next(it) |
| 240 | + |
| 241 | + return (val0, val1, val2) |
| 242 | + |
| 243 | + |
| 244 | +def measure(num): |
| 245 | + result = count(num) |
| 246 | + print("result = " + str(result)) |
| 247 | + |
| 248 | + |
| 249 | +def __benchmark__(num=1000000): |
| 250 | + measure(num) |
| 251 | + |
0 commit comments