Skip to content

Commit 029ba5d

Browse files
authored
Port of Hasbrown #183 "Avoid closures to improve compile times" (#14)
Port of the relevant changes from hashbrown from rust-lang/hashbrown#183
1 parent 898ceeb commit 029ba5d

File tree

2 files changed

+81
-35
lines changed

2 files changed

+81
-35
lines changed

src/map.rs

+66-32
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,11 @@ where
787787
K: Borrow<Q>,
788788
Q: Hash + Eq,
789789
{
790-
self.get_key_value(k).map(|(_, v)| v)
790+
// Avoid `Option::map` because it bloats LLVM IR.
791+
match self.get_key_value(k) {
792+
Some((_, v)) => Some(v),
793+
None => None,
794+
}
791795
}
792796

793797
/// Returns the key-value pair corresponding to the supplied key.
@@ -816,12 +820,14 @@ where
816820
Q: Hash + Eq,
817821
{
818822
let hash = make_hash(&self.hash_builder, k);
819-
self.table
820-
.find(hash, |x| k.eq(x.0.borrow()))
821-
.map(|item| unsafe {
823+
// Avoid `Option::map` because it bloats LLVM IR.
824+
match self.table.find(hash, |x| k.eq(x.0.borrow())) {
825+
Some(item) => unsafe {
822826
let &(ref key, ref value) = item.as_ref();
823-
(key, value)
824-
})
827+
Some((key, value))
828+
},
829+
None => None,
830+
}
825831
}
826832

827833
/// Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.
@@ -854,12 +860,14 @@ where
854860
Q: Hash + Eq,
855861
{
856862
let hash = make_hash(&self.hash_builder, k);
857-
self.table
858-
.find(hash, |x| k.eq(x.0.borrow()))
859-
.map(|item| unsafe {
863+
// Avoid `Option::map` because it bloats LLVM IR.
864+
match self.table.find(hash, |x| k.eq(x.0.borrow())) {
865+
Some(item) => unsafe {
860866
let &mut (ref key, ref mut value) = item.as_mut();
861-
(key, value)
862-
})
867+
Some((key, value))
868+
},
869+
None => None,
870+
}
863871
}
864872

865873
/// Returns `true` if the map contains a value for the specified key.
@@ -918,9 +926,11 @@ where
918926
Q: Hash + Eq,
919927
{
920928
let hash = make_hash(&self.hash_builder, k);
921-
self.table
922-
.find(hash, |x| k.eq(x.0.borrow()))
923-
.map(|item| unsafe { &mut item.as_mut().1 })
929+
// Avoid `Option::map` because it bloats LLVM IR.
930+
match self.table.find(hash, |x| k.eq(x.0.borrow())) {
931+
Some(item) => Some(unsafe { &mut item.as_mut().1 }),
932+
None => None,
933+
}
924934
}
925935

926936
/// Inserts a key-value pair into the map.
@@ -995,7 +1005,11 @@ where
9951005
K: Borrow<Q>,
9961006
Q: Hash + Eq,
9971007
{
998-
self.remove_entry(k).map(|(_, v)| v)
1008+
// Avoid `Option::map` because it bloats LLVM IR.
1009+
match self.remove_entry(k) {
1010+
Some((_, v)) => Some(v),
1011+
None => None,
1012+
}
9991013
}
10001014

10011015
/// Removes a key from the map, returning the stored key and value if the
@@ -1478,13 +1492,13 @@ impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
14781492
where
14791493
F: FnMut(&K) -> bool,
14801494
{
1481-
self.map
1482-
.table
1483-
.find(hash, |(k, _)| is_match(k))
1484-
.map(|item| unsafe {
1495+
match self.map.table.find(hash, |(k, _)| is_match(k)) {
1496+
Some(item) => unsafe {
14851497
let &(ref key, ref value) = item.as_ref();
1486-
(key, value)
1487-
})
1498+
Some((key, value))
1499+
},
1500+
None => None,
1501+
}
14881502
}
14891503

14901504
/// Access an entry by hash.
@@ -1947,10 +1961,14 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
19471961

