From 266f620140a47cc4bf53cc30cac22d4c052c9993 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 5 Jan 2025 13:00:13 +0100 Subject: [PATCH] Simplify Event construction --- src/event.rs | 9 +++++++++ src/function/diff_outputs.rs | 7 +++---- src/function/execute.rs | 7 +++---- src/function/memo.rs | 7 +++---- src/runtime.rs | 7 +++---- src/storage.rs | 5 +---- src/tracked_struct.rs | 12 ++++-------- src/zalsa_local.rs | 7 +------ 8 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/event.rs b/src/event.rs index cd425d6c..ac413647 100644 --- a/src/event.rs +++ b/src/event.rs @@ -17,6 +17,15 @@ pub struct Event { pub kind: EventKind, } +impl Event { + pub fn new(kind: EventKind) -> Self { + Self { + thread_id: std::thread::current().id(), + kind, + } + } +} + /// An enum identifying the various kinds of events that can occur. #[derive(Debug)] pub enum EventKind { diff --git a/src/function/diff_outputs.rs b/src/function/diff_outputs.rs index 93a42835..a727d772 100644 --- a/src/function/diff_outputs.rs +++ b/src/function/diff_outputs.rs @@ -45,12 +45,11 @@ where fn report_stale_output(db: &C::DbView, key: DatabaseKeyIndex, output: OutputDependencyIndex) { let db = db.as_dyn_database(); - db.salsa_event(&|| Event { - thread_id: std::thread::current().id(), - kind: EventKind::WillDiscardStaleOutput { + db.salsa_event(&|| { + Event::new(EventKind::WillDiscardStaleOutput { execute_key: key, output_key: output, - }, + }) }); output.remove_stale_output(db, key); diff --git a/src/function/execute.rs b/src/function/execute.rs index 4171fe6d..9e7f303d 100644 --- a/src/function/execute.rs +++ b/src/function/execute.rs @@ -31,11 +31,10 @@ where tracing::info!("{:?}: executing query", database_key_index); - db.salsa_event(&|| Event { - thread_id: std::thread::current().id(), - kind: EventKind::WillExecute { + db.salsa_event(&|| { + Event::new(EventKind::WillExecute { database_key: database_key_index, - }, + }) }); // If we already executed this query once, then use the tracked-struct ids from the diff --git a/src/function/memo.rs b/src/function/memo.rs index 304c6c31..57898cc1 100644 --- a/src/function/memo.rs +++ b/src/function/memo.rs @@ -144,11 +144,10 @@ impl Memo { revision_now: Revision, database_key_index: DatabaseKeyIndex, ) { - db.salsa_event(&|| Event { - thread_id: std::thread::current().id(), - kind: EventKind::DidValidateMemoizedValue { + db.salsa_event(&|| { + Event::new(EventKind::DidValidateMemoizedValue { database_key: database_key_index, - }, + }) }); self.verified_at.store(revision_now); diff --git a/src/runtime.rs b/src/runtime.rs index 917ec5c4..ae07afa4 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -191,12 +191,11 @@ impl Runtime { assert!(!dg.depends_on(other_id, thread_id)); } - db.salsa_event(&|| Event { - thread_id, - kind: EventKind::WillBlockOn { + db.salsa_event(&|| { + Event::new(EventKind::WillBlockOn { other_thread_id: other_id, database_key, - }, + }) }); let result = local_state.with_query_stack(|stack| { diff --git a/src/storage.rs b/src/storage.rs index fdcf1e3d..10341e0b 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -69,10 +69,7 @@ impl Storage { fn cancel_others(&self, db: &Db) { self.zalsa_impl.set_cancellation_flag(); - db.salsa_event(&|| Event { - thread_id: std::thread::current().id(), - kind: EventKind::DidSetCancellationFlag, - }); + db.salsa_event(&|| Event::new(EventKind::DidSetCancellationFlag)); let mut clones = self.coordinate.clones.lock(); while *clones != 1 { diff --git a/src/tracked_struct.rs b/src/tracked_struct.rs index 3e13b7d1..d0961c0a 100644 --- a/src/tracked_struct.rs +++ b/src/tracked_struct.rs @@ -469,11 +469,10 @@ where /// unspecified results (but not UB). See [`InternedIngredient::delete_index`] for more /// discussion and important considerations. pub(crate) fn delete_entity(&self, db: &dyn crate::Database, id: Id) { - db.salsa_event(&|| Event { - thread_id: std::thread::current().id(), - kind: crate::EventKind::DidDiscard { + db.salsa_event(&|| { + Event::new(crate::EventKind::DidDiscard { key: self.database_key_index(id), - }, + }) }); let zalsa = db.zalsa(); @@ -514,10 +513,7 @@ where key_index: id, }; - db.salsa_event(&|| Event { - thread_id: std::thread::current().id(), - kind: EventKind::DidDiscard { key: executor }, - }); + db.salsa_event(&|| Event::new(EventKind::DidDiscard { key: executor })); for stale_output in memo.origin().outputs() { stale_output.remove_stale_output(db, executor); diff --git a/src/zalsa_local.rs b/src/zalsa_local.rs index de5a7010..3d087b27 100644 --- a/src/zalsa_local.rs +++ b/src/zalsa_local.rs @@ -288,12 +288,7 @@ impl ZalsaLocal { /// `salsa_event` is emitted when this method is called, so that should be /// used instead. pub(crate) fn unwind_if_revision_cancelled(&self, db: &dyn Database) { - let thread_id = std::thread::current().id(); - db.salsa_event(&|| Event { - thread_id, - - kind: EventKind::WillCheckCancellation, - }); + db.salsa_event(&|| Event::new(EventKind::WillCheckCancellation)); let zalsa = db.zalsa(); if zalsa.load_cancellation_flag() { self.unwind_cancelled(zalsa.current_revision());