Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specialized filter kernels in compute module (up to 10x faster) #1248

Merged
merged 21 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
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
113 changes: 61 additions & 52 deletions arrow/benches/filter_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,127 +48,136 @@ fn add_benchmark(c: &mut Criterion) {

let data_array = create_primitive_array::<UInt8Type>(size, 0.0);

c.bench_function("filter optimize", |b| {
c.bench_function("filter optimize (kept 1/2)", |b| {
b.iter(|| FilterBuilder::new(&filter_array).optimize().build())
});

c.bench_function("filter optimize high selectivity", |b| {
c.bench_function("filter optimize high selectivity (kept 1023/1024)", |b| {
b.iter(|| FilterBuilder::new(&dense_filter_array).optimize().build())
});

c.bench_function("filter optimize low selectivity", |b| {
c.bench_function("filter optimize low selectivity (kept 1/1024)", |b| {
b.iter(|| FilterBuilder::new(&sparse_filter_array).optimize().build())
});

c.bench_function("filter u8", |b| {
c.bench_function("filter u8 (kept 1/2)", |b| {
b.iter(|| bench_filter(&data_array, &filter_array))
});
c.bench_function("filter u8 high selectivity", |b| {
c.bench_function("filter u8 high selectivity (kept 1023/1024)", |b| {
b.iter(|| bench_filter(&data_array, &dense_filter_array))
});
c.bench_function("filter u8 low selectivity", |b| {
c.bench_function("filter u8 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_filter(&data_array, &sparse_filter_array))
});

c.bench_function("filter context u8", |b| {
c.bench_function("filter context u8 (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context u8 high selectivity", |b| {
c.bench_function("filter context u8 high selectivity (kept 1023/1024)", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context u8 low selectivity", |b| {
c.bench_function("filter context u8 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_primitive_array::<Int32Type>(size, 0.0);
c.bench_function("filter i32", |b| {
c.bench_function("filter i32 (kept 1/2)", |b| {
b.iter(|| bench_filter(&data_array, &filter_array))
});
c.bench_function("filter i32 high selectivity", |b| {
c.bench_function("filter i32 high selectivity (kept 1023/1024)", |b| {
b.iter(|| bench_filter(&data_array, &dense_filter_array))
});
c.bench_function("filter i32 low selectivity", |b| {
c.bench_function("filter i32 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_filter(&data_array, &sparse_filter_array))
});

c.bench_function("filter context i32", |b| {
c.bench_function("filter context i32 (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context i32 high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context i32 low selectivity", |b| {
c.bench_function(
"filter context i32 high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function("filter context i32 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_primitive_array::<Int32Type>(size, 0.5);
c.bench_function("filter context i32 w NULLs", |b| {
c.bench_function("filter context i32 w NULLs (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context i32 w NULLs high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context i32 w NULLs low selectivity", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});
c.bench_function(
"filter context i32 w NULLs high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context i32 w NULLs low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

let data_array = create_primitive_array::<UInt8Type>(size, 0.5);
c.bench_function("filter context u8 w NULLs", |b| {
c.bench_function("filter context u8 w NULLs (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context u8 w NULLs high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context u8 w NULLs low selectivity", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});
c.bench_function(
"filter context u8 w NULLs high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context u8 w NULLs low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

let data_array = create_primitive_array::<Float32Type>(size, 0.5);
c.bench_function("filter f32", |b| {
c.bench_function("filter f32 (kept 1/2)", |b| {
b.iter(|| bench_filter(&data_array, &filter_array))
});
c.bench_function("filter context f32", |b| {
c.bench_function("filter context f32 (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context f32 high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context f32 low selectivity", |b| {
c.bench_function(
"filter context f32 high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function("filter context f32 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_string_array::<i32>(size, 0.5);
c.bench_function("filter context string", |b| {
c.bench_function("filter context string (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context string high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context string low selectivity", |b| {
c.bench_function(
"filter context string high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function("filter context string low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_string_dict_array::<Int32Type>(size, 0.0);
c.bench_function("filter context string dictionary", |b| {
c.bench_function("filter context string dictionary (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context string dictionary high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context string dictionary low selectivity", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});
c.bench_function(
"filter context string dictionary high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context string dictionary low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

let data_array = create_string_dict_array::<Int32Type>(size, 0.5);
c.bench_function("filter context string dictionary w NULLs", |b| {
c.bench_function("filter context string dictionary w NULLs (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function(
"filter context string dictionary w NULLs high selectivity",
"filter context string dictionary w NULLs high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context string dictionary w NULLs low selectivity",
"filter context string dictionary w NULLs low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

Expand Down
23 changes: 11 additions & 12 deletions arrow/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,36 +475,35 @@ impl MutableBuffer {
let (_, upper) = iterator.size_hint();
let upper = upper.expect("from_trusted_len_iter requires an upper limit");

let aligned_len = bit_util::ceil(upper, 64) * 8;
let mut result = MutableBuffer::new(aligned_len);
let mut result = {
let byte_capacity: usize = upper.saturating_add(7) / 8;
MutableBuffer::new(byte_capacity)
};

'a: loop {
let mut accum: u64 = 0;
let mut mask: u64 = 1;
let mut byte_accum: u8 = 0;
let mut mask: u8 = 1;

//collect (up to) 64 bits into a u64
//collect (up to) 8 bits into a byte
while mask != 0 {
if let Some(value) = iterator.next() {
accum |= match value {
byte_accum |= match value {
true => mask,
false => 0,
};
mask <<= 1;
} else {
if mask != 1 {
// Add accumulator
result.push_unchecked(accum);
// Add last byte
result.push_unchecked(byte_accum);
}
break 'a;
}
}

// Soundness: from_trusted_len
result.push_unchecked(accum);
result.push_unchecked(byte_accum);
}

// Truncate to byte length - technically not necessary but cannot hurt
result.resize(bit_util::ceil(upper, 8), 0);
result
}

Expand Down
Loading