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 1 commit
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
35 changes: 25 additions & 10 deletions common/datavalues/src/columns/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,32 @@ impl<T: PrimitiveType> Column for PrimitiveColumn<T> {
if length == self.len() {
return Arc::new(self.clone());
}
let iter = self
.values()
.iter()
.zip(filter.values().iter())
.filter(|(_, f)| *f)
.map(|(v, _)| *v);

let values: Vec<T> = iter.collect();
let col = PrimitiveColumn {
values: values.into(),
};
let mut res = Vec::<T>::with_capacity(length);
let mut offset = 0;
let values = self.values();

const MASK_BITS: usize = 64;
for mut mask in 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.

should care about the remaining in chunks.

Copy link
Contributor Author

@platoneko platoneko Feb 28, 2022

Choose a reason for hiding this comment

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

I have checked that Bitmap::chunks::<u64>() won't yield chunk less than 64

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my unittest, I set N = 1000 to make Column not aligned to 64.

if mask == u64::MAX {
res.extend(&values[offset..offset + MASK_BITS]);
} else {
while mask != 0 {
let n = std::intrinsics::cttz(mask) as usize;
res.push(values[offset + n]);
Copy link
Member

Choose a reason for hiding this comment

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

maybe better to use ptr.write to have better performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe better to use ptr.write to have better performance.

I'd like to have a try

mask = mask & (mask - 1);
}
}
offset += MASK_BITS;
}

for i in offset..self.len() {
if filter.get_data(i) {
res.push(values[i]);
}
}

let col = PrimitiveColumn { values: res.into() };

Arc::new(col)
}
Expand Down
1 change: 1 addition & 0 deletions common/datavalues/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#![feature(generic_associated_types)]
#![feature(trusted_len)]
#![feature(core_intrinsics)]
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LGTM


#[macro_use]
mod macros;
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| i < 100 || i > 800)),
expect: (0..N)
.map(|i| i as i32)
.filter(|&i| i < 100 || i > 800)
.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
);
}
}