Skip to content

Commit

Permalink
make event generation lazy
Browse files Browse the repository at this point in the history
Creating events if nobody is listening has
always bugged me.
  • Loading branch information
nikomatsakis committed Jul 27, 2024
1 parent 475c3bb commit 9f05061
Show file tree
Hide file tree
Showing 21 changed files with 37 additions and 24 deletions.
3 changes: 2 additions & 1 deletion examples/calc/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion examples/lazy-input/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
Expand Down
8 changes: 5 additions & 3 deletions src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -38,14 +38,16 @@ 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,
output_key: output,
},
});

output.remove_stale_output(db.as_dyn_database(), key);
output.remove_stale_output(db, key);
}
}
2 changes: 1 addition & 1 deletion src/function/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/function/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/local_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,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,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion tests/accumulate-from-tracked-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/accumulate-reuse-workaround.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/accumulate-reuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/accumulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion tests/deletion-cascade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. } => {
Expand Down
3 changes: 2 additions & 1 deletion tests/deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. } => {
Expand Down
3 changes: 2 additions & 1 deletion tests/parallel/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion tests/preverify-struct-with-leaked-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"));
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/tracked-struct-value-field-bad-eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. } => {
Expand Down

0 comments on commit 9f05061

Please sign in to comment.