19481962
#[cfg_attr(feature = "inline-more", inline)]
19491963
fn next(&mut self) -> Option<(&'a K, &'a V)> {
1950-
self.inner.next().map(|x| unsafe {
1951-
let r = x.as_ref();
1952-
(&r.0, &r.1)
1953-
})
1964+
// Avoid `Option::map` because it bloats LLVM IR.
1965+
match self.inner.next() {
1966+
Some(x) => unsafe {
1967+
let r = x.as_ref();
1968+
Some((&r.0, &r.1))
1969+
},
1970+
None => None,
1971+
}
19541972
}
19551973
#[cfg_attr(feature = "inline-more", inline)]
19561974
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -1971,10 +1989,14 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
19711989

19721990
#[cfg_attr(feature = "inline-more", inline)]
19731991
fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
1974-
self.inner.next().map(|x| unsafe {
1975-
let r = x.as_mut();
1976-
(&r.0, &mut r.1)
1977-
})
1992+
// Avoid `Option::map` because it bloats LLVM IR.
1993+
match self.inner.next() {
1994+
Some(x) => unsafe {
1995+
let r = x.as_mut();
1996+
Some((&r.0, &mut r.1))
1997+
},
1998+
None => None,
1999+
}
19782000
}
19792001
#[cfg_attr(feature = "inline-more", inline)]
19802002
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -2030,7 +2052,11 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
20302052

20312053
#[cfg_attr(feature = "inline-more", inline)]
20322054
fn next(&mut self) -> Option<&'a K> {
2033-
self.inner.next().map(|(k, _)| k)
2055+
// Avoid `Option::map` because it bloats LLVM IR.
2056+
match self.inner.next() {
2057+
Some((k, _)) => Some(k),
2058+
None => None,
2059+
}
20342060
}
20352061
#[cfg_attr(feature = "inline-more", inline)]
20362062
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -2050,7 +2076,11 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
20502076

20512077
#[cfg_attr(feature = "inline-more", inline)]
20522078
fn next(&mut self) -> Option<&'a V> {
2053-
self.inner.next().map(|(_, v)| v)
2079+
// Avoid `Option::map` because it bloats LLVM IR.
2080+
match self.inner.next() {
2081+
Some((_, v)) => Some(v),
2082+
None => None,
2083+
}
20542084
}
20552085
#[cfg_attr(feature = "inline-more", inline)]
20562086
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -2070,7 +2100,11 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
20702100

20712101
#[cfg_attr(feature = "inline-more", inline)]
20722102
fn next(&mut self) -> Option<&'a mut V> {
2073-
self.inner.next().map(|(_, v)| v)
2103+
// Avoid `Option::map` because it bloats LLVM IR.
2104+
match self.inner.next() {
2105+
Some((_, v)) => Some(v),
2106+
None => None,
2107+
}
20742108
}
20752109
#[cfg_attr(feature = "inline-more", inline)]
20762110
fn size_hint(&self) -> (usize, Option<usize>) {

src/set.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,11 @@ where
628628
T: Borrow<Q>,
629629
Q: Hash + Eq,
630630
{
631-
self.map.get_key_value(value).map(|(k, _)| k)
631+
// Avoid `Option::map` because it bloats LLVM IR.
632+
match self.map.get_key_value(value) {
633+
Some((k, _)) => Some(k),
634+
None => None,
635+
}
632636
}
633637

634638
/// Returns `true` if `self` has no elements in common with `other`.
@@ -800,7 +804,11 @@ where
800804
T: Borrow<Q>,
801805
Q: Hash + Eq,
802806
{
803-
self.map.remove_entry(value).map(|(k, _)| k)
807+
// Avoid `Option::map` because it bloats LLVM IR.
808+
match self.map.remove_entry(value) {
809+
Some((k, _)) => Some(k),
810+
None => None,
811+
}
804812
}
805813
}
806814

@@ -1188,7 +1196,11 @@ impl<K> Iterator for IntoIter<K> {
11881196

11891197
#[cfg_attr(feature = "inline-more", inline)]
11901198
fn next(&mut self) -> Option<K> {
1191-
self.iter.next().map(|(k, _)| k)
1199+
// Avoid `Option::map` because it bloats LLVM IR.
1200+
match self.iter.next() {
1201+
Some((k, _)) => Some(k),
1202+
None => None,
1203+
}
11921204
}
11931205
#[cfg_attr(feature = "inline-more", inline)]
11941206
fn size_hint(&self) -> (usize, Option<usize>) {

0 commit comments

Comments
 (0)