salsa/tests/lru.rs

167 lines
4.7 KiB
Rust
Raw Normal View History

2022-08-17 09:22:12 +00:00
//! Test that a `tracked` fn with lru options
2022-08-15 23:29:43 +00:00
//! compiles and executes successfully.
2022-08-17 10:44:43 +00:00
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
2022-08-15 23:29:43 +00:00
2024-06-18 07:40:21 +00:00
mod common;
use common::LogDatabase;
use salsa::Database as _;
2022-08-15 23:29:43 +00:00
use test_log::test;
#[derive(Debug, PartialEq, Eq)]
struct HotPotato(u32);
thread_local! {
static N_POTATOES: AtomicUsize = const { AtomicUsize::new(0) }
2022-08-15 23:29:43 +00:00
}
impl HotPotato {
fn new(id: u32) -> HotPotato {
N_POTATOES.with(|n| n.fetch_add(1, Ordering::SeqCst));
HotPotato(id)
}
}
impl Drop for HotPotato {
fn drop(&mut self) {
N_POTATOES.with(|n| n.fetch_sub(1, Ordering::SeqCst));
}
}
2024-07-16 10:04:01 +00:00
#[salsa::input]
2022-08-15 23:29:43 +00:00
struct MyInput {
field: u32,
}
2024-07-17 13:14:20 +00:00
#[salsa::tracked(lru = 32)]
fn get_hot_potato(db: &dyn LogDatabase, input: MyInput) -> Arc<HotPotato> {
2022-08-21 02:31:46 +00:00
db.push_log(format!("get_hot_potato({:?})", input.field(db)));
2022-08-15 23:29:43 +00:00
Arc::new(HotPotato::new(input.field(db)))
}
2024-07-16 10:04:01 +00:00
#[salsa::tracked]
fn get_hot_potato2(db: &dyn LogDatabase, input: MyInput) -> u32 {
2022-08-21 02:31:46 +00:00
db.push_log(format!("get_hot_potato2({:?})", input.field(db)));
get_hot_potato(db, input).0
}
2024-07-17 13:14:20 +00:00
#[salsa::tracked(lru = 32)]
fn get_volatile(db: &dyn LogDatabase, _input: MyInput) -> usize {
2022-08-17 09:22:12 +00:00
static COUNTER: AtomicUsize = AtomicUsize::new(0);
db.report_untracked_read();
2022-08-17 09:22:12 +00:00
COUNTER.fetch_add(1, Ordering::SeqCst)
}
2022-08-15 23:29:43 +00:00
fn load_n_potatoes() -> usize {
N_POTATOES.with(|n| n.load(Ordering::SeqCst))
}
#[test]
2022-08-17 09:22:12 +00:00
fn lru_works() {
let db = common::LoggerDatabase::default();
2022-08-15 23:29:43 +00:00
assert_eq!(load_n_potatoes(), 0);
for i in 0..128u32 {
let input = MyInput::new(&db, i);
2022-08-15 23:29:43 +00:00
let p = get_hot_potato(&db, input);
assert_eq!(p.0, i)
}
2022-08-17 09:22:12 +00:00
// Create a new input to change the revision, and trigger the GC
MyInput::new(&db, 0);
2022-08-15 23:29:43 +00:00
assert_eq!(load_n_potatoes(), 32);
2022-08-17 09:22:12 +00:00
}
#[test]
fn lru_doesnt_break_volatile_queries() {
let db = common::LoggerDatabase::default();
2022-08-17 09:22:12 +00:00
// Create all inputs first, so that there are no revision changes among calls to `get_volatile`
let inputs: Vec<MyInput> = (0..128usize).map(|i| MyInput::new(&db, i as u32)).collect();
2022-08-17 09:22:12 +00:00
// Here, we check that we execute each volatile query at most once, despite
// LRU. That does mean that we have more values in DB than the LRU capacity,
// but it's much better than inconsistent results from volatile queries!
for _ in 0..3 {
for (i, input) in inputs.iter().enumerate() {
let x = get_volatile(&db, *input);
assert_eq!(x, i);
}
}
2022-08-17 10:44:43 +00:00
}
2022-08-21 02:02:45 +00:00
#[test]
fn lru_can_be_changed_at_runtime() {
let db = common::LoggerDatabase::default();
2022-08-21 02:02:45 +00:00
assert_eq!(load_n_potatoes(), 0);
let inputs: Vec<(u32, MyInput)> = (0..128).map(|i| (i, MyInput::new(&db, i))).collect();
2022-08-22 00:21:51 +00:00
for &(i, input) in inputs.iter() {
2022-08-21 02:02:45 +00:00
let p = get_hot_potato(&db, input);
assert_eq!(p.0, i)
}
// Create a new input to change the revision, and trigger the GC
MyInput::new(&db, 0);
2022-08-21 02:02:45 +00:00
assert_eq!(load_n_potatoes(), 32);
get_hot_potato::set_lru_capacity(&db, 64);
assert_eq!(load_n_potatoes(), 32);
2022-08-22 00:21:51 +00:00
for &(i, input) in inputs.iter() {
2022-08-21 02:02:45 +00:00
let p = get_hot_potato(&db, input);
assert_eq!(p.0, i)
}
// Create a new input to change the revision, and trigger the GC
MyInput::new(&db, 0);
2022-08-21 02:02:45 +00:00
assert_eq!(load_n_potatoes(), 64);
// Special case: setting capacity to zero disables LRU
get_hot_potato::set_lru_capacity(&db, 0);
assert_eq!(load_n_potatoes(), 64);
2022-08-22 00:21:51 +00:00
for &(i, input) in inputs.iter() {
2022-08-21 02:02:45 +00:00
let p = get_hot_potato(&db, input);
assert_eq!(p.0, i)
}
// Create a new input to change the revision, and trigger the GC
MyInput::new(&db, 0);
2022-08-22 00:21:51 +00:00
assert_eq!(load_n_potatoes(), 128);
2022-08-21 02:02:45 +00:00
drop(db);
assert_eq!(load_n_potatoes(), 0);
}
2022-08-21 02:31:46 +00:00
#[test]
fn lru_keeps_dependency_info() {
let mut db = common::LoggerDatabase::default();
2022-08-21 02:31:46 +00:00
let capacity = 32;
// Invoke `get_hot_potato2` 33 times. This will (in turn) invoke
// `get_hot_potato`, which will trigger LRU after 32 executions.
let inputs: Vec<MyInput> = (0..(capacity + 1))
.map(|i| MyInput::new(&db, i as u32))
2022-08-21 02:31:46 +00:00
.collect();
for (i, input) in inputs.iter().enumerate() {
let x = get_hot_potato2(&db, *input);
assert_eq!(x as usize, i);
}
db.synthetic_write(salsa::Durability::HIGH);
2022-08-22 23:10:34 +00:00
2022-08-21 02:31:46 +00:00
// We want to test that calls to `get_hot_potato2` are still considered
// clean. Check that no new executions occur as we go here.
db.assert_logs_len((capacity + 1) * 2);
// calling `get_hot_potato2(0)` has to check that `get_hot_potato(0)` is still valid;
// even though we've evicted it (LRU), we find that it is still good
let p = get_hot_potato2(&db, *inputs.first().unwrap());
assert_eq!(p, 0);
db.assert_logs_len(0);
}