comemo/
cache.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::any::{Any, TypeId};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::hash::Hash;

use siphasher::sip128::{Hasher128, SipHasher13};

use crate::input::Input;

thread_local! {
    /// The global, dynamic cache shared by all memoized functions.
    static CACHE: RefCell<Cache> = RefCell::new(Cache::default());

    /// The global ID counter for tracked values. Each tracked value gets a
    /// unqiue ID based on which its validations are cached in the accelerator.
    /// IDs may only be reused upon eviction of the accelerator.
    static ID: Cell<usize> = const { Cell::new(0) };

    /// The global, dynamic accelerator shared by all cached values.
    static ACCELERATOR: RefCell<HashMap<(usize, u128), u128>>
        = RefCell::new(HashMap::default());
}

/// Execute a function or use a cached result for it.
pub fn memoized<'c, In, Out, F>(
    id: TypeId,
    mut input: In,
    constraint: &'c In::Constraint,
    func: F,
) -> Out
where
    In: Input + 'c,
    Out: Clone + 'static,
    F: FnOnce(In::Tracked<'c>) -> Out,
{
    CACHE.with(|cache| {
        // Compute the hash of the input's key part.
        let key = {
            let mut state = SipHasher13::new();
            input.key(&mut state);
            let hash = state.finish128().as_u128();
            (id, hash)
        };

        // Check if there is a cached output.
        let mut borrow = cache.borrow_mut();
        if let Some(constrained) = borrow.lookup::<In, Out>(key, &input) {
            // Replay the mutations.
            input.replay(&constrained.constraint);

            // Add the cached constraints to the outer ones.
            input.retrack(constraint).1.join(&constrained.constraint);

            let value = constrained.output.clone();
            borrow.last_was_hit = true;
            return value;
        }

        // Release the borrow so that nested memoized calls can access the
        // cache without panicking.
        drop(borrow);

        // Execute the function with the new constraints hooked in.
        let (input, outer) = input.retrack(constraint);
        let output = func(input);

        // Add the new constraints to the outer ones.
        outer.join(constraint);

        // Insert the result into the cache.
        borrow = cache.borrow_mut();
        borrow.insert::<In, Out>(key, constraint.take(), output.clone());
        borrow.last_was_hit = false;

        output
    })
}

/// Whether the last call was a hit.
pub fn last_was_hit() -> bool {
    CACHE.with(|cache| cache.borrow().last_was_hit)
}

/// Get the next ID.
pub fn id() -> usize {
    ID.with(|cell| {
        let current = cell.get();
        cell.set(current.wrapping_add(1));
        current
    })
}

/// Evict the cache.
///
/// This removes all memoized results from the cache whose age is larger than or
/// equal to `max_age`. The age of a result grows by one during each eviction
/// and is reset to zero when the result produces a cache hit. Set `max_age` to
/// zero to completely clear the cache.
///
/// Comemo's cache is thread-local, meaning that this only evicts this thread's
/// cache.
pub fn evict(max_age: usize) {
    CACHE.with(|cache| {
        let mut cache = cache.borrow_mut();
        cache.map.retain(|_, entries| {
            entries.retain_mut(|entry| {
                entry.age += 1;
                entry.age <= max_age
            });
            !entries.is_empty()
        });
    });
    ACCELERATOR.with(|accelerator| accelerator.borrow_mut().clear());
}

/// The global cache.
#[derive(Default)]
struct Cache {
    /// Maps from function IDs + hashes to memoized results.
    map: HashMap<(TypeId, u128), Vec<CacheEntry>>,
    /// Whether the last call was a hit.
    last_was_hit: bool,
}

impl Cache {
    /// Look for a matching entry in the cache.
    fn lookup<In, Out>(
        &mut self,
        key: (TypeId, u128),
        input: &In,
    ) -> Option<&Constrained<In::Constraint, Out>>
    where
        In: Input,
        Out: Clone + 'static,
    {
        self.map
            .get_mut(&key)?
            .iter_mut()
            .rev()
            .find_map(|entry| entry.lookup::<In, Out>(input))
    }

    /// Insert an entry into the cache.
    fn insert<In, Out>(
        &mut self,
        key: (TypeId, u128),
        constraint: In::Constraint,
        output: Out,
    ) where
        In: Input,
        Out: 'static,
    {
        self.map
            .entry(key)
            .or_default()
            .push(CacheEntry::new::<In, Out>(constraint, output));
    }
}

/// A memoized result.
struct CacheEntry {
    /// The memoized function's constrained output.
    ///
    /// This is of type `Constrained<In::Constraint, Out>`.
    constrained: Box<dyn Any>,
    /// How many evictions have passed since the entry has been last used.
    age: usize,
}

/// A value with a constraint.
struct Constrained<C, T> {
    /// The constraint which must be fulfilled for the output to be used.
    constraint: C,
    /// The memoized function's output.
    output: T,
}

impl CacheEntry {
    /// Create a new entry.
    fn new<In, Out>(constraint: In::Constraint, output: Out) -> Self
    where
        In: Input,
        Out: 'static,
    {
        Self {
            constrained: Box::new(Constrained { constraint, output }),
            age: 0,
        }
    }

    /// Return the entry's output if it is valid for the given input.
    fn lookup<In, Out>(&mut self, input: &In) -> Option<&Constrained<In::Constraint, Out>>
    where
        In: Input,
        Out: Clone + 'static,
    {
        let constrained: &Constrained<In::Constraint, Out> =
            self.constrained.downcast_ref().expect("wrong entry type");

        input.validate(&constrained.constraint).then(|| {
            self.age = 0;
            constrained
        })
    }
}

/// Defines a constraint for a tracked type.
#[derive(Clone)]
pub struct Constraint<T>(RefCell<Vec<Call<T>>>);

/// A call entry.
#[derive(Clone)]
struct Call<T> {
    args: T,
    ret: u128,
    both: u128,
    mutable: bool,
}

impl<T: Hash + PartialEq> Constraint<T> {
    /// Create empty constraints.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enter a constraint for a call to an immutable function.
    #[inline]
    pub fn push(&self, args: T, ret: u128, mutable: bool) {
        let both = hash(&(&args, ret));
        self.push_inner(Call { args, ret, both, mutable });
    }

    /// Enter a constraint for a call to an immutable function.
    #[inline]
    fn push_inner(&self, call: Call<T>) {
        let mut calls = self.0.borrow_mut();

        if !call.mutable {
            for prev in calls.iter().rev() {
                if prev.mutable {
                    break;
                }

                #[cfg(debug_assertions)]
                if prev.args == call.args {
                    check(prev.ret, call.ret);
                }

                if prev.both == call.both {
                    return;
                }
            }
        }

        calls.push(call);
    }

    /// Whether the method satisfies as all input-output pairs.
    #[inline]
    pub fn validate<F>(&self, mut f: F) -> bool
    where
        F: FnMut(&T) -> u128,
    {
        self.0.borrow().iter().all(|entry| f(&entry.args) == entry.ret)
    }

    /// Whether the method satisfies as all input-output pairs.
    #[inline]
    pub fn validate_with_id<F>(&self, mut f: F, id: usize) -> bool
    where
        F: FnMut(&T) -> u128,
    {
        let calls = self.0.borrow();
        ACCELERATOR.with(|accelerator| {
            let mut map = accelerator.borrow_mut();
            calls.iter().all(|entry| {
                *map.entry((id, entry.both)).or_insert_with(|| f(&entry.args))
                    == entry.ret
            })
        })
    }

    /// Replay all input-output pairs.
    #[inline]
    pub fn replay<F>(&self, mut f: F)
    where
        F: FnMut(&T),
    {
        for entry in self.0.borrow().iter() {
            if entry.mutable {
                f(&entry.args);
            }
        }
    }
}

impl<T> Default for Constraint<T> {
    fn default() -> Self {
        Self(RefCell::new(vec![]))
    }
}

/// Extend an outer constraint by an inner one.
pub trait Join<T = Self> {
    /// Join this constraint with the `inner` one.
    fn join(&self, inner: &T);

    /// Take out the constraint.
    fn take(&self) -> Self;
}

impl<T: Join> Join<T> for Option<&T> {
    #[inline]
    fn join(&self, inner: &T) {
        if let Some(outer) = self {
            outer.join(inner);
        }
    }

    #[inline]
    fn take(&self) -> Self {
        unimplemented!("cannot call `Join::take` on optional constraint")
    }
}

impl<T: Hash + Clone + PartialEq> Join for Constraint<T> {
    #[inline]
    fn join(&self, inner: &Self) {
        for call in inner.0.borrow().iter() {
            self.push_inner(call.clone());
        }
    }

    #[inline]
    fn take(&self) -> Self {
        Self(RefCell::new(std::mem::take(&mut *self.0.borrow_mut())))
    }
}

/// Produce a 128-bit hash of a value.
#[inline]
pub fn hash<T: Hash>(value: &T) -> u128 {
    let mut state = SipHasher13::new();
    value.hash(&mut state);
    state.finish128().as_u128()
}

/// Check for a constraint violation.
#[inline]
#[track_caller]
#[allow(dead_code)]
fn check(left_hash: u128, right_hash: u128) {
    if left_hash != right_hash {
        panic!(
            "comemo: found conflicting constraints. \
             is this tracked function pure?"
        )
    }
}