essential_vm/
cached.rs

1use crate::access::init_predicate_exists;
2use essential_types::{solution::Solution, Hash};
3use std::{collections::HashSet, sync::OnceLock};
4
5#[derive(Default, Debug, PartialEq)]
6/// Lazily cache expensive to compute values.
7pub struct LazyCache {
8    /// Predicate data and addresses set of hashes.
9    /// See [`PredicateExists`][essential_asm] for more details.
10    pub pred_data_hashes: OnceLock<HashSet<Hash>>,
11}
12
13impl LazyCache {
14    /// Create a new empty `LazyCache`.
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    /// Get the predicate data hashes.
20    ///
21    /// The first time this is called, it will compute the hashes.
22    pub fn get_pred_data_hashes(&self, solutions: &[Solution]) -> &HashSet<Hash> {
23        self.pred_data_hashes
24            .get_or_init(|| init_predicate_exists(solutions).collect())
25    }
26}