From c1e6f9a552567133e1a75393d9b864bec56e5895 Mon Sep 17 00:00:00 2001 From: Zejun Zhao Date: Wed, 5 Mar 2025 13:48:30 +0800 Subject: [PATCH] Eliminate redundant Clone in hot path --- kernel/src/sched/sched_class/idle.rs | 13 ++++++------- kernel/src/sched/sched_class/stop.rs | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/kernel/src/sched/sched_class/idle.rs b/kernel/src/sched/sched_class/idle.rs index db55684f4..4f4ee2f91 100644 --- a/kernel/src/sched/sched_class/idle.rs +++ b/kernel/src/sched/sched_class/idle.rs @@ -35,12 +35,11 @@ impl core::fmt::Debug for IdleClassRq { impl SchedClassRq for IdleClassRq { fn enqueue(&mut self, entity: Arc, _: Option) { - let ptr = Arc::as_ptr(&entity); - if let Some(t) = self.entity.replace(entity) - && ptr != Arc::as_ptr(&t) - { - panic!("Multiple `idle` entities spawned") - } + let old = self.entity.replace(entity); + debug_assert!( + old.is_none(), + "the length of the idle queue should be no larger than 1" + ); } fn len(&self) -> usize { @@ -52,7 +51,7 @@ impl SchedClassRq for IdleClassRq { } fn pick_next(&mut self) -> Option> { - self.entity.clone() + self.entity.take() } fn update_current(&mut self, _: &CurrentRuntime, _: &SchedAttr, _flags: UpdateFlags) -> bool { diff --git a/kernel/src/sched/sched_class/stop.rs b/kernel/src/sched/sched_class/stop.rs index 7dfadd7fc..0996a9d86 100644 --- a/kernel/src/sched/sched_class/stop.rs +++ b/kernel/src/sched/sched_class/stop.rs @@ -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 used for the most critical tasks, such as powering off and rebooting. pub(super) struct StopClassRq { - thread: Option>, + entity: Option>, } impl StopClassRq { pub fn new() -> Self { - Self { thread: None } + Self { entity: None } } } impl core::fmt::Debug for StopClassRq { 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")?; } else { write!(f, "Stop: empty")?; @@ -35,10 +35,12 @@ impl core::fmt::Debug for StopClassRq { } impl SchedClassRq for StopClassRq { - fn enqueue(&mut self, thread: Arc, _: Option) { - if self.thread.replace(thread).is_some() { - panic!("Multiple `stop` threads spawned") - } + fn enqueue(&mut self, entity: Arc, _: Option) { + let old = self.entity.replace(entity); + debug_assert!( + old.is_none(), + "the length of the stop queue should be no larger than 1" + ); } fn len(&self) -> usize { @@ -46,15 +48,15 @@ impl SchedClassRq for StopClassRq { } fn is_empty(&self) -> bool { - self.thread.is_none() + self.entity.is_none() } fn pick_next(&mut self) -> Option> { - self.thread.take() + self.entity.take() } 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 } }