Eliminate redundant Clone in hot path

This commit is contained in:
Zejun Zhao 2025-03-05 13:48:30 +08:00 committed by Tate, Hongliang Tian
parent 3ef8f20de6
commit c1e6f9a552
2 changed files with 18 additions and 17 deletions

View File

@ -35,12 +35,11 @@ impl core::fmt::Debug for IdleClassRq {
impl SchedClassRq for IdleClassRq { impl SchedClassRq for IdleClassRq {
fn enqueue(&mut self, entity: Arc<Task>, _: Option<EnqueueFlags>) { fn enqueue(&mut self, entity: Arc<Task>, _: Option<EnqueueFlags>) {
let ptr = Arc::as_ptr(&entity); let old = self.entity.replace(entity);
if let Some(t) = self.entity.replace(entity) debug_assert!(
&& ptr != Arc::as_ptr(&t) old.is_none(),
{ "the length of the idle queue should be no larger than 1"
panic!("Multiple `idle` entities spawned") );
}
} }
fn len(&self) -> usize { fn len(&self) -> usize {
@ -52,7 +51,7 @@ impl SchedClassRq for IdleClassRq {
} }
fn pick_next(&mut self) -> Option<Arc<Task>> { fn pick_next(&mut self) -> Option<Arc<Task>> {
self.entity.clone() self.entity.take()
} }
fn update_current(&mut self, _: &CurrentRuntime, _: &SchedAttr, _flags: UpdateFlags) -> bool { fn update_current(&mut self, _: &CurrentRuntime, _: &SchedAttr, _flags: UpdateFlags) -> bool {

View File

@ -14,18 +14,18 @@ use super::{CurrentRuntime, SchedAttr, SchedClassRq};
/// This is a singleton class, meaning that only one thread can be in this class at a time. /// This is a singleton class, meaning that only one thread can be in this class at a time.
/// This is used for the most critical tasks, such as powering off and rebooting. /// This is used for the most critical tasks, such as powering off and rebooting.
pub(super) struct StopClassRq { pub(super) struct StopClassRq {
thread: Option<Arc<Task>>, entity: Option<Arc<Task>>,
} }
impl StopClassRq { impl StopClassRq {
pub fn new() -> Self { pub fn new() -> Self {
Self { thread: None } Self { entity: None }
} }
} }
impl core::fmt::Debug for StopClassRq { impl core::fmt::Debug for StopClassRq {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.thread.is_some() { if self.entity.is_some() {
write!(f, "Stop: occupied")?; write!(f, "Stop: occupied")?;
} else { } else {
write!(f, "Stop: empty")?; write!(f, "Stop: empty")?;
@ -35,10 +35,12 @@ impl core::fmt::Debug for StopClassRq {
} }
impl SchedClassRq for StopClassRq { impl SchedClassRq for StopClassRq {
fn enqueue(&mut self, thread: Arc<Task>, _: Option<EnqueueFlags>) { fn enqueue(&mut self, entity: Arc<Task>, _: Option<EnqueueFlags>) {
if self.thread.replace(thread).is_some() { let old = self.entity.replace(entity);
panic!("Multiple `stop` threads spawned") debug_assert!(
} old.is_none(),
"the length of the stop queue should be no larger than 1"
);
} }
fn len(&self) -> usize { fn len(&self) -> usize {
@ -46,15 +48,15 @@ impl SchedClassRq for StopClassRq {
} }
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.thread.is_none() self.entity.is_none()
} }
fn pick_next(&mut self) -> Option<Arc<Task>> { fn pick_next(&mut self) -> Option<Arc<Task>> {
self.thread.take() self.entity.take()
} }
fn update_current(&mut self, _: &CurrentRuntime, _: &SchedAttr, _flags: UpdateFlags) -> bool { fn update_current(&mut self, _: &CurrentRuntime, _: &SchedAttr, _flags: UpdateFlags) -> bool {
// Stop threads has the lowest priority value. They should never be preempted. // Stop entities has the lowest priority value. They should never be preempted.
false false
} }
} }