This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12057-b010f53dffad838e137423e81980db3d9e36c26c in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 0cb6ae182b283809e161f5d7899d72c0626bb9a4 Author: Matthew Katz <[email protected]> AuthorDate: Wed Nov 4 03:59:09 2026 +0000 Use `ahash` and a single lookup in `PrimitiveDictionaryBuilder` (#12057) # Which issue does this PR close? Closes #12055 # Rationale for this change `PrimitiveDictionaryBuilder` hashes with `SipHash` and does a `get` followed by an `insert` for each new value. Using `ahash` and hashing values to be added only once improves performance significantly. `cast` to a primitive dictionary goes through this builder, so casts get faster as well # What changes are included in this PR? - Hash with `ahash::RandomState`, which arrow-array already depends on and `GenericByteDictionaryBuilder` uses. - Look up each value once with the entry API. - Hash a value's bytes without the slice length. # Are these changes tested? Covered by the existing tests. The benchmark is added in #12060. Results with Int32 keys on an Apple M5 Max: | distinct values / rows | UInt32 | UInt64 | Decimal128 | |---|---|---|---| | 20 / 1,000 | −63% | −68% | −70% | | 100 / 10,000 | −60% | −64% | −70% | | 10,000 / 100,000 | −62% | −69% | −69% | # Are there any significant user-facing changes? No. This PR was drafted with AI assistance. I reviewed all changes and take responsibility for them. --------- Signed-off-by: Matt Katz <[email protected]> Co-authored-by: RIchard Baah <[email protected]> --- .../src/builder/primitive_dictionary_builder.rs | 27 +++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/arrow-array/src/builder/primitive_dictionary_builder.rs b/arrow-array/src/builder/primitive_dictionary_builder.rs index f2ab6a49d3..46ee4fd5c9 100644 --- a/arrow-array/src/builder/primitive_dictionary_builder.rs +++ b/arrow-array/src/builder/primitive_dictionary_builder.rs @@ -25,6 +25,7 @@ use arrow_schema::{ArrowError, DataType}; use num_traits::NumCast; use std::any::Any; use std::collections::HashMap; +use std::collections::hash_map::Entry; use std::sync::Arc; /// Wraps a type implementing `ToByteSlice` implementing `Hash` and `Eq` for it @@ -35,7 +36,7 @@ struct Value<T>(T); impl<T: ToByteSlice> std::hash::Hash for Value<T> { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { - self.0.to_byte_slice().hash(state) + state.write(self.0.to_byte_slice()) } } @@ -87,7 +88,7 @@ where { keys_builder: PrimitiveBuilder<K>, values_builder: PrimitiveBuilder<V>, - map: HashMap<Value<V::Native>, usize>, + map: HashMap<Value<V::Native>, usize, ahash::RandomState>, } impl<K, V> Default for PrimitiveDictionaryBuilder<K, V> @@ -110,7 +111,7 @@ where Self { keys_builder: PrimitiveBuilder::new(), values_builder: PrimitiveBuilder::new(), - map: HashMap::new(), + map: HashMap::default(), } } @@ -131,7 +132,7 @@ where Self { keys_builder, values_builder, - map: HashMap::with_capacity(values_capacity), + map: HashMap::with_capacity_and_hasher(values_capacity, Default::default()), } } @@ -146,7 +147,7 @@ where ) -> Self { let keys = keys_builder.values_slice(); let values = values_builder.values_slice(); - let mut map = HashMap::with_capacity(values.len()); + let mut map = HashMap::with_capacity_and_hasher(values.len(), Default::default()); keys.iter().zip(values.iter()).for_each(|(key, value)| { map.insert(Value(*value), K::Native::to_usize(*key).unwrap()); @@ -167,7 +168,7 @@ where Self { keys_builder: PrimitiveBuilder::with_capacity(keys_capacity), values_builder: PrimitiveBuilder::with_capacity(values_capacity), - map: HashMap::with_capacity(values_capacity), + map: HashMap::with_capacity_and_hasher(values_capacity, Default::default()), } } @@ -286,17 +287,15 @@ where { #[inline] fn get_or_insert_key(&mut self, value: V::Native) -> Result<K::Native, ArrowError> { - match self.map.get(&Value(value)) { - Some(&key) => { - Ok(K::Native::from_usize(key).ok_or(ArrowError::DictionaryKeyOverflowError)?) - } - None => { + let key = match self.map.entry(Value(value)) { + Entry::Occupied(entry) => *entry.get(), + Entry::Vacant(entry) => { let key = self.values_builder.len(); self.values_builder.append_value(value); - self.map.insert(Value(value), key); - Ok(K::Native::from_usize(key).ok_or(ArrowError::DictionaryKeyOverflowError)?) + *entry.insert(key) } - } + }; + K::Native::from_usize(key).ok_or(ArrowError::DictionaryKeyOverflowError) } /// Append a primitive value to the array. Return an existing index
