Save a pointer by passing around an Arc<[T]> instead of Arc<Vec<T>>

This commit is contained in:
Félix Saparelli 2022-01-29 02:17:23 +13:00
parent 22f081a47b
commit cc0c0be45a
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
2 changed files with 9 additions and 9 deletions

View File

@ -114,7 +114,7 @@ pub async fn worker(
trace!("out of throttle, starting action process"); trace!("out of throttle, starting action process");
last = Instant::now(); last = Instant::now();
let events = Arc::new(set.drain(..).collect()); let events = Arc::from(set.drain(..).collect::<Vec<_>>().into_boxed_slice());
let action = Action::new(Arc::clone(&events)); let action = Action::new(Arc::clone(&events));
debug!(?action, "action constructed"); debug!(?action, "action constructed");
@ -157,7 +157,7 @@ pub async fn worker(
#[derive(Clone)] #[derive(Clone)]
struct ActionOutcome { struct ActionOutcome {
events: Arc<Vec<Event>>, // TODO: make this Arc<[Event]> events: Arc<[Event]>,
working: Receiver<WorkingData>, working: Receiver<WorkingData>,
process: ProcessHolder, process: ProcessHolder,
errors_c: mpsc::Sender<RuntimeError>, errors_c: mpsc::Sender<RuntimeError>,

View File

@ -127,18 +127,18 @@ impl Default for WorkingData {
/// ///
/// The [`Action::outcome()`] method is the only way to set the outcome of the action, and it _must_ /// The [`Action::outcome()`] method is the only way to set the outcome of the action, and it _must_
/// be called before the handler returns. /// be called before the handler returns.
#[derive(Debug, Default)] #[derive(Debug)]
pub struct Action { pub struct Action {
/// The collected events which triggered the action. /// The collected events which triggered the action.
pub events: Arc<Vec<Event>>, pub events: Arc<[Event]>,
pub(super) outcome: Arc<OnceCell<Outcome>>, pub(super) outcome: Arc<OnceCell<Outcome>>,
} }
impl Action { impl Action {
pub(super) fn new(events: Arc<Vec<Event>>) -> Self { pub(super) fn new(events: Arc<[Event]>) -> Self {
Self { Self {
events, events,
..Self::default() outcome: Default::default(),
} }
} }
@ -170,7 +170,7 @@ pub struct PreSpawn {
pub command: Vec<String>, pub command: Vec<String>,
/// The collected events which triggered the action this command issues from. /// The collected events which triggered the action this command issues from.
pub events: Arc<Vec<Event>>, pub events: Arc<[Event]>,
command_w: Weak<Mutex<Command>>, command_w: Weak<Mutex<Command>>,
} }
@ -179,7 +179,7 @@ impl PreSpawn {
pub(super) fn new( pub(super) fn new(
command: Command, command: Command,
cmd: Vec<String>, cmd: Vec<String>,
events: Arc<Vec<Event>>, events: Arc<[Event]>,
) -> (Self, Arc<Mutex<Command>>) { ) -> (Self, Arc<Mutex<Command>>) {
let arc = Arc::new(Mutex::new(command)); let arc = Arc::new(Mutex::new(command));
( (
@ -220,7 +220,7 @@ pub struct PostSpawn {
pub command: Vec<String>, pub command: Vec<String>,
/// The collected events which triggered the action the command issues from. /// The collected events which triggered the action the command issues from.
pub events: Arc<Vec<Event>>, pub events: Arc<[Event]>,
/// The process ID or the process group ID. /// The process ID or the process group ID.
pub id: u32, pub id: u32,