@@ -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`!
168168pub struct EpollInterestTable ( BTreeMap < FdId , Vec < WeakFileDescriptionRef < Epoll > > > ) ;
169169
170170impl 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