fst/raw/
registry_minimal.rs

1// This module is a drop-in but inefficient replacement of the LRU registry.
2// In particular, this registry will never forget a node. In other words, if
3// this registry is used during construction, then you're guaranteed a minimal
4// FST.
5//
6// This is really only meant to be used for debugging and experiments. It is
7// a memory/CPU hog.
8//
9// One "easy" improvement here is to use an FNV hash instead of the super
10// expensive SipHasher.
11
12#![allow(dead_code)]
13
14use std::collections::hash_map::{Entry, HashMap};
15
16use crate::raw::build::BuilderNode;
17use crate::raw::CompiledAddr;
18
19#[derive(Debug)]
20pub struct Registry {
21    table: HashMap<BuilderNode, RegistryCell>,
22}
23
24#[derive(Debug)]
25pub enum RegistryEntry<'a> {
26    Found(CompiledAddr),
27    NotFound(&'a mut RegistryCell),
28    Rejected,
29}
30
31#[derive(Clone, Copy, Debug)]
32pub struct RegistryCell(CompiledAddr);
33
34impl Registry {
35    pub fn new(table_size: usize, _lru_size: usize) -> Registry {
36        Registry { table: HashMap::with_capacity(table_size) }
37    }
38
39    pub fn entry<'a>(&'a mut self, bnode: &BuilderNode) -> RegistryEntry<'a> {
40        match self.table.entry(bnode.clone()) {
41            Entry::Occupied(v) => RegistryEntry::Found(v.get().0),
42            Entry::Vacant(v) => {
43                RegistryEntry::NotFound(v.insert(RegistryCell(0)))
44            }
45        }
46    }
47}
48
49impl RegistryCell {
50    pub fn insert(&mut self, addr: CompiledAddr) {
51        self.0 = addr;
52    }
53}