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

Minor: Refactor memory size estimation for HashTable #10748

Merged
merged 8 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
123 changes: 123 additions & 0 deletions datafusion/common/src/utils/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//! This module provides a function to estimate the memory size of a HashTable prior to alloaction

/// Estimates the memory size required for a hash table prior to allocation.
///
/// # Parameters
/// - `num_elements`: The number of elements expected in the hash table.
/// - `fixed_size`: A fixed overhead size associated with the collection
marvinlanhenke marked this conversation as resolved.
Show resolved Hide resolved
/// (e.g., HashSet or HashTable).
/// - `T`: The type of elements stored in the hash table.
///
/// # Details
/// This function calculates the estimated memory size by considering:
/// - An overestimation of buckets to keep approximately 1/8 of them empty.
/// - The total memory size is computed as:
/// - The size of each entry (`T`) multiplied by the estimated number of
/// buckets.
/// - One byte overhead for each bucket.
/// - The fixed size overhead of the collection.
/// - Returns `usize::MAX` if an overflow occurs.
///
/// # Examples
/// ---
///
/// ## From within a struct
marvinlanhenke marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```rust
/// use datafusion_common::utils::memory::estimate_memory_size;
///
/// struct MyStruct<T> {
/// values: Vec<T>,
/// other_data: usize,
/// }
///
/// impl<T> MyStruct<T> {
/// fn size(&self) -> usize {
/// let num_elements = self.values.len();
/// let fixed_size = std::mem::size_of_val(self) +
/// std::mem::size_of_val(&self.values);
///
/// estimate_memory_size::<T>(num_elements, fixed_size)
/// }
/// }
/// ```
/// ---
/// ## With a simple collection
///
/// ```rust
/// use datafusion_common::utils::memory::estimate_memory_size;
/// use std::collections::HashMap;
///
/// let num_rows = 100;
/// let fixed_size = std::mem::size_of::<HashMap<u64, u64>>();
/// let estimated_hashtable_size =
/// estimate_memory_size::<(u64, u64)>(num_rows,fixed_size);
/// ```
pub fn estimate_memory_size<T>(num_elements: usize, fixed_size: usize) -> usize {
// For the majority of cases hashbrown overestimates the bucket quantity
// to keep ~1/8 of them empty. We take this factor into account by
// multiplying the number of elements with a fixed ratio of 8/7 (~1.14).
// This formula leads to overallocation for small tables (< 8 elements)
// but should be fine overall.
num_elements
.checked_mul(8)
.and_then(|estimate| estimate.checked_div(7))
.map(|buckets| buckets.next_power_of_two())
.and_then(|estimated_buckets| {
// + size of entry * number of buckets
// + 1 byte for each bucket
// + fixed size of collection (HashSet/HashTable)
std::mem::size_of::<T>()
.checked_mul(estimated_buckets)
.and_then(|size_of_entries| {
estimated_buckets
.checked_add(fixed_size)
.and_then(|overhead| size_of_entries.checked_add(overhead))
})
})
marvinlanhenke marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or(usize::MAX)
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;

use super::estimate_memory_size;

#[test]
fn test_estimate_memory() {
let num_elements = 8;
// size: 48
let fixed_size = std::mem::size_of::<HashSet<u32>>();
// size: 128 = 16 * 4 + 16 + 48
marvinlanhenke marked this conversation as resolved.
Show resolved Hide resolved
let estimated = estimate_memory_size::<u32>(num_elements, fixed_size);

assert_eq!(estimated, 128);
marvinlanhenke marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn test_estimate_memory_overflow() {
let num_elements = usize::MAX;
marvinlanhenke marked this conversation as resolved.
Show resolved Hide resolved
let fixed_size = std::mem::size_of::<HashSet<u32>>();
let estimated = estimate_memory_size::<u32>(num_elements, fixed_size);

assert_eq!(estimated, usize::MAX);
}
}
1 change: 1 addition & 0 deletions datafusion/common/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

//! This module provides the bisect function, which implements binary search.

pub mod memory;
pub mod proxy;

use crate::error::{_internal_datafusion_err, _internal_err};
Expand Down
35 changes: 11 additions & 24 deletions datafusion/physical-expr/src/aggregate/count_distinct/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use arrow_schema::DataType;

use datafusion_common::cast::{as_list_array, as_primitive_array};
use datafusion_common::utils::array_into_list_array;
use datafusion_common::utils::memory::estimate_memory_size;
use datafusion_common::ScalarValue;
use datafusion_expr::Accumulator;

Expand Down Expand Up @@ -115,18 +116,11 @@ where
}

