Skip to content

Commit a3afe21

Browse files
committed
Debug interner struct
1 parent 539ab8a commit a3afe21

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

cobalt-ast/src/types/agg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ impl Type for SizedArray {
773773
eprintln!("base kind: {}, our int kind: {}, real int kind: {}", base.kind(), self.elem().kind(), types::Int::KIND);
774774
dbg!(base == self.elem());
775775
eprintln!("base: {base:p}, elem: {:p}", self.elem());
776+
eprintln!("{:?}", types::int::INTERN);
776777
}
777778
dbg!(target.is_and::<types::Pointer>(|r| r.base() == self.elem()))
778779
}

cobalt-ast/src/types/int.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use inkwell::IntPredicate::*;
3+
pub(crate) static INTERN: Interner<(u16, bool)> = Interner::new();
34
#[derive(Debug, ConstIdentify, PartialEq, Eq, Hash, Display, RefCastCustom)]
45
#[display(fmt = "{}{}", r#"if _0.1 {"u"} else {"i"}"#, "_0.0")]
56
#[repr(transparent)]
@@ -9,7 +10,6 @@ impl Int {
910
fn from_ref(val: &(u16, bool)) -> &Self;
1011
pub fn new(bits: u16, unsigned: bool) -> &'static Self {
1112
eprintln!("new int: {bits}, {unsigned}");
12-
static INTERN: Interner<(u16, bool)> = Interner::new();
1313
let ret = Self::from_ref(INTERN.intern((bits, unsigned)));
1414
eprintln!("finished making int");
1515
ret

cobalt-utils/src/intern.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use hashbrown::raw::*;
1+
use hashbrown::HashTable;
22
use once_cell::sync::Lazy;
33
use std::hash::{Hash, Hasher};
44
use std::sync::RwLock;
5+
use std::fmt::{self, Debug, Formatter};
56
#[inline]
67
fn hash(val: &impl Hash) -> u64 {
78
let mut state = std::collections::hash_map::DefaultHasher::default();
@@ -10,27 +11,32 @@ fn hash(val: &impl Hash) -> u64 {
1011
}
1112
pub struct Interner<'a, T: PartialEq + Eq + Hash> {
1213
vec: Lazy<aovec::Aovec<T>>,
13-
map: RwLock<RawTable<(&'a T, usize)>>,
14+
map: RwLock<HashTable<(&'a T, usize)>>,
15+
}
16+
impl<T: Debug + PartialEq + Eq + Hash> Debug for Interner<'_, T> {
17+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
18+
f.debug_struct("Interner").field("map", &self.map.read().unwrap()).finish_non_exhaustive()
19+
}
1420
}
1521
impl<'a, K: PartialEq + Eq + Hash> Interner<'a, K> {
1622
pub const fn new() -> Self {
1723
Self {
1824
vec: Lazy::new(|| aovec::Aovec::new(16)),
19-
map: RwLock::new(RawTable::new()),
25+
map: RwLock::new(HashTable::new()),
2026
}
2127
}
2228
pub fn intern(&'a self, key: K) -> &K {
2329
let hashed = hash(&key);
2430
let lock = self.map.read().unwrap();
25-
if let Some(k) = lock.get(hashed, |v| v.0 == &key).map(|x| x.1) {
31+
if let Some(k) = lock.find(hashed, |v| v.0 == &key).map(|x| x.1) {
2632
&self.vec[k]
2733
} else {
2834
eprintln!("creating new element!");
2935
std::mem::drop(lock);
3036
let mut lock = self.map.write().unwrap();
3137
let idx = self.vec.push(key);
3238
let val = &self.vec[idx];
33-
lock.insert(hashed, (val, idx), |v| hash(&v.0));
39+
lock.insert_unique(hashed, (val, idx), |v| hash(&v.0));
3440
val
3541
}
3642
}
@@ -45,7 +51,7 @@ impl<'a, K: PartialEq + Eq + Hash> Interner<'a, K> {
4551
let hashed = hash(&key);
4652
let lock = self.map.read().unwrap();
4753
if let Some(k) = lock
48-
.get(hashed, |v| key.as_ref() == v.0.as_ref())
54+
.find(hashed, |v| key.as_ref() == v.0.as_ref())
4955
.map(|x| x.1)
5056
{
5157
&self.vec[k]
@@ -54,7 +60,7 @@ impl<'a, K: PartialEq + Eq + Hash> Interner<'a, K> {
5460
let mut lock = self.map.write().unwrap();
5561
let idx = self.vec.push(key.into());
5662
let val = &self.vec[idx];
57-
lock.insert(hashed, (val, idx), |v| hash(&v.0));
63+
lock.insert_unique(hashed, (val, idx), |v| hash(&v.0));
5864
val
5965
}
6066
}

0 commit comments

Comments
 (0)