Skip to content

Commit 8d6c278

Browse files
committed
insert_unique_unchecked operation
Sometimes a map is constructed when it is known that all keys are unique (e. e. if keys are coming from another map or from a sorted/deduplicated iterator). In this case we can make insertion faster by skipping a check that a key already exists in the map. `insert_unique_unchecked` is guaranteed to be memory-safe, but does not guarantee anything beyond that: if inserted key is not unique, `HashMap` can panic, loop forever, return incorrect entry etc. Added simple benchmark. `insert_unique_unchecked` is about 30% faster than `insert`. Your mileage may vary of course. Similar PR was [added to `indexmap` crate](indexmap-rs/indexmap#200) and they asked to discuss the name of the operation with `hashbrown` crate owners to come to the same naming convention (if `hashbrown` is willing to have the same operation).
1 parent 728f9e8 commit 8d6c278

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

benches/insert_unique_unchecked.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Compare `insert` and `insert_unique_unchecked` operations performance.
2+
3+
#![feature(test)]
4+
5+
extern crate test;
6+
7+
use hashbrown::HashMap;
8+
use test::Bencher;
9+
10+
#[bench]
11+
fn insert(b: &mut Bencher) {
12+
let keys: Vec<String> = (0..1000).map(|i| format!("xxxx{}yyyy", i)).collect();
13+
b.iter(|| {
14+
let mut m = HashMap::with_capacity(1000);
15+
for k in &keys {
16+
m.insert(k, k);
17+
}
18+
m
19+
});
20+
}
21+
22+
#[bench]
23+
fn insert_unique_unchecked(b: &mut Bencher) {
24+
let keys: Vec<String> = (0..1000).map(|i| format!("xxxx{}yyyy", i)).collect();
25+
b.iter(|| {
26+
let mut m = HashMap::with_capacity(1000);
27+
for k in &keys {
28+
m.insert_unique_unchecked(k, k);
29+
}
30+
m
31+
});
32+
}

src/map.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,31 @@ where
12781278
}
12791279
}
12801280

1281+
/// Insert a key-value pair into the map without checking
1282+
/// if the key already exists in the map.
1283+
///
1284+
/// This operation is safe if a key does not exist in the map.
1285+
///
1286+
/// However, if a key exists in the map already, the behavior is unspecified:
1287+
/// this operation may panic, loop forever, or any following operation with the map
1288+
/// may panic, loop forever or return arbitrary result.
1289+
///
1290+
/// That said, this operation (and following operations) are guaranteed to
1291+
/// not violate memory safety.
1292+
///
1293+
/// This operation is faster than regular insert, because it does not perform
1294+
/// lookup before insertion.
1295+
///
1296+
/// This operation is useful during initial population of the map.
1297+
/// For example, when constructing a map from another map, we know
1298+
/// that keys are unique.
1299+
#[cfg_attr(feature = "inline-more", inline)]
1300+
pub fn insert_unique_unchecked(&mut self, k: K, v: V) {
1301+
let hash = make_insert_hash::<K, S>(&self.hash_builder, &k);
1302+
self.table
1303+
.insert(hash, (k, v), make_hasher::<K, _, V, S>(&self.hash_builder));
1304+
}
1305+
12811306
/// Tries to insert a key-value pair into the map, and returns
12821307
/// a mutable reference to the value in the entry.
12831308
///
@@ -3898,6 +3923,16 @@ mod test_map {
38983923
assert_eq!(*m.get(&5).unwrap(), 3);
38993924
}
39003925

3926+
#[test]
3927+
fn test_insert_unique_unchecked() {
3928+
let mut map = HashMap::new();
3929+
map.insert_unique_unchecked(10, 11);
3930+
map.insert_unique_unchecked(20, 21);
3931+
assert_eq!(Some(&11), map.get(&10));
3932+
assert_eq!(Some(&21), map.get(&20));
3933+
assert_eq!(None, map.get(&30));
3934+
}
3935+
39013936
#[test]
39023937
fn test_is_empty() {
39033938
let mut m = HashMap::with_capacity(4);

src/set.rs

+22
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,28 @@ where
991991
self.map.insert(value, ()).is_none()
992992
}
993993

994+
/// Insert a value the set without checking if the value already exists in the set.
995+
///
996+
/// This operation is safe if a value does not exist in the set.
997+
///
998+
/// However, if a value exists in the set already, the behavior is unspecified:
999+
/// this operation may panic, loop forever, or any following operation with the set
1000+
/// may panic, loop forever or return arbitrary result.
1001+
///
1002+
/// That said, this operation (and following operations) are guaranteed to
1003+
/// not violate memory safety.
1004+
///
1005+
/// This operation is faster than regular insert, because it does not perform
1006+
/// lookup before insertion.
1007+
///
1008+
/// This operation is useful during initial population of the set.
1009+
/// For example, when constructing a set from another set, we know
1010+
/// that values are unique.
1011+
#[cfg_attr(feature = "inline-more", inline)]
1012+
pub fn insert_unique_unchecked(&mut self, value: T) {
1013+
self.map.insert_unique_unchecked(value, ());
1014+
}
1015+
9941016
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
9951017
/// one. Returns the replaced value.
9961018
///

0 commit comments

Comments
 (0)