Skip to content

Commit dd3b2d8

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 dd3b2d8

File tree

4 files changed

+89
-1
lines changed

4 files changed

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

+35
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,31 @@ 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 safe if a key does not exist in the map.
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.
386+
///
387+
/// That said, this operation (and following operations) are guaranteed to
388+
/// not violate memory safety.
389+
///
390+
/// This operation is faster than regular insert, because it does not perform
391+
/// lookup before insertion.
392+
///
393+
/// This operation is useful during initial population of the map.
394+
/// For example, when constructing a map from another map, we know
395+
/// that keys are unique.
396+
pub fn insert_unique_unchecked(&mut self, key: K, value: V) {
397+
let hash = self.hash(&key);
398+
self.core.push(hash, key, value);
399+
}
400+
376401
/// Get the given key’s corresponding entry in the map for insertion and/or
377402
/// in-place manipulation.
378403
///
@@ -1834,4 +1859,14 @@ mod tests {
18341859
assert!(values.contains(&'b'));
18351860
assert!(values.contains(&'c'));
18361861
}
1862+
1863+
#[test]
1864+
fn insert_unique_unchecked() {
1865+
let mut map = IndexMap::new();
1866+
map.insert_unique_unchecked(10, 11);
1867+
map.insert_unique_unchecked(20, 21);
1868+
assert_eq!(Some(&11), map.get(&10));
1869+
assert_eq!(Some(&21), map.get(&20));
1870+
assert_eq!(None, map.get(&30));
1871+
}
18371872
}

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

+24
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,30 @@ 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+
/// Returns a reference to the value just inserted.
283+
///
284+
/// This operation is safe if a value does not exist in the set.
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.
289+
///
290+
/// That said, this operation (and following operations) are guaranteed to
291+
/// not violate memory safety.
292+
///
293+
/// This operation is faster than regular insert, because it does not perform
294+
/// lookup before insertion.
295+
///
296+
/// This operation is useful during initial population of the set.
297+
/// For example, when constructing a set from another set, we know
298+
/// that values are unique.
299+
#[cfg_attr(feature = "inline-more", inline)]
300+
pub fn insert_unique_unchecked(&mut self, value: T) {
301+
self.map.insert_unique_unchecked(value, ())
302+
}
303+
280304
/// Insert the value into the set, and get its index.
281305
///
282306
/// If an equivalent item already exists in the set, it returns

0 commit comments

Comments
 (0)