Skip to content

Commit 21edbab

Browse files
committed
IndexMap::insert_unique_unchecked
Insert a key-value pair into the map without checking if the key already exists in the map. This operation is safe if a key does not exist in the map. However, if a key exists in the map already, the behavior is unspecified: this operation may panic, or any following operation with the map may panic or return arbitrary result. This operation is faster than regular insert, because it does not perform lookup before insertion. This operation is useful during initial population of the map. For example, when constructing a map from another map, we know that keys are unique. Simple benchmark of `insert` vs `insert_unique_unchecked` included: ``` test insert ... bench: 14,929 ns/iter (+/- 2,222) test insert_unique_unchecked ... bench: 11,272 ns/iter (+/- 1,172) ```
1 parent 35b36eb commit 21edbab

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

benches/insert_unique_unchecked.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![feature(test)]
2+
3+
extern crate test;
4+
5+
use test::Bencher;
6+
7+
use indexmap::IndexMap;
8+
9+
#[bench]
10+
fn insert(b: &mut Bencher) {
11+
b.iter(|| {
12+
let mut m = IndexMap::with_capacity(1000);
13+
for i in 0..1000 {
14+
m.insert(i, i);
15+
}
16+
m
17+
});
18+
}
19+
20+
#[bench]
21+
fn insert_unique_unchecked(b: &mut Bencher) {
22+
b.iter(|| {
23+
let mut m = IndexMap::with_capacity(1000);
24+
for i in 0..1000 {
25+
m.insert_unique_unchecked(i, i);
26+
}
27+
m
28+
});
29+
}

src/map.rs

+30
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,26 @@ where
373373
self.core.insert_full(hash, key, value)
374374
}
375375

376+
/// Insert a key-value pair into the map without checking
377+
/// if the key already exists in the map.
378+
///
379+
/// This operation is memory-safe.
380+
///
381+
/// However, **if a key exists in the map already, the behavior is unspecified**:
382+
/// this operation may panic, loop forever, or any following operation with the map
383+
/// may panic, loop forever or return arbitrary result. But it won't violate memory safety.
384+
///
385+
/// This operation is faster than regular insert because it does not perform
386+
/// lookup before insertion.
387+
///
388+
/// This operation is useful during initial population of the map.
389+
/// For example, when constructing a map from another map, we know
390+
/// that keys are unique.
391+
pub fn insert_unique_unchecked(&mut self, key: K, value: V) {
392+
let hash = self.hash(&key);
393+
self.core.push(hash, key, value);
394+
}
395+
376396
/// Get the given key’s corresponding entry in the map for insertion and/or
377397
/// in-place manipulation.
378398
///
@@ -1834,4 +1854,14 @@ mod tests {
18341854
assert!(values.contains(&'b'));
18351855
assert!(values.contains(&'c'));
18361856
}
1857+
1858+
#[test]
1859+
fn insert_unique_unchecked() {
1860+
let mut map = IndexMap::new();
1861+
map.insert_unique_unchecked(10, 11);
1862+
map.insert_unique_unchecked(20, 21);
1863+
assert_eq!(Some(&11), map.get(&10));
1864+
assert_eq!(Some(&21), map.get(&20));
1865+
assert_eq!(None, map.get(&30));
1866+
}
18371867
}

src/map/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<K, V> IndexMapCore<K, V> {
209209

210210
/// Append a key-value pair, *without* checking whether it already exists,
211211
/// and return the pair's new index.
212-
fn push(&mut self, hash: HashValue, key: K, value: V) -> usize {
212+
pub(crate) fn push(&mut self, hash: HashValue, key: K, value: V) -> usize {
213213
let i = self.entries.len();
214214
self.indices.insert(hash.get(), i, get_hash(&self.entries));
215215
if i == self.entries.capacity() {

src/set.rs

+19
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ where
277277
self.map.insert(value, ()).is_none()
278278
}
279279

280+
/// Insert a value the set without checking if the value already exists in the set.
281+
///
282+
/// This operation is memory-safe.
283+
///
284+
/// However, **if a value exists in the set already, the behavior is unspecified**:
285+
/// this operation may panic, loop forever, or any following operation with the set
286+
/// may panic, loop forever or return arbitrary result. But it won't violate memory safety.
287+
///
288+
/// This operation is faster than regular insert because it does not perform
289+
/// lookup before insertion.
290+
///
291+
/// This operation is useful during initial population of the set.
292+
/// For example, when constructing a set from another set, we know
293+
/// that values are unique.
294+
#[cfg_attr(feature = "inline-more", inline)]
295+
pub fn insert_unique_unchecked(&mut self, value: T) {
296+
self.map.insert_unique_unchecked(value, ())
297+
}
298+
280299
/// Insert the value into the set, and get its index.
281300
///
282301
/// If an equivalent item already exists in the set, it returns

0 commit comments

Comments
 (0)