Skip to content

Commit 969115b

Browse files
committedAug 22, 2021
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 969115b

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-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

+36
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,30 @@ 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+
/// Returns a reference to the key and value just inserted.
380+
///
381+
/// This operation is memory-safe.
382+
///
383+
/// However, **if a key exists in the map already, the behavior is unspecified**:
384+
/// this operation may panic, loop forever, or any following operation with the map
385+
/// may panic, loop forever or return arbitrary result. But it won't violate memory safety.
386+
///
387+
/// This operation is faster than regular insert because it does not perform
388+
/// lookup before insertion.
389+
///
390+
/// This operation is useful during initial population of the map.
391+
/// For example, when constructing a map from another map, we know
392+
/// that keys are unique.
393+
pub fn insert_unique_unchecked(&mut self, key: K, value: V) -> (&K, &mut V) {
394+
let hash = self.hash(&key);
395+
let index = self.core.push(hash, key, value);
396+
let entry = &mut self.core.as_entries_mut()[index];
397+
(&entry.key, &mut entry.value)
398+
}
399+
376400
/// Get the given key’s corresponding entry in the map for insertion and/or
377401
/// in-place manipulation.
378402
///
@@ -1834,4 +1858,16 @@ mod tests {
18341858
assert!(values.contains(&'b'));
18351859
assert!(values.contains(&'c'));
18361860
}
1861+
1862+
#[test]
1863+
fn insert_unique_unchecked() {
1864+
let mut map = IndexMap::new();
1865+
let (k1, v1) = map.insert_unique_unchecked(10, 11);
1866+
assert_eq!((&10, &mut 11), (k1, v1));
1867+
let (k2, v2) = map.insert_unique_unchecked(20, 21);
1868+
assert_eq!((&20, &mut 21), (k2, v2));
1869+
assert_eq!(Some(&11), map.get(&10));
1870+
assert_eq!(Some(&21), map.get(&20));
1871+
assert_eq!(None, map.get(&30));
1872+
}
18371873
}

‎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

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

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

0 commit comments

Comments
 (0)