structured arrays slow to mask
I have 5 arrays of the same data type and the same length n (can be quite large). I need to regularly mask all of those array with the same mask, and I thought about what would be the fastest way. Fitting the data into one (n, 5) array works, and masking is equally fast with a (5, n) array.
I'd love to use a structured array so I can properly name the data in the rest of the code (e.g., arr["density"] instead of arr[:, 3]). Unfortunately, structured arrays mask way slower than the other two options:

Code to reproduce the plot:
import numpy
import perfplot
import random
def setup(n):
a = numpy.random.rand(n, 5)
b = numpy.ascontiguousarray(a.T)
c = a.copy()
c.dtype = [(f"col{k}", c.dtype) for k in range(a.shape[1])]
idx = random.sample(range(0, n), 25)
mask = numpy.ones(n, dtype=bool)
mask[idx] = False
return a, b, c, mask
def mask_rows(data):
a, _, _, mask = data
return a[mask]
def mask_named(data):
_, _, c, mask = data
return c[mask]
def mask_cols(data):
_, b, _, mask = data
return b[:, mask]
perfplot.show(
setup=setup,
kernels=[mask_rows, mask_cols, mask_named],
n_range=[2 ** k for k in range(5, 20)],
equality_check=None,
)
Hi, I’d like to work on this issue. I’ll investigate why boolean masking is slower for structured arrays compared to regular NumPy arrays, profile the relevant operations, and explore whether the performance can be improved without changing existing behavior. Could you please assign it to me?