fn size(&self) -> usize {
let estimated_buckets = (self.values.len().checked_mul(8).unwrap_or(usize::MAX)
/ 7)
.next_power_of_two();

// Size of accumulator
// + size of entry * number of buckets
// + 1 byte for each bucket
// + fixed size of HashSet
std::mem::size_of_val(self)
+ std::mem::size_of::<T::Native>() * estimated_buckets
+ estimated_buckets
+ std::mem::size_of_val(&self.values)
let num_elements = self.values.len();
let fixed_size =
std::mem::size_of_val(self) + std::mem::size_of_val(&self.values);

estimate_memory_size::<T::Native>(num_elements, fixed_size)
}
}

Expand Down Expand Up @@ -202,17 +196,10 @@ where
}

fn size(&self) -> usize {
let estimated_buckets = (self.values.len().checked_mul(8).unwrap_or(usize::MAX)
/ 7)
.next_power_of_two();

// Size of accumulator
// + size of entry * number of buckets
// + 1 byte for each bucket
// + fixed size of HashSet
std::mem::size_of_val(self)
+ std::mem::size_of::<T::Native>() * estimated_buckets
+ estimated_buckets
+ std::mem::size_of_val(&self.values)
let num_elements = self.values.len();
let fixed_size =
std::mem::size_of_val(self) + std::mem::size_of_val(&self.values);

estimate_memory_size::<T::Native>(num_elements, fixed_size)
}
}
25 changes: 7 additions & 18 deletions datafusion/physical-plan/src/joins/hash_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! [`HashJoinExec`] Partitioned Hash Join Operator

use std::fmt;
use std::mem::size_of;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::Poll;
Expand Down Expand Up @@ -59,6 +58,7 @@ use arrow::record_batch::RecordBatch;
use arrow::util::bit_util;
use arrow_array::cast::downcast_array;
use arrow_schema::ArrowError;
use datafusion_common::utils::memory::estimate_memory_size;
use datafusion_common::{
internal_datafusion_err, internal_err, plan_err, project_schema, DataFusionError,
JoinSide, JoinType, Result,
Expand Down Expand Up @@ -875,23 +875,12 @@ async fn collect_left_input(

// Estimation of memory size, required for hashtable, prior to allocation.
// Final result can be verified using `RawTable.allocation_info()`
//
// For majority of cases hashbrown overestimates buckets qty to keep ~1/8 of them empty.
// This formula leads to overallocation for small tables (< 8 elements) but fine overall.
let estimated_buckets = (num_rows.checked_mul(8).ok_or_else(|| {
alamb marked this conversation as resolved.
Show resolved Hide resolved
DataFusionError::Execution(
"usize overflow while estimating number of hasmap buckets".to_string(),
)
})? / 7)
.next_power_of_two();
// 16 bytes per `(u64, u64)`
// + 1 byte for each bucket
// + fixed size of JoinHashMap (RawTable + Vec)
let estimated_hastable_size =
16 * estimated_buckets + estimated_buckets + size_of::<JoinHashMap>();

reservation.try_grow(estimated_hastable_size)?;
metrics.build_mem_used.add(estimated_hastable_size);
let fixed_size = std::mem::size_of::<JoinHashMap>();
let estimated_hashtable_size =
estimate_memory_size::<(u64, u64)>(num_rows, fixed_size);

reservation.try_grow(estimated_hashtable_size)?;
metrics.build_mem_used.add(estimated_hashtable_size);

let mut hashmap = JoinHashMap::with_capacity(num_rows);
let mut hashes_buffer = Vec::new();
Expand Down