Skip to content

Commit 09ebd4e

Browse files
authored
perf(parquet): slice up contiguous buffer for decimals and fsb (#10364)
# Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. --> N/A # Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Converting decimals and FixedLenByteArrays to individual allocs requires a lot of alloc+drop time and memory overhead. # What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Allocate one big buffer, and cut it up into slices of `bytes::Bytes`. This amortizes the alloc upfront, and effectively eliminates the drop overhead by turning a dealloc to an Arc decrement per slice. This still has moderate memory overhead. `Bytes` is 32 bytes, while the slices usually expose less than 16 bytes at a time e.g. UUIDs or decimal128, but a lot more effort would be required to implement a (perhaps borrowed?) alternative that behaves like `FixedLenByteArray`. There are a few other ideas worth exploring, primarily custom `ColumnValueEncoder`s that converts on the fly to avoid the temporary allocs all together. ~~There is also a small outlining+inlining in `compare_greater` to help out in the `get_min_max` loop.~~ Extracted to #10389. # Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. --> Should be covered by existing tests. There are also new benches included (extracted to #10388): <details> <summary>decimal bench</summary> ``` Benchmarking decimal/default: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.4s, or reduce sample count to 90. decimal/default time: [53.676 ms 53.979 ms 54.306 ms] thrpt: [1.0790 GiB/s 1.0855 GiB/s 1.0916 GiB/s] change: time: [−44.611% −44.247% −43.885%] (p = 0.00 < 0.05) thrpt: [+78.204% +79.362% +80.542%] Performance has improved. Found 5 outliers among 100 measurements (5.00%) 4 (4.00%) high mild 1 (1.00%) high severe Benchmarking decimal/bloom_filter: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.8s, or reduce sample count to 70. decimal/bloom_filter time: [66.597 ms 66.838 ms 67.095 ms] thrpt: [894.26 MiB/s 897.70 MiB/s 900.94 MiB/s] change: time: [−40.229% −39.907% −39.555%] (p = 0.00 < 0.05) thrpt: [+65.440% +66.408% +67.306%] Performance has improved. Found 10 outliers among 100 measurements (10.00%) 8 (8.00%) high mild 2 (2.00%) high severe Benchmarking decimal/parquet_2: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 10.8s, or reduce sample count to 40. decimal/parquet_2 time: [105.82 ms 106.23 ms 106.66 ms] thrpt: [562.52 MiB/s 564.84 MiB/s 567.02 MiB/s] change: time: [−48.804% −48.523% −48.229%] (p = 0.00 < 0.05) thrpt: [+93.160% +94.263% +95.329%] Performance has improved. Found 3 outliers among 100 measurements (3.00%) 2 (2.00%) high mild 1 (1.00%) high severe Benchmarking decimal/zstd: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.4s, or reduce sample count to 70. decimal/zstd time: [61.806 ms 62.013 ms 62.282 ms] thrpt: [963.36 MiB/s 967.55 MiB/s 970.78 MiB/s] change: time: [−60.257% −59.907% −59.556%] (p = 0.00 < 0.05) thrpt: [+147.25% +149.42% +151.62%] Performance has improved. Found 11 outliers among 100 measurements (11.00%) 1 (1.00%) low severe 2 (2.00%) low mild 5 (5.00%) high mild 3 (3.00%) high severe Benchmarking decimal/zstd_parquet_2: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 11.9s, or reduce sample count to 40. decimal/zstd_parquet_2 time: [119.51 ms 119.87 ms 120.29 ms] thrpt: [498.82 MiB/s 500.54 MiB/s 502.07 MiB/s] change: time: [−55.716% −55.352% −54.992%] (p = 0.00 < 0.05) thrpt: [+122.18% +123.98% +125.82%] Performance has improved. Found 11 outliers among 100 measurements (11.00%) 2 (2.00%) low mild 2 (2.00%) high mild 7 (7.00%) high severe Benchmarking decimal/cdc: Warming up for 3.0000 s Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.8s, or reduce sample count to 60. decimal/cdc time: [77.810 ms 78.092 ms 78.389 ms] thrpt: [765.42 MiB/s 768.33 MiB/s 771.11 MiB/s] change: time: [−35.689% −35.360% −35.011%] (p = 0.00 < 0.05) thrpt: [+53.873% +54.703% +55.494%] Performance has improved. Found 10 outliers among 100 measurements (10.00%) 10 (10.00%) high mild ``` </details> <details> <summary>fsb bench</summary> ``` fsb/default time: [5.4152 ms 5.4276 ms 5.4407 ms] thrpt: [2.8943 GiB/s 2.9013 GiB/s 2.9079 GiB/s] change: time: [−27.824% −27.512% −27.215%] (p = 0.00 < 0.05) thrpt: [+37.391% +37.953% +38.550%] Performance has improved. Found 7 outliers among 100 measurements (7.00%) 1 (1.00%) low mild 5 (5.00%) high mild 1 (1.00%) high severe fsb/bloom_filter time: [5.8681 ms 5.8908 ms 5.9154 ms] thrpt: [2.6621 GiB/s 2.6732 GiB/s 2.6835 GiB/s] change: time: [−25.701% −25.298% −24.896%] (p = 0.00 < 0.05) thrpt: [+33.148% +33.866% +34.592%] Performance has improved. Found 6 outliers among 100 measurements (6.00%) 3 (3.00%) high mild 3 (3.00%) high severe fsb/parquet_2 time: [8.6936 ms 8.7692 ms 8.8503 ms] thrpt: [1.7793 GiB/s 1.7957 GiB/s 1.8113 GiB/s] change: time: [−31.621% −31.005% −30.286%] (p = 0.00 < 0.05) thrpt: [+43.444% +44.939% +46.243%] Performance has improved. Found 10 outliers among 100 measurements (10.00%) 9 (9.00%) high mild 1 (1.00%) high severe fsb/zstd time: [7.1760 ms 7.2043 ms 7.2360 ms] thrpt: [2.1762 GiB/s 2.1858 GiB/s 2.1944 GiB/s] change: time: [−21.480% −21.068% −20.631%] (p = 0.00 < 0.05) thrpt: [+25.994% +26.692% +27.356%] Performance has improved. Found 2 outliers among 100 measurements (2.00%) 1 (1.00%) high mild 1 (1.00%) high severe fsb/zstd_parquet_2 time: [9.2977 ms 9.4085 ms 9.5422 ms] thrpt: [1.6503 GiB/s 1.6737 GiB/s 1.6937 GiB/s] change: time: [−30.266% −29.333% −28.383%] (p = 0.00 < 0.05) thrpt: [+39.632% +41.508% +43.401%] Performance has improved. Found 7 outliers among 100 measurements (7.00%) 4 (4.00%) high mild 3 (3.00%) high severe fsb/cdc time: [7.9524 ms 7.9700 ms 7.9878 ms] thrpt: [1.9714 GiB/s 1.9758 GiB/s 1.9802 GiB/s] change: time: [−21.280% −20.793% −20.375%] (p = 0.00 < 0.05) thrpt: [+25.589% +26.251% +27.033%] Performance has improved. Found 2 outliers among 100 measurements (2.00%) 2 (2.00%) high mildaa ``` </details> # Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out. --> No.
1 parent b1ca9c4 commit 09ebd4e

1 file changed

Lines changed: 86 additions & 72 deletions

File tree

  • parquet/src/arrow/arrow_writer

parquet/src/arrow/arrow_writer/mod.rs

Lines changed: 86 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use std::sync::{Arc, Mutex};
2626
use std::vec::IntoIter;
2727

2828
use arrow_array::cast::AsArray;
29-
use arrow_array::types::*;
3029
use arrow_array::{ArrayRef, Int32Array, RecordBatch, RecordBatchWriter};
30+
use arrow_array::{PrimitiveArray, types::*};
3131
use arrow_schema::{
3232
ArrowError, DataType as ArrowDataType, Field, IntervalUnit, SchemaRef, TimeUnit,
3333
};
@@ -1751,19 +1751,19 @@ fn write_leaf(
17511751
}
17521752
ArrowDataType::Decimal32(_, _) => {
17531753
let array = column.as_primitive::<Decimal32Type>();
1754-
get_decimal_32_array_slice(array, indices.iter().copied())
1754+
get_decimal_array_slice(array, indices.iter().copied())
17551755
}
17561756
ArrowDataType::Decimal64(_, _) => {
17571757
let array = column.as_primitive::<Decimal64Type>();
1758-
get_decimal_64_array_slice(array, indices.iter().copied())
1758+
get_decimal_array_slice(array, indices.iter().copied())
17591759
}
17601760
ArrowDataType::Decimal128(_, _) => {
17611761
let array = column.as_primitive::<Decimal128Type>();
1762-
get_decimal_128_array_slice(array, indices.iter().copied())
1762+
get_decimal_array_slice(array, indices.iter().copied())
17631763
}
17641764
ArrowDataType::Decimal256(_, _) => {
17651765
let array = column.as_primitive::<Decimal256Type>();
1766-
get_decimal_256_array_slice(array, indices.iter().copied())
1766+
get_decimal_array_slice(array, indices.iter().copied())
17671767
}
17681768
ArrowDataType::Float16 => {
17691769
let array = column.as_primitive::<Float16Type>();
@@ -1821,14 +1821,10 @@ fn get_interval_ym_array_slice(
18211821
array: &arrow_array::IntervalYearMonthArray,
18221822
indices: impl ExactSizeIterator<Item = usize>,
18231823
) -> Vec<FixedLenByteArray> {
1824-
let mut values = Vec::with_capacity(indices.len());
1825-
for i in indices {
1826-
let mut value = array.value(i).to_le_bytes().to_vec();
1827-
let mut suffix = vec![0; 8];
1828-
value.append(&mut suffix);
1829-
values.push(FixedLenByteArray::from(ByteArray::from(value)))
1830-
}
1831-
values
1824+
chunk_array_slice(12, indices, move |i, chunk| {
1825+
let value = array.value(i);
1826+
chunk[0..4].copy_from_slice(&value.to_le_bytes());
1827+
})
18321828
}
18331829

18341830
/// Returns 12-byte values representing 3 values of months, days and milliseconds (4-bytes each).
@@ -1837,93 +1833,111 @@ fn get_interval_dt_array_slice(
18371833
array: &arrow_array::IntervalDayTimeArray,
18381834
indices: impl ExactSizeIterator<Item = usize>,
18391835
) -> Vec<FixedLenByteArray> {
1840-
let mut values = Vec::with_capacity(indices.len());
1841-
for i in indices {
1842-
let mut out = [0; 12];
1836+
chunk_array_slice(12, indices, move |i, chunk| {
18431837
let value = array.value(i);
1844-
out[4..8].copy_from_slice(&value.days.to_le_bytes());
1845-
out[8..12].copy_from_slice(&value.milliseconds.to_le_bytes());
1846-
values.push(FixedLenByteArray::from(ByteArray::from(out.to_vec())));
1838+
chunk[4..8].copy_from_slice(&value.days.to_le_bytes());
1839+
chunk[8..12].copy_from_slice(&value.milliseconds.to_le_bytes());
1840+
})
1841+
}
1842+
1843+
trait NativeDecimalType: DecimalType {
1844+
type NativeBytes: AsRef<[u8]>;
1845+
1846+
fn to_be_bytes(value: Self::Native) -> Self::NativeBytes;
1847+
}
1848+
impl NativeDecimalType for Decimal32Type {
1849+
type NativeBytes = [u8; Self::BYTE_LENGTH];
1850+
1851+
fn to_be_bytes(value: Self::Native) -> Self::NativeBytes {
1852+
value.to_be_bytes()
18471853
}
1848-
values
18491854
}
1855+
impl NativeDecimalType for Decimal64Type {
1856+
type NativeBytes = [u8; Self::BYTE_LENGTH];
18501857

1851-
fn get_decimal_32_array_slice(
1852-
array: &arrow_array::Decimal32Array,
1853-
indices: impl ExactSizeIterator<Item = usize>,
1854-
) -> Vec<FixedLenByteArray> {
1855-
let mut values = Vec::with_capacity(indices.len());
1856-
let size = decimal_length_from_precision(array.precision());
1857-
for i in indices {
1858-
let as_be_bytes = array.value(i).to_be_bytes();
1859-
let resized_value = as_be_bytes[(4 - size)..].to_vec();
1860-
values.push(FixedLenByteArray::from(ByteArray::from(resized_value)));
1858+
fn to_be_bytes(value: Self::Native) -> Self::NativeBytes {
1859+
value.to_be_bytes()
18611860
}
1862-
values
18631861
}
1862+
impl NativeDecimalType for Decimal128Type {
1863+
type NativeBytes = [u8; Self::BYTE_LENGTH];
18641864

1865-
fn get_decimal_64_array_slice(
1866-
array: &arrow_array::Decimal64Array,
1867-
indices: impl ExactSizeIterator<Item = usize>,
1868-
) -> Vec<FixedLenByteArray> {
1869-
let mut values = Vec::with_capacity(indices.len());
1870-
let size = decimal_length_from_precision(array.precision());
1871-
for i in indices {
1872-
let as_be_bytes = array.value(i).to_be_bytes();
1873-
let resized_value = as_be_bytes[(8 - size)..].to_vec();
1874-
values.push(FixedLenByteArray::from(ByteArray::from(resized_value)));
1865+
fn to_be_bytes(value: Self::Native) -> Self::NativeBytes {
1866+
value.to_be_bytes()
18751867
}
1876-
values
18771868
}
1869+
impl NativeDecimalType for Decimal256Type {
1870+
type NativeBytes = [u8; Self::BYTE_LENGTH];
18781871

1879-
fn get_decimal_128_array_slice(
1880-
array: &arrow_array::Decimal128Array,
1881-
indices: impl ExactSizeIterator<Item = usize>,
1882-
) -> Vec<FixedLenByteArray> {
1883-
let mut values = Vec::with_capacity(indices.len());
1884-
let size = decimal_length_from_precision(array.precision());
1885-
for i in indices {
1886-
let as_be_bytes = array.value(i).to_be_bytes();
1887-
let resized_value = as_be_bytes[(16 - size)..].to_vec();
1888-
values.push(FixedLenByteArray::from(ByteArray::from(resized_value)));
1872+
fn to_be_bytes(value: Self::Native) -> Self::NativeBytes {
1873+
value.to_be_bytes()
18891874
}
1890-
values
18911875
}
18921876

1893-
fn get_decimal_256_array_slice(
1894-
array: &arrow_array::Decimal256Array,
1877+
fn get_decimal_array_slice<T: NativeDecimalType>(
1878+
array: &PrimitiveArray<T>,
18951879
indices: impl ExactSizeIterator<Item = usize>,
18961880
) -> Vec<FixedLenByteArray> {
1897-
let mut values = Vec::with_capacity(indices.len());
1898-
let size = decimal_length_from_precision(array.precision());
1899-
for i in indices {
1900-
let as_be_bytes = array.value(i).to_be_bytes();
1901-
let resized_value = as_be_bytes[(32 - size)..].to_vec();
1902-
values.push(FixedLenByteArray::from(ByteArray::from(resized_value)));
1881+
let chunk_size = decimal_length_from_precision(array.precision());
1882+
assert!(chunk_size <= T::BYTE_LENGTH);
1883+
1884+
if chunk_size == T::BYTE_LENGTH {
1885+
// Special-case that allows inlining memcpy.
1886+
chunk_array_slice(chunk_size, indices, move |i, chunk| {
1887+
let as_be_bytes = T::to_be_bytes(array.value(i));
1888+
chunk.copy_from_slice(as_be_bytes.as_ref());
1889+
})
1890+
} else {
1891+
chunk_array_slice(chunk_size, indices, move |i, chunk| {
1892+
let as_be_bytes = T::to_be_bytes(array.value(i));
1893+
let resized_value = &as_be_bytes.as_ref()[(T::BYTE_LENGTH - chunk.len())..];
1894+
chunk.copy_from_slice(resized_value);
1895+
})
19031896
}
1904-
values
19051897
}
19061898

19071899
fn get_float_16_array_slice(
19081900
array: &arrow_array::Float16Array,
19091901
indices: impl ExactSizeIterator<Item = usize>,
19101902
) -> Vec<FixedLenByteArray> {
1911-
let mut values = Vec::with_capacity(indices.len());
1912-
for i in indices {
1913-
let value = array.value(i).to_le_bytes().to_vec();
1914-
values.push(FixedLenByteArray::from(ByteArray::from(value)));
1915-
}
1916-
values
1903+
chunk_array_slice(2, indices, move |i, chunk| {
1904+
let value = array.value(i).to_le_bytes();
1905+
chunk.copy_from_slice(&value);
1906+
})
19171907
}
19181908

19191909
fn get_fsb_array_slice(
19201910
array: &arrow_array::FixedSizeBinaryArray,
19211911
indices: impl ExactSizeIterator<Item = usize>,
19221912
) -> Vec<FixedLenByteArray> {
1923-
let mut values = Vec::with_capacity(indices.len());
1924-
for i in indices {
1925-
let value = array.value(i).to_vec();
1926-
values.push(FixedLenByteArray::from(ByteArray::from(value)))
1913+
chunk_array_slice(array.value_size(), indices, move |i, chunk| {
1914+
let value = array.value(i);
1915+
chunk.copy_from_slice(value);
1916+
})
1917+
}
1918+
1919+
#[inline]
1920+
fn chunk_array_slice(
1921+
chunk_size: usize,
1922+
indices: impl ExactSizeIterator<Item = usize>,
1923+
writer: impl Fn(usize, &mut [u8]),
1924+
) -> Vec<FixedLenByteArray> {
1925+
let capacity = indices.len() * chunk_size;
1926+
// TODO: This could be done with Vec::spare_capacity_mut,
1927+
// but [MaybeUninit]::write_copy_of_slice is gated behind MSRV 1.93
1928+
let mut arena = vec![0; capacity];
1929+
for (i, chunk) in indices.zip(arena.chunks_exact_mut(chunk_size)) {
1930+
writer(i, chunk);
1931+
}
1932+
chunk_contiguous_vec(arena, chunk_size)
1933+
}
1934+
1935+
fn chunk_contiguous_vec(arena: Vec<u8>, chunk_size: usize) -> Vec<FixedLenByteArray> {
1936+
let mut values = Vec::with_capacity(arena.len() / chunk_size);
1937+
let mut arena = Bytes::from(arena);
1938+
while arena.len() >= chunk_size {
1939+
let slice = arena.split_to(chunk_size);
1940+
values.push(FixedLenByteArray::from(ByteArray::from(slice)));
19271941
}
19281942
values
19291943
}

0 commit comments

Comments
 (0)