Skip to content

Commit 8d6e14d

Browse files
committed
Auto merge of #327 - JustForFun88:master, r=Amanieu
Removing additional third copy operation for OccupiedEntry::insert Removing unnecessary additional third copy operation for `OccupiedEntry::insert` and `OccupiedEntryRef::insert` because with `swap` we have three operation: 1. `Read` from `x` to the third value `z`, 2. `Copy` from `y` to `x`, 3. `Copy` from `z` to the `y` value. But when we use `replace` funtion we actually do only first two operation and immediatly return z value and skip third `copy` operation
2 parents 31a1630 + 0f1bc99 commit 8d6e14d

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/map.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,10 +3587,8 @@ impl<'a, K, V, S, A: Allocator + Clone> OccupiedEntry<'a, K, V, S, A> {
35873587
/// assert_eq!(map["poneyland"], 15);
35883588
/// ```
35893589
#[cfg_attr(feature = "inline-more", inline)]
3590-
pub fn insert(&mut self, mut value: V) -> V {
3591-
let old_value = self.get_mut();
3592-
mem::swap(&mut value, old_value);
3593-
value
3590+
pub fn insert(&mut self, value: V) -> V {
3591+
mem::replace(self.get_mut(), value)
35943592
}
35953593

35963594
/// Takes the value out of the entry, and returns it.
@@ -4256,10 +4254,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator + Clone> OccupiedEntryRef<'a, 'b,
42564254
/// assert_eq!(map["poneyland"], 15);
42574255
/// ```
42584256
#[cfg_attr(feature = "inline-more", inline)]
4259-
pub fn insert(&mut self, mut value: V) -> V {
4260-
let old_value = self.get_mut();
4261-
mem::swap(&mut value, old_value);
4262-
value
4257+
pub fn insert(&mut self, value: V) -> V {
4258+
mem::replace(self.get_mut(), value)
42634259
}
42644260

42654261
/// Takes the value out of the entry, and returns it.

0 commit comments

Comments
 (0)