diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-07-24 23:44:42 +0200 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-07-25 01:27:16 +0200 |
commit | e04e79ae2cef411ff0ae26ca6089a5ac4f249021 (patch) | |
tree | 867881c2053a01b4ee9b4df52339a606ba4400e4 | |
parent | 3b5f871606a758f8bf19981b06f423792cbdc5f4 (diff) | |
download | numpy-e04e79ae2cef411ff0ae26ca6089a5ac4f249021.tar.gz |
ENH: intern some commonly used strings in the multiarray module
Allows improving dictionary lookup performance.
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 5 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 37 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.h | 11 |
3 files changed, 51 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 3da2dfae7..c57df147a 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -12,6 +12,7 @@ #include "npy_config.h" #include "npy_pycompat.h" +#include "multiarraymodule.h" #include "common.h" #include "ctors.h" @@ -1069,7 +1070,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd, if ((subtype != &PyArray_Type)) { PyObject *res, *func, *args; - func = PyObject_GetAttrString((PyObject *)fa, "__array_finalize__"); + func = PyObject_GetAttr((PyObject *)fa, npy_ma_str_array_finalize); if (func && func != Py_None) { if (NpyCapsule_Check(func)) { /* A C-function is stored here */ @@ -3368,7 +3369,7 @@ PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type, #endif ) { PyObject *newbuf; - newbuf = PyObject_GetAttrString(buf, "__buffer__"); + newbuf = PyObject_GetAttr(buf, npy_ma_str_buffer); if (newbuf == NULL) { Py_DECREF(type); return NULL; diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 7fade0832..463acbebd 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -56,6 +56,7 @@ NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0; #include "common.h" #include "ufunc_override.h" #include "scalarmathmodule.h" /* for npy_mul_with_overflow_intp */ +#include "multiarraymodule.h" /* Only here for API compatibility */ NPY_NO_EXPORT PyTypeObject PyBigArray_Type; @@ -4022,6 +4023,38 @@ set_flaginfo(PyObject *d) return; } +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_prepare = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_wrap = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_finalize = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_buffer = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_ufunc = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_order = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_copy = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_dtype = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_ndmin = NULL; + +static int +intern_strings(void) +{ + npy_ma_str_array = PyUString_InternFromString("__array__"); + npy_ma_str_array_prepare = PyUString_InternFromString("__array_prepare__"); + npy_ma_str_array_wrap = PyUString_InternFromString("__array_wrap__"); + npy_ma_str_array_finalize = PyUString_InternFromString("__array_finalize__"); + npy_ma_str_buffer = PyUString_InternFromString("__buffer__"); + npy_ma_str_ufunc = PyUString_InternFromString("__numpy_ufunc__"); + npy_ma_str_order = PyUString_InternFromString("order"); + npy_ma_str_copy = PyUString_InternFromString("copy"); + npy_ma_str_dtype = PyUString_InternFromString("dtype"); + npy_ma_str_ndmin = PyUString_InternFromString("ndmin"); + + return npy_ma_str_array && npy_ma_str_array_prepare && + npy_ma_str_array_wrap && npy_ma_str_array_finalize && + npy_ma_str_array_finalize && npy_ma_str_ufunc && + npy_ma_str_order && npy_ma_str_copy && npy_ma_str_dtype && + npy_ma_str_ndmin; +} + #if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, @@ -4194,6 +4227,10 @@ PyMODINIT_FUNC initmultiarray(void) { set_flaginfo(d); + if (!intern_strings()) { + goto err; + } + if (set_typeinfo(d) != 0) { goto err; } diff --git a/numpy/core/src/multiarray/multiarraymodule.h b/numpy/core/src/multiarray/multiarraymodule.h index 5a3b14b0b..82ae24845 100644 --- a/numpy/core/src/multiarray/multiarraymodule.h +++ b/numpy/core/src/multiarray/multiarraymodule.h @@ -1,4 +1,15 @@ #ifndef _NPY_MULTIARRAY_H_ #define _NPY_MULTIARRAY_H_ +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_prepare; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_wrap; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_finalize; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_buffer; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_ufunc; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_order; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_copy; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_dtype; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_ndmin; + #endif |