Making progress on using interning in gen_uplc

Done interning for uniqueness. Now to fix the static optimization

Remove unused function

Fixing issues. Have a few remaining tests
This commit is contained in:
microproofs
2024-09-09 17:29:15 -04:00
parent 0905146140
commit 7c52094b15
3 changed files with 756 additions and 433 deletions

View File

@@ -0,0 +1,57 @@
use std::collections::HashMap;
use vec1::{vec1, Vec1};
#[derive(Clone)]
pub struct AirInterner {
identifiers: HashMap<String, Vec1<usize>>,
current: usize,
}
impl Default for AirInterner {
fn default() -> Self {
Self::new()
}
}
/// Interner that uses previous uniques to prevent future unique collisions
/// when performing optimizations
impl AirInterner {
pub fn new() -> Self {
Self {
identifiers: HashMap::new(),
current: 0,
}
}
pub fn intern(&mut self, text: String) {
if let Some(u) = self.identifiers.get_mut(&text) {
u.push(self.current);
self.current += 1;
} else {
self.identifiers.insert(text, vec1!(self.current));
self.current += 1;
}
}
pub fn pop_text(&mut self, text: String) {
if let Some(mut u) = self.identifiers.remove(&text) {
if u.len() != 1 {
u.pop().unwrap();
self.identifiers.insert(text, u);
}
} else {
unreachable!("Looking up a missing text: {}", text);
}
}
pub fn lookup_interned(&self, text: &String) -> String {
if let Some(u) = self.identifiers.get(text) {
format!("{}_id_{}", text, *u.last())
} else {
unreachable!("Looking up a missing text: {}", text);
}
}
}