You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple numeric cast in pyarrow / Arrow C++ seems to be quite a bit slower compared to numpy. Often Arrow does more work (safe casting, handling nulls, ..), but I also see this this for a plain unsafe int64 -> float64 cast of a 1d array without nulsl, where Arrow is almost 4-5x slower than numpy. For a simple operation like that, this seems an unexpected big difference.
The python code I used to compare both:
importnumpyasnpimportpyarrowaspanrows=10**5arr_np=np.random.randn(nrows*20).astype("int64")
arr=pa.array(arr_np)
%timeitarr_np.astype("float64")
# 1.1 ms ± 2.69 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)%timeitarr.cast(pa.float64(), safe=False)
# 4.7 ms ± 10.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
I see this both with my local development version as with released conda packages.
Looking into the inner loop in both Arrow and numpy, it seems to be quite similar. In numpy, almost all time is spent in _aligned_contig_cast_long_to_double, which essentially boils down to:
Anybody any insight in why our templated C++ code is so much slower than numpy's C code? Logically it looks very similar, or would there be something in our code that prevents optimizations?
Running perf on the examples, two quick observations (without much experience with this tool and without looking into detail) that might be relevant:
The perf flamegraph show big chunk of exc_page_fault in CastPrimitive (a bit more than half of the time spent in CastPrimitive is attributed to that)
Drilling down into the annotations of the hotspot, the actual copy of the casted float into the destination buffer (movsd in the assembly) is taking relatively a lot more time in the Arrow version compared to numpy
Tried running smaller examples through godbolt with gcc 14.1 to see if they produced any instruction differences. I think this replicates the NumPy example:
Maybe the processor just handles the add instructions more effectively than the lea / mov combination in the C++ example?
FWIW if you change the C++ code to do the pointer increment in subsequent expressions rather than doing inline, the asm generated matches the C examples:
voidfoo() {
using OutT = double;
using InT = long;
const InT* in_values;
OutT* out_values;
for (;;) {
*out_values = static_cast<OutT>(*in_values);
out_values++;
in_values++;
}
}
Activity
jorisvandenbossche commentedon Mar 28, 2024
Looking into the inner loop in both Arrow and numpy, it seems to be quite similar. In numpy, almost all time is spent in
_aligned_contig_cast_long_to_double, which essentially boils down to:and in Arrow, almost all time is spent in
CastPrimitive, which essentially does:arrow/cpp/src/arrow/compute/kernels/scalar_cast_internal.cc
Lines 40 to 46 in cf832b8
Anybody any insight in why our templated C++ code is so much slower than numpy's C code? Logically it looks very similar, or would there be something in our code that prevents optimizations?
Running
perfon the examples, two quick observations (without much experience with this tool and without looking into detail) that might be relevant:exc_page_faultinCastPrimitive(a bit more than half of the time spent inCastPrimitiveis attributed to that)movsdin the assembly) is taking relatively a lot more time in the Arrow version compared to numpypitrou commentedon Mar 28, 2024
Well, this really seems to be a problem with jemalloc actually:
pitrou commentedon Mar 28, 2024
Trying to prefer mimalloc in #40875
pitrou commentedon Mar 28, 2024
That said, even with mimalloc we're much slower that Numpy on larger arrays, so there's perhaps another issue (are we allocating a null bitmap?):
pitrou commentedon Mar 28, 2024
By the way, it seems the mimalloc/jemalloc really occurs for a certain range of sizes, above and below which they seem to have similar perf:
pitrou commentedon Mar 28, 2024
By the way, one can also take a look at the C++ results (with mimalloc):
and compare them with a corresponding Python benchmark:
and then to Numpy:
They are all very similar (~150 µs per iteration, despite different levels of internal boilerplate).
jorisvandenbossche commentedon Mar 28, 2024
It's indeed a specific size of data where jemalloc is performing worse, and it was just that size as I was testing in the OP ..
For larger data, the different allocators give the same result, but it's still around 2x slower than numpy (eg for the 10^9 size, 2.3 vs 1.3s)
WillAyd commentedon Jun 5, 2024
Tried running smaller examples through godbolt with gcc 14.1 to see if they produced any instruction differences. I think this replicates the NumPy example:
producing the following asm:
and this typifies the C++ example:
producing the following asm:
Maybe the processor just handles the add instructions more effectively than the lea / mov combination in the C++ example?
FWIW if you change the C++ code to do the pointer increment in subsequent expressions rather than doing inline, the asm generated matches the C examples:
WillAyd commentedon Jun 5, 2024
Maybe there is also the possibility to use memcpy in the case wheresizeof(OutT) == sizeof(InT)to help boost performance?pitrou commentedon Jun 5, 2024
When casting int64 to float64?
WillAyd commentedon Jun 5, 2024
Ah sorry - ignore that comment