Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
baee891
MNT: move interned strings into a single global struct
ngoldbaum May 30, 2024
69075c1
MNT: move cached imports into a global struct
ngoldbaum May 21, 2024
e5c1bd6
MNT: move cpu dispatch registry into global data struct
ngoldbaum May 30, 2024
7719cf2
MNT: move ndarray.__array_*__ references to global data struct
ngoldbaum May 30, 2024
3cbb68d
MNT: move sys.flags.optimize cache to global data struct
ngoldbaum May 30, 2024
2ffcc71
MNT: set up tuple for truediv in global data struct
ngoldbaum May 30, 2024
d2ca21b
MNT: move unpack_bits LUT into global static struct
ngoldbaum May 30, 2024
a1f7200
MNT: move references to int(1) and int(0) to global static struct
ngoldbaum May 30, 2024
26c243d
MNT: move initialization of global ArrayMethods to module initialization
ngoldbaum May 30, 2024
536e5fb
MNT: move initialization of global tuples to global data struct
ngoldbaum May 30, 2024
0c22126
MNT: move default extobj contextvar to global data dict
ngoldbaum May 30, 2024
90b1f38
MNT: move PyArray_SetStringFunction internals into global data struct
ngoldbaum May 30, 2024
6a296c4
BUG: remove questionable static initialization of an array object
ngoldbaum May 30, 2024
398f095
MNT: split global data struct into two structs
ngoldbaum Jun 3, 2024
8f84875
MNT: add PyArrayMethodObject caches to static data struct
ngoldbaum Jun 5, 2024
402a83c
MNT: move some thread-unsafe state in thread-unsafe state struct
ngoldbaum Jun 5, 2024
e43275a
MNT: make data structs static instead of heap-allocated
ngoldbaum Jun 5, 2024
b706536
MNT: apply sebastian's refactoring suggestions
ngoldbaum Jun 6, 2024
c237038
MNT: move static data structs into their own file
ngoldbaum Jun 7, 2024
98ae65d
MNT: Add more global state I missed to the thread_unsafe_state struct
ngoldbaum Jun 7, 2024
a334ddc
MNT: verify all entries in npy_interned_str and npy_static_pydata are…
ngoldbaum Jun 11, 2024
9ed317f
Apply suggestions from code review
ngoldbaum Jun 13, 2024
3ae66b1
MAINT: apply more of Sebastian's suggestions
ngoldbaum Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MNT: move unpack_bits LUT into global static struct
  • Loading branch information
ngoldbaum committed Jun 19, 2024
commit d2ca21bafb66060a2915d0b86c6b54e3626ccc36
31 changes: 3 additions & 28 deletions numpy/_core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -1747,15 +1747,6 @@ pack_bits(PyObject *input, int axis, char order)
static PyObject *
unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
{
static int unpack_init = 0;
/*
* lookuptable for bitorder big as it has been around longer
* bitorder little is handled via byteswapping in the loop
*/
static union {
npy_uint8 bytes[8];
npy_uint64 uint64;
} unpack_lookup_big[256];
PyArrayObject *inp;
PyArrayObject *new = NULL;
PyArrayObject *out = NULL;
Expand Down Expand Up @@ -1841,22 +1832,6 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
goto fail;
}

/*
* setup lookup table under GIL, 256 8 byte blocks representing 8 bits
* expanded to 1/0 bytes
*/
if (unpack_init == 0) {
npy_intp j;
for (j=0; j < 256; j++) {
npy_intp k;
for (k=0; k < 8; k++) {
npy_uint8 v = (j & (1 << k)) == (1 << k);
unpack_lookup_big[j].bytes[7 - k] = v;
}
}
unpack_init = 1;
}

count = PyArray_DIM(new, axis) * 8;
if (outdims[axis] > count) {
in_n = count / 8;
Expand All @@ -1883,15 +1858,15 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
/* for unity stride we can just copy out of the lookup table */
if (order == 'b') {
for (index = 0; index < in_n; index++) {
npy_uint64 v = unpack_lookup_big[*inptr].uint64;
npy_uint64 v = npy_ma_global_data->unpack_lookup_big[*inptr].uint64;
memcpy(outptr, &v, 8);
outptr += 8;
inptr += in_stride;
}
}
else {
for (index = 0; index < in_n; index++) {
npy_uint64 v = unpack_lookup_big[*inptr].uint64;
npy_uint64 v = npy_ma_global_data->unpack_lookup_big[*inptr].uint64;
if (order != 'b') {
v = npy_bswap8(v);
}
Expand All @@ -1902,7 +1877,7 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
}
/* Clean up the tail portion */
if (in_tail) {
npy_uint64 v = unpack_lookup_big[*inptr].uint64;
npy_uint64 v = npy_ma_global_data->unpack_lookup_big[*inptr].uint64;
if (order != 'b') {
v = npy_bswap8(v);
}
Expand Down
18 changes: 18 additions & 0 deletions numpy/_core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4990,6 +4990,24 @@ initialize_static_globals(void)
}
npy_ma_global_data->optimize = PyLong_AsLong(level);
Py_DECREF(level);

/*
* see unpack_bits for how this table is used.
*
* LUT for bigendian bitorder, littleendian is handled via
* byteswapping in the loop.
*
* 256 8 byte blocks representing 8 bits expanded to 1 or 0 bytes
*/
npy_intp j;
for (j=0; j < 256; j++) {
npy_intp k;
for (k=0; k < 8; k++) {
npy_uint8 v = (j & (1 << k)) == (1 << k);
npy_ma_global_data->unpack_lookup_big[j].bytes[7 - k] = v;
}
}

return 0;
}

Expand Down
8 changes: 8 additions & 0 deletions numpy/_core/src/multiarray/multiarraymodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ typedef struct npy_ma_global_data_struct {
*/
long optimize;

/*
* LUT used by unpack_bits
*/
union {
npy_uint8 bytes[8];
npy_uint64 uint64;
} unpack_lookup_big[256];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it makes sense to split out the non-objects because at some point, I assume that modules may need to decref all of these (or implement a tp_traverse, but that is the same thing).

Also, this table for example is truly static even with subinterpreters. The only issue is initialization.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this table for example is truly static even with subinterpreters. The only issue is initialization.

Yup, everything in this struct is static after module initialization. I guess if we ever supported subinterpreters someone could make this be initialized once for all subinterpreters or just do it every time. I don't think it makes a ton of difference...


/*
* Used for CPU feature detection and dispatch
*
Expand Down