Skip to content

Commit

Permalink
PERF: Statically allocate unicode strings of memhandler (#21450)
Browse files Browse the repository at this point in the history
* PERF Statically allocate unicode strings

* Update numpy/core/src/multiarray/multiarraymodule.c

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
  • Loading branch information
eendebakpt and seberg committed May 5, 2022
1 parent 7f77205 commit 06f6cc2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/alloc.c
Expand Up @@ -576,7 +576,7 @@ PyDataMem_GetHandler()
if (p == NULL) {
return NULL;
}
handler = PyDict_GetItemString(p, "current_allocator");
handler = PyDict_GetItem(p, npy_ma_str_current_allocator);
if (handler == NULL) {
handler = PyCapsule_New(&default_handler, "mem_handler", NULL);
if (handler == NULL) {
Expand Down
5 changes: 5 additions & 0 deletions numpy/core/src/multiarray/multiarraymodule.c
Expand Up @@ -4665,6 +4665,7 @@ set_flaginfo(PyObject *d)
return;
}

NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_current_allocator = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_function = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_struct = NULL;
Expand All @@ -4681,6 +4682,10 @@ NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_numpy = NULL;
static int
intern_strings(void)
{
npy_ma_str_current_allocator = PyUnicode_InternFromString("current_allocator");
if (npy_ma_str_current_allocator == NULL) {
return -1;
}
npy_ma_str_array = PyUnicode_InternFromString("__array__");
if (npy_ma_str_array == NULL) {
return -1;
Expand Down
1 change: 1 addition & 0 deletions numpy/core/src/multiarray/multiarraymodule.h
@@ -1,6 +1,7 @@
#ifndef NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_
#define NUMPY_CORE_SRC_MULTIARRAY_MULTIARRAYMODULE_H_

NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_current_allocator;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_function;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_struct;
Expand Down

0 comments on commit 06f6cc2

Please sign in to comment.