rhai/src/eval/cache.rs

83 lines
2.6 KiB
Rust
Raw Normal View History

2022-04-16 10:36:53 +02:00
//! System caches.
use crate::func::{CallableFunction, StraightHashMap};
2022-09-12 13:47:29 +02:00
use crate::types::BloomFilterU64;
2022-10-29 08:12:18 +02:00
use crate::{ImmutableString, StaticVec};
2022-04-16 10:36:53 +02:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// _(internals)_ An entry in a function resolution cache.
/// Exported under the `internals` feature only.
#[derive(Debug, Clone)]
pub struct FnResolutionCacheEntry {
/// Function.
pub func: CallableFunction,
/// Optional source.
2022-10-29 08:12:18 +02:00
pub source: Option<ImmutableString>,
2022-04-16 10:36:53 +02:00
}
2022-09-12 13:47:29 +02:00
/// _(internals)_ A function resolution cache with a bloom filter.
2022-04-16 10:36:53 +02:00
/// Exported under the `internals` feature only.
///
2022-09-12 17:08:38 +02:00
/// The bloom filter is used to rapidly check whether a function hash has never been encountered.
/// It enables caching a hash only during the second encounter to avoid "one-hit wonders".
2022-09-12 13:47:29 +02:00
#[derive(Debug, Clone, Default)]
pub struct FnResolutionCache {
/// Hash map containing cached functions.
2022-09-12 17:08:38 +02:00
pub map: StraightHashMap<Option<FnResolutionCacheEntry>>,
2022-09-12 13:47:29 +02:00
/// Bloom filter to avoid caching "one-hit wonders".
pub filter: BloomFilterU64,
}
impl FnResolutionCache {
/// Clear the [`FnResolutionCache`].
#[inline(always)]
pub fn clear(&mut self) {
self.map.clear();
self.filter.clear();
}
}
2022-04-16 10:36:53 +02:00
/// _(internals)_ A type containing system-wide caches.
/// Exported under the `internals` feature only.
///
/// The following caches are contained inside this type:
/// * A stack of [function resolution caches][FnResolutionCache]
#[derive(Debug, Clone)]
2022-11-10 07:25:48 +01:00
pub struct Caches(StaticVec<FnResolutionCache>);
2022-04-16 10:36:53 +02:00
impl Caches {
2022-04-16 10:36:53 +02:00
/// Create an empty [`Caches`].
#[inline(always)]
#[must_use]
pub const fn new() -> Self {
2022-11-10 07:25:48 +01:00
Self(StaticVec::new_const())
2022-04-16 10:36:53 +02:00
}
/// Get the number of function resolution cache(s) in the stack.
#[inline(always)]
#[must_use]
pub fn fn_resolution_caches_len(&self) -> usize {
2022-11-10 07:25:48 +01:00
self.0.len()
2022-04-16 10:36:53 +02:00
}
/// Get a mutable reference to the current function resolution cache.
#[inline]
#[must_use]
pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache {
2022-11-10 07:25:48 +01:00
if self.0.is_empty() {
2022-04-16 10:36:53 +02:00
// Push a new function resolution cache if the stack is empty
self.push_fn_resolution_cache();
}
2022-11-10 07:25:48 +01:00
self.0.last_mut().unwrap()
2022-04-16 10:36:53 +02:00
}
/// Push an empty function resolution cache onto the stack and make it current.
#[inline(always)]
pub fn push_fn_resolution_cache(&mut self) {
2022-11-10 07:25:48 +01:00
self.0.push(Default::default());
2022-04-16 10:36:53 +02:00
}
/// Rewind the function resolution caches stack to a particular size.
#[inline(always)]
pub fn rewind_fn_resolution_caches(&mut self, len: usize) {
2022-11-10 07:25:48 +01:00
self.0.truncate(len);
2022-04-16 10:36:53 +02:00
}
}