about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/interner/tests.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-03-01T16·31+0200
committertazjin <mail@tazj.in>2021-03-01T21·09+0000
commitef7a0da8cb22601d8cdd466644413877126c70df (patch)
tree82efc70429434293452f717c36e5d5f77bbcf533 /users/tazjin/rlox/src/bytecode/interner/tests.rs
parent6f600c8300c028beb07bf224baf7dfdaa6490fd3 (diff)
feat(tazjin/rlox): Add a simple string interner r/2259
This is based on this matklad post:

https://matklad.github.io/2020/03/22/fast-simple-rust-interner.html

It's modified slightly to provide a safer interface and slightly more
readable implementation:

* interned string IDs are wrapped in a newtype that is not publicly
  constructible

* unsafe block is reduced to only the small scope in which it is
  needed

* lookup lifetime is pinned explicitly to make the intent clearer when
  reading this code

Change-Id: Ia3dae988f33f8e5e7d8dc0c1a9216914a945b036
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2578
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'users/tazjin/rlox/src/bytecode/interner/tests.rs')
-rw-r--r--users/tazjin/rlox/src/bytecode/interner/tests.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/interner/tests.rs b/users/tazjin/rlox/src/bytecode/interner/tests.rs
new file mode 100644
index 000000000000..b34bf6835389
--- /dev/null
+++ b/users/tazjin/rlox/src/bytecode/interner/tests.rs
@@ -0,0 +1,24 @@
+use super::*;
+
+#[test]
+fn interns_strings() {
+    let mut interner = Interner::with_capacity(128);
+    let id = interner.intern("hello world");
+    assert_eq!("hello world", interner.lookup(id));
+}
+
+#[test]
+fn deduplicates_strings() {
+    let mut interner = Interner::with_capacity(128);
+    let id_1 = interner.intern("hello world");
+    let id_2 = interner.intern("hello world");
+    assert_eq!(id_1, id_2);
+}
+
+#[test]
+fn ids_survive_growing() {
+    let mut interner = Interner::with_capacity(16);
+    let id = interner.intern("hello");
+    interner.intern("excessively large string that will cause eallocation");
+    assert_eq!("hello", interner.lookup(id));
+}