Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@ryansg.com>2020-01-24 11:09:24 -0500
committerMike Gerwitz <mike.gerwitz@ryansg.com>2020-02-25 16:10:55 -0500
commit6aae741162595942021b5d42b26d1ceb458d61ba (patch)
treee7988b8176f5b9e582007934644c855ff13ea27a
parente8cd378d5952f3ae51d4cfecf29d8f70426de8e9 (diff)
downloadtame-6aae741162595942021b5d42b26d1ceb458d61ba.tar.gz
tame-6aae741162595942021b5d42b26d1ceb458d61ba.tar.bz2
tame-6aae741162595942021b5d42b26d1ceb458d61ba.zip
TAMER (sym::Interner::intern_utf8_unchecked): New function
This removes boilerplate for reading xmlo files. See next commit.
-rw-r--r--tamer/benches/sym.rs10
-rw-r--r--tamer/src/sym.rs24
2 files changed, 34 insertions, 0 deletions
diff --git a/tamer/benches/sym.rs b/tamer/benches/sym.rs
index 2e1c894..d96b7d2 100644
--- a/tamer/benches/sym.rs
+++ b/tamer/benches/sym.rs
@@ -148,6 +148,16 @@ mod interner {
});
}
+ #[bench]
+ fn with_one_new_1000_utf8_unchecked(bench: &mut Bencher) {
+ bench.iter(|| {
+ let sut = ArenaInterner::<FxBuildHasher>::new();
+ (0..1000)
+ .map(|_| unsafe { sut.intern_utf8_unchecked(b"first") })
+ .for_each(drop);
+ });
+ }
+
/// Since Fx is the best-performing, let's build upon it to demonstrate
/// the benefits of with_capacity
#[bench]
diff --git a/tamer/src/sym.rs b/tamer/src/sym.rs
index 13e2c02..023ebae 100644
--- a/tamer/src/sym.rs
+++ b/tamer/src/sym.rs
@@ -427,6 +427,20 @@ pub trait Interner<'i> {
/// This count will increase each time a unique string is interned.
/// It does not increase when a string is already interned.
fn len(&self) -> usize;
+
+ /// Intern an assumed-UTF8 slice of bytes or return an existing
+ /// [`Symbol`].
+ ///
+ /// Safety
+ /// ======
+ /// This function is unsafe because it uses
+ /// [`std::str::from_utf8_unchecked`].
+ /// It is provided for convenience when interning from trusted binary
+ /// data
+ /// (such as object files).
+ unsafe fn intern_utf8_unchecked(&'i self, value: &[u8]) -> &'i Symbol<'i> {
+ self.intern(std::str::from_utf8_unchecked(value))
+ }
}
/// An interner backed by an [arena](bumpalo).
@@ -772,5 +786,15 @@ mod test {
// note that this is not publicly available
assert!(sut.map.borrow().capacity() >= n);
}
+
+ #[test]
+ fn intern_utf8_unchecked() {
+ let sut = Sut::new();
+
+ let a = sut.intern("foo");
+ let b = unsafe { sut.intern_utf8_unchecked(b"foo") };
+
+ assert_eq!(a, b);
+ }
}
}