-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Minor: Refactor memory size estimation for HashTable #10748
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
176b257
refactor: extract estimate_memory_size
marvinlanhenke b555adc
refactor: cap at usize::MAX
marvinlanhenke a719511
refactor: use estimate_memory_size
marvinlanhenke 93de4ac
chore: add examples
marvinlanhenke 97906ec
refactor: return Result<usize>; add testcase
marvinlanhenke 75b5892
fix: docs
marvinlanhenke 33d24d1
fix: remove unneccessary checked_div
marvinlanhenke eebf4cf
fix: remove additional and_then
marvinlanhenke 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // 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 | ||
| use crate::{DataFusionError, Result}; | ||
|
|
||
| /// 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 | ||
| /// (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. | ||
| /// - If the estimation overflows, we return a [`DataFusionError`] | ||
| /// | ||
| /// # Examples | ||
| /// --- | ||
| /// | ||
| /// ## From within a struct | ||
marvinlanhenke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// ```rust | ||
| /// # use datafusion_common::utils::memory::estimate_memory_size; | ||
| /// # use datafusion_common::Result; | ||
| /// | ||
| /// struct MyStruct<T> { | ||
| /// values: Vec<T>, | ||
| /// other_data: usize, | ||
| /// } | ||
| /// | ||
| /// impl<T> MyStruct<T> { | ||
| /// fn size(&self) -> Result<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) | ||
| /// .expect("Size estimation failed"); | ||
| /// ``` | ||
| pub fn estimate_memory_size<T>(num_elements: usize, fixed_size: usize) -> Result<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(|overestimate| { | ||
| let estimated_buckets = (overestimate / 7).next_power_of_two(); | ||
marvinlanhenke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // + 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)? | ||
| .checked_add(estimated_buckets)? | ||
| .checked_add(fixed_size) | ||
| }) | ||
| .ok_or_else(|| { | ||
| DataFusionError::Execution( | ||
| "usize overflow while estimating the number of buckets".to_string(), | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::HashSet; | ||
|
|
||
| use super::estimate_memory_size; | ||
|
|
||
| #[test] | ||
| fn test_estimate_memory() { | ||
| // size (bytes): 48 | ||
| let fixed_size = std::mem::size_of::<HashSet<u32>>(); | ||
|
|
||
| // estimated buckets: 16 = (8 * 8 / 7).next_power_of_two() | ||
| let num_elements = 8; | ||
| // size (bytes): 128 = 16 * 4 + 16 + 48 | ||
| let estimated = estimate_memory_size::<u32>(num_elements, fixed_size).unwrap(); | ||
| assert_eq!(estimated, 128); | ||
marvinlanhenke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // estimated buckets: 64 = (40 * 8 / 7).next_power_of_two() | ||
| let num_elements = 40; | ||
| // size (bytes): 368 = 64 * 4 + 64 + 48 | ||
| let estimated = estimate_memory_size::<u32>(num_elements, fixed_size).unwrap(); | ||
| assert_eq!(estimated, 368); | ||
| } | ||
|
|
||
| #[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!(estimated.is_err()); | ||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.