mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-16 18:35:02 +01:00
fix string interning
This commit is contained in:
parent
d1179ff2ea
commit
9ed7998401
2 changed files with 34 additions and 5 deletions
29
src/gc.rs
29
src/gc.rs
|
|
@ -74,10 +74,29 @@ enum HeapObjectKind {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RtAlloc {
|
pub struct RtAlloc {
|
||||||
symbols: HashSet<NonNull<str>>,
|
symbols: HashSet<NonNullStrWrapper>,
|
||||||
objects: LinkedList<HeapObject>,
|
objects: LinkedList<HeapObject>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct NonNullStrWrapper(NonNull<str>);
|
||||||
|
|
||||||
|
impl Hash for NonNullStrWrapper {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
// SAFETY: Assume the ptr is valid, same rules as `Gc<T>`
|
||||||
|
unsafe { self.0.as_ref().hash(state) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for NonNullStrWrapper {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
// SAFETY: Assume the ptr is valid, same rules as `Gc<T>`
|
||||||
|
unsafe { self.0.as_ref().eq(other.0.as_ref()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for NonNullStrWrapper {}
|
||||||
|
|
||||||
impl RtAlloc {
|
impl RtAlloc {
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// Promise to not forget to mark any roots and to not deref `Gc<T>` after you've dropped me 🥺
|
/// Promise to not forget to mark any roots and to not deref `Gc<T>` after you've dropped me 🥺
|
||||||
|
|
@ -119,10 +138,12 @@ impl RtAlloc {
|
||||||
pub fn intern_string(&mut self, str: &str) -> Symbol {
|
pub fn intern_string(&mut self, str: &str) -> Symbol {
|
||||||
let original_nonnull = NonNull::from(str);
|
let original_nonnull = NonNull::from(str);
|
||||||
|
|
||||||
if let Some(interned) = self.symbols.get(&original_nonnull) {
|
if let Some(interned) = self.symbols.get(&NonNullStrWrapper(original_nonnull)) {
|
||||||
Symbol::new(Gc { ptr: *interned })
|
Symbol::new(Gc { ptr: interned.0 })
|
||||||
} else {
|
} else {
|
||||||
Symbol::new(self.alloc_str(str))
|
let allocated = self.alloc_str(str);
|
||||||
|
self.symbols.insert(NonNullStrWrapper(allocated.ptr));
|
||||||
|
Symbol::new(allocated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
test.dil
10
test.dil
|
|
@ -1 +1,9 @@
|
||||||
print "Hello World!";
|
let x = 5;
|
||||||
|
print x + 4;
|
||||||
|
|
||||||
|
let test = x + 6 * 159353.004;
|
||||||
|
print test;
|
||||||
|
|
||||||
|
if x > test {
|
||||||
|
print "lmao";
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue