make event generation lazy

Creating events if nobody is listening has
always bugged me.
This commit is contained in:
Niko Matsakis 2024-07-27 11:47:58 +00:00
parent 1842b1dfbb
commit 64556e9d28
21 changed files with 37 additions and 24 deletions

View file

@ -36,7 +36,8 @@ impl Database {
// ANCHOR: db_impl
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
let event = event();
eprintln!("Event: {event:?}");
// Log interesting events, if logging is enabled
if let Some(logs) = &self.logs {

View file

@ -124,8 +124,9 @@ impl Db for Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
// don't log boring events
let event = event();
if let salsa::EventKind::WillExecute { .. } = event.kind {
self.logs.lock().unwrap().push(format!("{:?}", event));
}

View file

@ -177,7 +177,7 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
) {
assert!(stale_output_key.is_none());
if self.map.remove(&executor).is_some() {
db.salsa_event(Event {
db.salsa_event(&|| Event {
thread_id: std::thread::current().id(),
kind: EventKind::DidDiscardAccumulated {
executor_key: executor,

View file

@ -8,8 +8,12 @@ pub trait Database: ZalsaDatabase + AsDynDatabase {
///
/// By default, the event is logged at level debug using
/// the standard `log` facade.
fn salsa_event(&self, event: Event) {
tracing::debug!("salsa_event: {:?}", event)
///
/// # Parameters
///
/// * `event`, a fn that, if called, will create the event that occurred
fn salsa_event(&self, event: &dyn Fn() -> Event) {
tracing::debug!("salsa_event: {:?}", event())
}
/// A "synthetic write" causes the system to act *as though* some

View file

@ -264,7 +264,7 @@ where
if let Some(origin) = self.delete_memo(id) {
let key = self.database_key_index(id);
db.salsa_event(Event {
db.salsa_event(&|| Event {
thread_id: std::thread::current().id(),
kind: EventKind::DidDiscard { key },
});

View file

@ -1,6 +1,6 @@
use crate::{
hash::FxHashSet, key::DependencyIndex, local_state::QueryRevisions, AsDynDatabase as _,
Database, DatabaseKeyIndex, Event, EventKind,
DatabaseKeyIndex, Event, EventKind,
};
use super::{memo::Memo, Configuration, IngredientImpl};
@ -38,7 +38,9 @@ where
}
fn report_stale_output(db: &C::DbView, key: DatabaseKeyIndex, output: DependencyIndex) {
db.salsa_event(Event {
let db = db.as_dyn_database();
db.salsa_event(&|| Event {
thread_id: std::thread::current().id(),
kind: EventKind::WillDiscardStaleOutput {
execute_key: key,
@ -46,6 +48,6 @@ where
},
});
output.remove_stale_output(db.as_dyn_database(), key);
output.remove_stale_output(db, key);
}
}

View file

@ -32,7 +32,7 @@ where
tracing::info!("{:?}: executing query", database_key_index);
db.salsa_event(Event {
db.salsa_event(&|| Event {
thread_id: std::thread::current().id(),
kind: EventKind::WillExecute {
database_key: database_key_index,

View file

@ -149,7 +149,7 @@ impl<V> Memo<V> {
revision_now: Revision,
database_key_index: DatabaseKeyIndex,
) {
db.salsa_event(Event {
db.salsa_event(&|| Event {
thread_id: std::thread::current().id(),
kind: EventKind::DidValidateMemoizedValue {
database_key: database_key_index,

View file

@ -80,7 +80,7 @@ impl<Db: Database> Handle<Db> {
let zalsa = self.db().zalsa();
zalsa.set_cancellation_flag();
self.db().salsa_event(Event {
self.db().salsa_event(&|| Event {
thread_id: std::thread::current().id(),
kind: EventKind::DidSetCancellationFlag,

View file

@ -321,7 +321,7 @@ impl LocalState {
/// used instead.
pub(crate) fn unwind_if_revision_cancelled(&self, db: &dyn Database) {
let thread_id = std::thread::current().id();
db.salsa_event(Event {
db.salsa_event(&|| Event {
thread_id,
kind: EventKind::WillCheckCancellation,

View file

@ -187,7 +187,7 @@ impl Runtime {
assert!(!dg.depends_on(other_id, thread_id));
}
db.salsa_event(Event {
db.salsa_event(&|| Event {
thread_id,
kind: EventKind::WillBlockOn {
other_thread_id: other_id,

View file

@ -394,7 +394,7 @@ 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 {
db.salsa_event(&|| Event {
thread_id: std::thread::current().id(),
kind: crate::EventKind::DidDiscard {
key: self.database_key_index(id),

View file

@ -52,7 +52,7 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, _event: salsa::Event) {}
fn salsa_event(&self, _event: &dyn Fn() -> salsa::Event) {}
}
#[salsa::db]

View file

@ -59,7 +59,7 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, _event: salsa::Event) {}
fn salsa_event(&self, _event: &dyn Fn() -> salsa::Event) {}
}
#[salsa::db]

View file

@ -50,7 +50,7 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, _event: salsa::Event) {}
fn salsa_event(&self, _event: &dyn Fn() -> salsa::Event) {}
}
#[salsa::db]

View file

@ -65,7 +65,7 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, _event: salsa::Event) {}
fn salsa_event(&self, _event: &dyn Fn() -> salsa::Event) {}
}
#[salsa::db]

View file

@ -60,7 +60,8 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
let event = event();
match event.kind {
salsa::EventKind::WillDiscardStaleOutput { .. }
| salsa::EventKind::DidDiscard { .. } => {

View file

@ -54,7 +54,8 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
let event = event();
match event.kind {
salsa::EventKind::WillDiscardStaleOutput { .. }
| salsa::EventKind::DidDiscard { .. } => {

View file

@ -37,7 +37,8 @@ pub(crate) struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
let event = event();
match event.kind {
salsa::EventKind::WillBlockOn { .. } => {
self.signal(self.knobs().signal_on_will_block.load());

View file

@ -25,7 +25,8 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
let event = event();
self.push_log(format!("{event:?}"));
}
}

View file

@ -61,7 +61,8 @@ struct Database {
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
let event = event();
match event.kind {
salsa::EventKind::WillExecute { .. }
| salsa::EventKind::DidValidateMemoizedValue { .. } => {