rustyrazorblade opened a new issue, #12537:
URL: https://github.com/apache/arrow-rs/issues/12537
**Describe the bug**
`RowConverter::convert_rows` is very slow for a `FixedSizeList` column. Its
cost per row grows with the list length.
In `arrow-row` 59.3.0, `list::decode_fixed_size_list` (`src/list.rs`, around
line 378) walks each row to find where each element ends. It does this by
calling `converter.convert_raw` once **per element** on a one-row slice:
```rust
for _ in 0..value_length {
let mut temp_child_rows = vec![&row[row_offset..]];
unsafe { converter.convert_raw(&mut temp_child_rows, validate_utf8) }?;
let decoded_bytes = row.len() - row_offset - temp_child_rows[0].len();
...
}
```
Each of those calls builds a full one-row child array (buffers, a
`NullBuffer`, an `Arc<dyn Array>`) only to measure its length, then drops it.
For a `FixedSizeList(Float32, 768)` (an embedding vector), that is 768 array
builds and drops per row, plus a `Vec` allocation per element, before the real
decode of `child_rows` runs. The same loop is still in 60.2.
**Impact we measured**
We decode rows of a table with one `FixedSizeList(Field("item", Float32,
false), 768)` column plus a few scalar columns. A CPU profile of the decode of
a few tens of GB of such rows (about 1,000 CPU seconds in total):
- `convert_rows` is about 78% of all CPU.
- The top self frames under it are `decode_primitive<Float32>` (14%),
`decode_column` (8%), `Arc<Bytes>::drop_slow` (7%), `decode_fixed_size_list`
(6%), `decode_nulls` / `collect_bool` (5%) and `malloc` (5%). Nearly all of
this is the per-element probe loop, not the final decode.
As a workaround, we now encode such a column as `FixedSizeBinary(768 * 4)`
over the values buffer before the row converter sees it, and rebuild the list
after decode. That makes the allocation count per row the same for 8 elements
as for 768.
**To Reproduce**
```rust
use std::sync::Arc;
use arrow_array::{ArrayRef, FixedSizeListArray, Float32Array};
use arrow_row::{RowConverter, SortField};
use arrow_schema::{DataType, Field};
let dims = 768;
let n = 10_000;
let item = Arc::new(Field::new("item", DataType::Float32, false));
let values = Float32Array::from_iter_values((0..n * dims).map(|v| v as f32));
let list: ArrayRef = Arc::new(
FixedSizeListArray::try_new(item.clone(), dims as i32, Arc::new(values),
None).unwrap(),
);
let conv =
RowConverter::new(vec![SortField::new(DataType::FixedSizeList(item, dims as
i32))]).unwrap();
let rows = conv.convert_columns(&[list]).unwrap();
// Time this, and compare dims = 8 with dims = 768.
let _ = conv.convert_rows(rows.iter()).unwrap();
```
**Expected behavior**
The decode should not build an array per element. Two possible fixes:
- When the element type has a fixed encoded width (any primitive;
`encoded_len` is known up front), each element's length is a constant. The
probe loop can be skipped, so a row splits into `value_length` slices of that
width with no `convert_raw` call.
- For variable-width elements, a length-only walk (the `fixed`/`variable`
helpers already know how to skip one encoded value) would avoid building
throwaway arrays.
**Additional context**
`arrow-row` 59.3.0. The same code is in 60.2.0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]