Skip to content

Commit c0efd2a

Browse files
committed
Prefer partition_point to look up assoc items
1 parent 9fef8d9 commit c0efd2a

File tree

1 file changed

+5
-47
lines changed

1 file changed

+5
-47
lines changed

compiler/rustc_data_structures/src/sorted_map/index_map.rs

+5-47
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
9494
Q: Ord + ?Sized,
9595
K: Borrow<Q>,
9696
{
97-
// FIXME: This should be in the standard library as `equal_range`. See rust-lang/rfcs#2184.
9897
match self.binary_search_idx(key) {
9998
Err(_) => self.idxs_to_items_enumerated(&[]),
10099

101100
Ok(idx) => {
102-
let start = self.find_lower_bound(key, idx);
103-
let end = self.find_upper_bound(key, idx);
101+
let start = self.idx_sorted_by_item_key[..idx]
102+
.partition_point(|&i| self.items[i].0.borrow() != key);
103+
let end = idx
104+
+ self.idx_sorted_by_item_key[idx..]
105+
.partition_point(|&i| self.items[i].0.borrow() == key);
104106
self.idxs_to_items_enumerated(&self.idx_sorted_by_item_key[start..end])
105107
}
106108
}
@@ -114,50 +116,6 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
114116
self.idx_sorted_by_item_key.binary_search_by(|&idx| self.items[idx].0.borrow().cmp(key))
115117
}
116118

117-
/// Returns the index into the `idx_sorted_by_item_key` array of the first item equal to
118-
/// `key`.
119-
///
120-
/// `initial` must be an index into that same array for an item that is equal to `key`.
121-
fn find_lower_bound<Q>(&self, key: &Q, initial: usize) -> usize
122-
where
123-
Q: Ord + ?Sized,
124-
K: Borrow<Q>,
125-
{
126-
debug_assert!(self.items[self.idx_sorted_by_item_key[initial]].0.borrow() == key);
127-
128-
// FIXME: At present, this uses linear search, meaning lookup is only `O(log n)` if duplicate
129-
// entries are rare. It would be better to start with a linear search for the common case but
130-
// fall back to an exponential search if many duplicates are found. This applies to
131-
// `upper_bound` as well.
132-
let mut start = initial;
133-
while start != 0 && self.items[self.idx_sorted_by_item_key[start - 1]].0.borrow() == key {
134-
start -= 1;
135-
}
136-
137-
start
138-
}
139-
140-
/// Returns the index into the `idx_sorted_by_item_key` array of the first item greater than
141-
/// `key`, or `self.len()` if no such item exists.
142-
///
143-
/// `initial` must be an index into that same array for an item that is equal to `key`.
144-
fn find_upper_bound<Q>(&self, key: &Q, initial: usize) -> usize
145-
where
146-
Q: Ord + ?Sized,
147-
K: Borrow<Q>,
148-
{
149-
debug_assert!(self.items[self.idx_sorted_by_item_key[initial]].0.borrow() == key);
150-
151-
// See the FIXME for `find_lower_bound`.
152-
let mut end = initial + 1;
153-
let len = self.items.len();
154-
while end < len && self.items[self.idx_sorted_by_item_key[end]].0.borrow() == key {
155-
end += 1;
156-
}
157-
158-
end
159-
}
160-
161119
fn idxs_to_items_enumerated(&'a self, idxs: &'a [I]) -> impl 'a + Iterator<Item = (I, &'a V)> {
162120
idxs.iter().map(move |&idx| (idx, &self.items[idx].1))
163121
}

0 commit comments

Comments
 (0)