-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
string-interner
dependency and implement custom string `Inte…
…rner` (#2147) So, @raskad and myself had a short discussion about the state of #736, and we came to the conclusion that it would be a good time to implement our own string interner; partly because the `string-interner` crate is a bit unmaintained (as shown by Robbepop/string-interner#42 and Robbepop/string-interner#47), and partly because it would be hard to experiment with custom optimizations for UTF-16 strings. I still want to thank @Robbepop for the original implementation though, because some parts of this design have been shamelessly stolen from it 😅. Having said that, this PR is a complete reimplementation of the interner, but with some modifications to (hopefully!) make it a bit easier to experiment with UTF-16 strings, apply optimizations, and whatnot :)
- Loading branch information
Showing
13 changed files
with
488 additions
and
314 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
//! Garbage collector for the Boa JavaScript engine. | ||
pub use gc::{ | ||
custom_trace, force_collect, unsafe_empty_trace, Finalize, Gc, GcCell as Cell, | ||
custom_trace, finalizer_safe, force_collect, unsafe_empty_trace, Finalize, Gc, GcCell as Cell, | ||
GcCellRef as Ref, GcCellRefMut as RefMut, Trace, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::interned_str::InternedStr; | ||
|
||
#[derive(Debug, Default)] | ||
pub(super) struct FixedString { | ||
inner: String, | ||
} | ||
|
||
impl FixedString { | ||
/// Creates a new, pinned [`FixedString`]. | ||
pub(super) fn new(capacity: usize) -> Self { | ||
Self { | ||
inner: String::with_capacity(capacity), | ||
} | ||
} | ||
|
||
/// Gets the maximum capacity of the [`FixedString`]. | ||
pub(super) fn capacity(&self) -> usize { | ||
self.inner.capacity() | ||
} | ||
|
||
/// Returns `true` if the [`FixedString`] has length zero, | ||
/// and `false` otherwise. | ||
pub(super) fn is_empty(&self) -> bool { | ||
self.inner.is_empty() | ||
} | ||
|
||
/// Tries to push `string` to the [`FixedString`], and returns | ||
/// an [`InternedStr`] pointer to the stored `string`, or | ||
/// `None` if the capacity is not enough to store `string`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The caller is responsible for ensuring `self` outlives the returned | ||
/// `InternedStr`. | ||
pub(super) unsafe fn push(&mut self, string: &str) -> Option<InternedStr> { | ||
let capacity = self.inner.capacity(); | ||
(capacity >= self.inner.len() + string.len()).then(|| { | ||
let old_len = self.inner.len(); | ||
self.inner.push_str(string); | ||
// SAFETY: The caller is responsible for extending the lifetime | ||
// of `self` to outlive the return value. | ||
unsafe { InternedStr::new(self.inner[old_len..self.inner.len()].into()) } | ||
}) | ||
} | ||
|
||
/// Pushes `string` to the [`FixedString`], and returns | ||
/// an [`InternedStr`] pointer to the stored `string`, without | ||
/// checking if the total `capacity` is enough to store `string`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The caller is responsible for ensuring that `self` outlives the returned | ||
/// `InternedStr` and that it has enough capacity to store `string` without | ||
/// reallocating. | ||
pub(super) unsafe fn push_unchecked(&mut self, string: &str) -> InternedStr { | ||
let old_len = self.inner.len(); | ||
self.inner.push_str(string); | ||
// SAFETY: The caller is responsible for extending the lifetime | ||
// of `self` to outlive the return value. | ||
unsafe { InternedStr::new(self.inner[old_len..self.inner.len()].into()) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::{borrow::Borrow, ptr::NonNull}; | ||
|
||
/// Wrapper for an interned str pointer, required to | ||
/// quickly check using a hash if a string is inside an [`Interner`][`super::Interner`]. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This struct could cause Undefined Behaviour on: | ||
/// - Use without ensuring the referenced memory is still allocated. | ||
/// - Construction of an [`InternedStr`] from an invalid [`NonNull<str>`]. | ||
/// | ||
/// In general, this should not be used outside of an [`Interner`][`super::Interner`]. | ||
#[derive(Debug, Clone)] | ||
pub(super) struct InternedStr { | ||
ptr: NonNull<str>, | ||
} | ||
|
||
impl InternedStr { | ||
/// Create a new interned string from the given `str`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Not maintaining the invariants specified on the struct definition | ||
/// could cause Undefined Behaviour. | ||
#[inline] | ||
pub(super) unsafe fn new(ptr: NonNull<str>) -> Self { | ||
Self { ptr } | ||
} | ||
|
||
/// Returns a shared reference to the underlying string. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Not maintaining the invariants specified on the struct definition | ||
/// could cause Undefined Behaviour. | ||
#[inline] | ||
pub(super) unsafe fn as_str(&self) -> &str { | ||
// SAFETY: The caller must verify the invariants | ||
// specified on the struct definition. | ||
unsafe { self.ptr.as_ref() } | ||
} | ||
} | ||
|
||
impl std::hash::Hash for InternedStr { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
// SAFETY: The caller must verify the invariants | ||
// specified in the struct definition. | ||
unsafe { | ||
self.as_str().hash(state); | ||
} | ||
} | ||
} | ||
|
||
impl Eq for InternedStr {} | ||
|
||
impl PartialEq for InternedStr { | ||
fn eq(&self, other: &Self) -> bool { | ||
// SAFETY: The caller must verify the invariants | ||
// specified in the struct definition. | ||
unsafe { self.as_str() == other.as_str() } | ||
} | ||
} | ||
|
||
impl Borrow<str> for InternedStr { | ||
fn borrow(&self) -> &str { | ||
// SAFETY: The caller must verify the invariants | ||
// specified in the struct definition. | ||
unsafe { self.as_str() } | ||
} | ||
} |
Oops, something went wrong.