Skip to content

Commit 90c45fd

Browse files
committed
IndexMap::insert_no_overwrite_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_no_overwrite_unchecked` included: ``` test insert ... bench: 14,112 ns/iter (+/- 1,653) test insert_no_overwrite_unchecked ... bench: 11,099 ns/iter (+/- 1,100) ```
1 parent 35b36eb commit 90c45fd

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
Lines changed: 29 additions & 0 deletions
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_no_overwrite_unchecked(b: &mut Bencher) {
22+
b.iter(|| {
23+
let mut m = IndexMap::with_capacity(1000);
24+
for i in 0..1000 {
25+
m.insert_no_overwrite_unchecked(i, i);
26+
}
27+
m
28+
});
29+
}

src/map.rs

Lines changed: 30 additions & 0 deletions
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 safe if a key does not exist in the map.
380+
///
381+
/// However, if a key exists in the map already, the behavior is unspecified:
382+
/// this operation may panic, or any following operation with the map may panic
383+
/// or return arbitrary result.
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_no_overwrite_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_no_overwrite_unchecked() {
1860+
let mut map = IndexMap::new();
1861+
map.insert_no_overwrite_unchecked(10, 11);
1862+
map.insert_no_overwrite_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

Lines changed: 1 addition & 1 deletion
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() {

0 commit comments

Comments
 (0)