essential_vm/
cached.rs

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