@@ -112,26 +112,32 @@ impl JobCoresInner {
112112 // This wraps around in the 0 case, so the partition point is simply
113113 // moved to the end of the ring buffer.
114114
115- let steal_from = self . next_core . checked_sub ( 1 ) . unwrap_or ( self . cores . len ( ) - 1 ) ;
115+ let steal_from_index = self . next_core . checked_sub ( 1 ) . unwrap_or ( self . cores . len ( ) - 1 ) ;
116116
117- if let Ok ( [ ( _, core) , ( _, steal_from) ] ) = self . cores . get_disjoint_indices_mut ( [ core_index, steal_from] ) {
117+ // if this core was already at `next_core - 1`, we don't need to steal from anywhere
118+ let ( core, steal_from) = match self . cores . get_disjoint_indices_mut ( [ core_index, steal_from_index] ) {
119+ Ok ( [ ( _, core) , ( _, steal_from) ] ) => ( core, Some ( steal_from) ) ,
120+ Err ( _) => ( & mut self . cores [ core_index] , None ) ,
121+ } ;
122+
123+ let pos = core. jobs . iter ( ) . position ( |x| * x == id) . unwrap ( ) ;
124+ core. jobs . remove ( pos) ;
125+
126+ if let Some ( steal_from) = steal_from {
118127 // This unwrap will never fail, since cores below `next_core` always have
119128 // at least 1 thread on them. Edge case: if `next_core` is 0, `steal_from`
120129 // would wrap around to the end - but when `next_core` is 0, every core has
121130 // the same number of threads; so, if the last core is empty, all the cores
122131 // would be empty, but we know that's impossible because we're deallocating
123132 // a thread right now.
124133 let stolen = steal_from. jobs . pop ( ) . unwrap ( ) ;
125-
126- let pos = core. jobs . iter ( ) . position ( |x| * x == id) . unwrap ( ) ;
127- core. jobs [ pos] = stolen;
128-
134+ // the way we pop and push here means that older job threads will be less
135+ // likely to be repinned, while younger ones are liable to bounce around.
136+ core. jobs . push ( stolen) ;
129137 self . job_threads [ & stolen] . send_replace ( core_id) ;
130- } else {
131- // this core was already at `next_core - 1` - nothing needs to be done!
132138 }
133139
134- self . next_core = steal_from ;
140+ self . next_core = steal_from_index ;
135141 }
136142}
137143
0 commit comments