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

ISSUE-4264: simd selection #4271

Merged
merged 6 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
52 changes: 43 additions & 9 deletions common/datavalues/src/columns/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::Arc;

use common_arrow::arrow::array::Array;
use common_arrow::arrow::array::PrimitiveArray;
use common_arrow::arrow::bitmap::utils::BitChunkIterExact;
use common_arrow::arrow::bitmap::Bitmap;
use common_arrow::arrow::buffer::Buffer;
use common_arrow::arrow::compute::arity::unary;
Expand Down Expand Up @@ -189,17 +190,50 @@ impl<T: PrimitiveType> Column for PrimitiveColumn<T> {
if length == self.len() {
return Arc::new(self.clone());
}
let iter = self
.values()

let mut new = Vec::<T>::with_capacity(length);
let mut dst = new.as_mut_ptr();

const CHUNK_SIZE: usize = 64;
let mut chunks = self.values().chunks_exact(CHUNK_SIZE);
let mut mask_chunks = filter.values().chunks::<u64>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a faster path for mask_chunks, if the filter has no offsets, it could use chunks_exact to have better performance. See: https://github.com/jorgecarleitao/arrow2/blob/main/src/compute/filter.rs#L142-L147

There are two approaches:

  1. Use generic dispatch like BitChunkIterExact

  2. if offset > 0, use an extra loop to consume the offset, this is called Header Loops, then we can continue the Main Loops && Tail Loops.

The second one may have better performance because chunks.iter() must merge two u64 values to generate one merged u64, see: merge_reversed in arrow2


chunks
.by_ref()
.zip(mask_chunks.by_ref())
.for_each(|(chunk, mut mask)| {
if mask == u64::MAX {
unsafe {
std::ptr::copy(chunk.as_ptr(), dst, CHUNK_SIZE);
dst = dst.add(CHUNK_SIZE);
}
} else {
while mask != 0 {
let n = mask.trailing_zeros() as usize;
unsafe {
dst.write(chunk[n]);
dst = dst.add(1);
}
mask = mask & (mask - 1);
}
}
});

chunks
.remainder()
.iter()
.zip(filter.values().iter())
.filter(|(_, f)| *f)
.map(|(v, _)| *v);
.zip(mask_chunks.remainder_iter())
.for_each(|(value, is_selected)| {
if is_selected {
unsafe {
dst.write(*value);
dst = dst.add(1);
}
}
});

let values: Vec<T> = iter.collect();
let col = PrimitiveColumn {
values: values.into(),
};
unsafe { new.set_len(length) };
let col = PrimitiveColumn { values: new.into() };

Arc::new(col)
}
Expand Down
45 changes: 45 additions & 0 deletions common/datavalues/tests/it/columns/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,48 @@ fn test_const_column() {
let c = ConstColumn::new(Series::from_data(vec![PI]), 24).arc();
println!("{:?}", c);
}

#[test]
fn test_filter_column() {
const N: usize = 1000;
let it = (0..N).map(|i| i as i32);
let data_column: PrimitiveColumn<i32> = Int32Column::from_iterator(it);

struct Test {
filter: BooleanColumn,
expect: Vec<i32>,
}

let tests: Vec<Test> = vec![
Test {
filter: BooleanColumn::from_iterator((0..N).map(|_| true)),
expect: (0..N).map(|i| i as i32).collect(),
},
Test {
filter: BooleanColumn::from_iterator((0..N).map(|_| false)),
expect: vec![],
},
Test {
filter: BooleanColumn::from_iterator((0..N).map(|i| i % 10 == 0)),
expect: (0..N).map(|i| i as i32).filter(|i| i % 10 == 0).collect(),
},
Test {
filter: BooleanColumn::from_iterator((0..N).map(|i| !(100..=800).contains(&i))),
expect: (0..N)
.map(|i| i as i32)
.filter(|&i| !(100..=800).contains(&i))
.collect(),
},
];

for test in tests {
let res = data_column.filter(&test.filter);
assert_eq!(
res.as_any()
.downcast_ref::<PrimitiveColumn<i32>>()
.unwrap()
.values(),
test.expect
);
}
}