-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Improve the hash join performance by replacing the RawTable to a simple Vec for JoinHashMap #6913
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,28 +87,79 @@ use datafusion_common::Result; | |
// | 0 | 0 | 0 | 2 | 4 | <--- hash value 1 maps to 5,4,2 (which means indices values 4,3,1) | ||
// --------------------- | ||
|
||
// TODO: speed up collision checks | ||
// https://github.com/apache/arrow-datafusion/issues/50 | ||
#[cfg(not(test))] | ||
const MIN_JOIN_HASH_MAP_LEN: usize = 1024; | ||
|
||
pub struct JoinHashMap { | ||
// Stores hash value to first index | ||
pub map: RawTable<(u64, u64)>, | ||
// Stores the first index for a bucket | ||
buckets: Vec<u64>, | ||
// Stores indices in chained list data structure | ||
pub next: Vec<u64>, | ||
// Stores the bucket mask for quickly finding a bucket for a hash value | ||
bucket_mask: usize, | ||
} | ||
|
||
/// SymmetricJoinHashMap is similar to JoinHashMap, except that it stores the indices inline, allowing it to mutate | ||
/// and shrink the indices. | ||
pub struct SymmetricJoinHashMap(pub RawTable<(u64, SmallVec<[u64; 1]>)>); | ||
|
||
impl JoinHashMap { | ||
pub(crate) fn with_capacity(capacity: usize) -> Self { | ||
#[cfg(not(test))] | ||
pub(crate) fn with_capacity(bucket_capacity: usize, capacity: usize) -> Self { | ||
let mut bucket_capacity = bucket_capacity.next_power_of_two(); | ||
if bucket_capacity < MIN_JOIN_HASH_MAP_LEN { | ||
bucket_capacity = MIN_JOIN_HASH_MAP_LEN; | ||
} | ||
let bucket_mask = bucket_capacity - 1; | ||
JoinHashMap { | ||
map: RawTable::with_capacity(capacity), | ||
buckets: vec![0; bucket_capacity], | ||
next: vec![0; capacity], | ||
bucket_mask, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) fn with_capacity(bucket_capacity: usize, capacity: usize) -> Self { | ||
assert!(bucket_capacity > 0); | ||
let bucket_capacity = bucket_capacity.next_power_of_two(); | ||
let bucket_mask = bucket_capacity - 1; | ||
JoinHashMap { | ||
buckets: vec![0; bucket_capacity], | ||
next: vec![0; capacity], | ||
bucket_mask, | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn insert(&mut self, hash_value: u64, index: usize) { | ||
let bucket_index = self.bucket_mask & (hash_value as usize); | ||
// We are sure the `bucket_index` is legal | ||
unsafe { | ||
let index_ref = self.buckets.get_unchecked_mut(bucket_index); | ||
let prev_index = *index_ref; | ||
// chained list at index is already initialized with 0 | ||
// 0 meaning end of list | ||
*index_ref = index as u64 + 1; | ||
if prev_index != 0 { | ||
// Update chained Vec at index with previous value | ||
self.next[index] = prev_index; | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn get_first_index(&self, hash_value: u64) -> u64 { | ||
let bucket_index = self.bucket_mask & (hash_value as usize); | ||
unsafe { *self.buckets.get_unchecked(bucket_index) } | ||
} | ||
} | ||
|
||
impl fmt::Debug for JoinHashMap { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? |
||
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// SymmetricJoinHashMap is similar to JoinHashMap, except that it stores the indices inline, allowing it to mutate | ||
/// and shrink the indices. | ||
pub struct SymmetricJoinHashMap(pub RawTable<(u64, SmallVec<[u64; 1]>)>); | ||
|
||
impl SymmetricJoinHashMap { | ||
pub(crate) fn with_capacity(capacity: usize) -> Self { | ||
Self(RawTable::with_capacity(capacity)) | ||
|
@@ -138,12 +189,6 @@ impl SymmetricJoinHashMap { | |
} | ||
} | ||
|
||
impl fmt::Debug for JoinHashMap { | ||
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn check_filter_expr_contains_sort_information( | ||
expr: &Arc<dyn PhysicalExpr>, | ||
reference: &Arc<dyn PhysicalExpr>, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can limit the unsafe to: