Skip to content

Commit fc4748c

Browse files
committed
make EpollInterestTable sorted to avoid linear scan
1 parent 7b4802d commit fc4748c

File tree

1 file changed

+6
-5
lines changed
  • src/tools/miri/src/shims/unix/linux_like

1 file changed

+6
-5
lines changed

src/tools/miri/src/shims/unix/linux_like/epoll.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl UnixFileDescription for Epoll {}
164164

165165
/// The table of all EpollEventInterest.
166166
/// This tracks, for each file description, which epoll instances have an interest in events
167-
/// for this file description.
167+
/// for this file description. The `Vec` is sorted by `addr`!
168168
pub struct EpollInterestTable(BTreeMap<FdId, Vec<WeakFileDescriptionRef<Epoll>>>);
169169

170170
impl EpollInterestTable {
@@ -174,15 +174,16 @@ impl EpollInterestTable {
174174

175175
fn insert(&mut self, id: FdId, epoll: WeakFileDescriptionRef<Epoll>) {
176176
let epolls = self.0.entry(id).or_default();
177-
epolls.push(epoll);
177+
let idx = epolls
178+
.binary_search_by_key(&epoll.addr(), |e| e.addr())
179+
.expect_err("trying to add an epoll that's already in the list");
180+
epolls.insert(idx, epoll);
178181
}
179182

180183
fn remove(&mut self, id: FdId, epoll_addr: usize) {
181184
let epolls = self.0.entry(id).or_default();
182-
// FIXME: linear scan. Keep the list sorted so we can do binary search?
183185
let idx = epolls
184-
.iter()
185-
.position(|old_ref| old_ref.addr() == epoll_addr)
186+
.binary_search_by_key(&epoll_addr, |e| e.addr())
186187
.expect("trying to remove an epoll that's not in the list");
187188
epolls.remove(idx);
188189
}

0 commit comments

Comments
 (0)