Skip to content

Commit 9fd6c69

Browse files
committed
Auto merge of #17638 - Veykril:salsa-perf, r=Veykril
perf: Reduce memory usage of salsa slots by 8 bytes
2 parents 9b1b29c + 4691ca9 commit 9fd6c69

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

crates/salsa/src/derived/slot.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::lru::LruNode;
66
use crate::plumbing::{DatabaseOps, QueryFunction};
77
use crate::revision::Revision;
88
use crate::runtime::local_state::ActiveQueryGuard;
9-
use crate::runtime::local_state::QueryInputs;
109
use crate::runtime::local_state::QueryRevisions;
1110
use crate::runtime::Runtime;
1211
use crate::runtime::RuntimeId;
@@ -28,8 +27,8 @@ where
2827
key_index: u32,
2928
group_index: u16,
3029
state: RwLock<QueryState<Q>>,
31-
policy: PhantomData<MP>,
3230
lru_index: LruIndex,
31+
policy: PhantomData<MP>,
3332
}
3433

3534
/// Defines the "current state" of query's memoized results.
@@ -430,7 +429,8 @@ where
430429
tracing::debug!("Slot::invalidate(new_revision = {:?})", new_revision);
431430
match &mut *self.state.write() {
432431
QueryState::Memoized(memo) => {
433-
memo.revisions.inputs = QueryInputs::Untracked;
432+
memo.revisions.untracked = true;
433+
memo.revisions.inputs = None;
434434
memo.revisions.changed_at = new_revision;
435435
Some(memo.revisions.durability)
436436
}
@@ -746,11 +746,8 @@ where
746746
match &self.revisions.inputs {
747747
// We can't validate values that had untracked inputs; just have to
748748
// re-execute.
749-
QueryInputs::Untracked => {
750-
return false;
751-
}
752-
753-
QueryInputs::NoInputs => {}
749+
None if self.revisions.untracked => return false,
750+
None => {}
754751

755752
// Check whether any of our inputs changed since the
756753
// **last point where we were verified** (not since we
@@ -761,7 +758,7 @@ where
761758
// R1. But our *verification* date will be R2, and we
762759
// are only interested in finding out whether the
763760
// input changed *again*.
764-
QueryInputs::Tracked { inputs } => {
761+
Some(inputs) => {
765762
let changed_input =
766763
inputs.slice.iter().find(|&&input| db.maybe_changed_after(input, verified_at));
767764
if let Some(input) = changed_input {
@@ -793,7 +790,7 @@ where
793790
}
794791

795792
fn has_untracked_input(&self) -> bool {
796-
matches!(self.revisions.inputs, QueryInputs::Untracked)
793+
self.revisions.untracked
797794
}
798795
}
799796

crates/salsa/src/runtime.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use dependency_graph::DependencyGraph;
1818
pub(crate) mod local_state;
1919
use local_state::LocalState;
2020

21-
use self::local_state::{ActiveQueryGuard, QueryInputs, QueryRevisions};
21+
use self::local_state::{ActiveQueryGuard, QueryRevisions};
2222

2323
/// The salsa runtime stores the storage for all queries as well as
2424
/// tracking the query stack and dependencies between cycles.
@@ -558,21 +558,25 @@ impl ActiveQuery {
558558
}
559559

560560
pub(crate) fn revisions(&self) -> QueryRevisions {
561-
let inputs = match &self.dependencies {
562-
None => QueryInputs::Untracked,
561+
let (inputs, untracked) = match &self.dependencies {
562+
None => (None, true),
563563

564-
Some(dependencies) => {
564+
Some(dependencies) => (
565565
if dependencies.is_empty() {
566-
QueryInputs::NoInputs
566+
None
567567
} else {
568-
QueryInputs::Tracked {
569-
inputs: ThinArc::from_header_and_iter((), dependencies.iter().copied()),
570-
}
571-
}
572-
}
568+
Some(ThinArc::from_header_and_iter((), dependencies.iter().copied()))
569+
},
570+
false,
571+
),
573572
};
574573

575-
QueryRevisions { changed_at: self.changed_at, inputs, durability: self.durability }
574+
QueryRevisions {
575+
changed_at: self.changed_at,
576+
inputs,
577+
untracked,
578+
durability: self.durability,
579+
}
576580
}
577581

578582
/// Adds any dependencies from `other` into `self`.

crates/salsa/src/runtime/local_state.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,13 @@ pub(crate) struct QueryRevisions {
3434
/// Minimum durability of the inputs to this query.
3535
pub(crate) durability: Durability,
3636

37-
/// The inputs that went into our query, if we are tracking them.
38-
pub(crate) inputs: QueryInputs,
39-
}
40-
41-
/// Every input.
42-
#[derive(Debug, Clone)]
43-
pub(crate) enum QueryInputs {
44-
/// Non-empty set of inputs, fully known
45-
Tracked { inputs: ThinArc<(), DatabaseKeyIndex> },
46-
47-
/// Empty set of inputs, fully known.
48-
NoInputs,
37+
/// Whether the input is untracked.
38+
/// Invariant: if `untracked`, `inputs` is `None`.
39+
/// Why is this encoded like this and not a proper enum? Struct size, this saves us 8 bytes.
40+
pub(crate) untracked: bool,
4941

50-
/// Unknown quantity of inputs
51-
Untracked,
42+
/// The inputs that went into our query, if we are tracking them.
43+
pub(crate) inputs: Option<ThinArc<(), DatabaseKeyIndex>>,
5244
}
5345

5446
impl Default for LocalState {

0 commit comments

Comments
 (0)