Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(hstr): Disable interning for global store #10036

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions crates/hstr/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ impl Deref for Item {
}
}

/// TODO: Use real weak pointer
type WeakItem = Item;

impl Hash for Item {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.0.header.header.header.hash);
Expand All @@ -56,7 +53,7 @@ pub(crate) unsafe fn restore_arc(v: TaggedValue) -> Item {
///
/// # Merging [AtomStore]
pub struct AtomStore {
pub(crate) data: hashbrown::HashMap<WeakItem, (), BuildEntryHasher>,
pub(crate) data: hashbrown::HashMap<Item, (), BuildEntryHasher>,
}

impl Default for AtomStore {
Expand All @@ -70,13 +67,13 @@ impl Default for AtomStore {
impl AtomStore {
#[inline(always)]
pub fn atom<'a>(&mut self, text: impl Into<Cow<'a, str>>) -> Atom {
new_atom(self, text.into())
atom_in_store(self, text.into())
}
}

/// This can create any kind of [Atom], although this lives in the `dynamic`
/// module.
pub(crate) fn new_atom<S>(storage: S, text: Cow<str>) -> Atom
fn atom_in_store<S>(storage: S, text: Cow<str>) -> Atom
where
S: Storage,
{
Expand Down Expand Up @@ -113,12 +110,18 @@ pub(crate) trait Storage {
impl Storage for &'_ mut AtomStore {
#[inline(never)]
fn insert_entry(self, text: Cow<str>, hash: u64) -> Item {
macro_rules! make {
() => {{
Item(ThinArc::from_header_and_slice(
HeaderWithLength::new(Metadata { hash }, text.len()),
text.as_bytes(),
))
}};
}

// If the text is too long, interning is not worth it.
if text.len() > 512 {
return Item(ThinArc::from_header_and_slice(
HeaderWithLength::new(Metadata { hash }, text.len()),
text.as_bytes(),
));
return make!();
}

let (entry, _) = self
Expand All @@ -127,15 +130,7 @@ impl Storage for &'_ mut AtomStore {
.from_hash(hash, |key| {
key.header.header.header.hash == hash && key.slice == *text.as_bytes()
})
.or_insert_with(move || {
(
Item(ThinArc::from_header_and_slice(
HeaderWithLength::new(Metadata { hash }, text.len()),
text.as_bytes(),
)),
(),
)
});
.or_insert_with(move || (make!(), ()));
entry.clone()
}
}
Expand All @@ -147,7 +142,24 @@ fn calc_hash(text: &str) -> u64 {
hasher.finish()
}

type BuildEntryHasher = BuildHasherDefault<EntryHasher>;
/// Do not intern.
struct GlobalAtomStore;

impl Storage for GlobalAtomStore {
#[inline(never)]
fn insert_entry(self, text: Cow<str>, hash: u64) -> Item {
Item(ThinArc::from_header_and_slice(
HeaderWithLength::new(Metadata { hash }, text.len()),
text.as_bytes(),
))
}
}

pub(crate) fn global_atom(text: Cow<str>) -> Atom {
atom_in_store(GlobalAtomStore, text)
}

pub(crate) type BuildEntryHasher = BuildHasherDefault<EntryHasher>;

/// A "no-op" hasher for [Entry] that returns [Entry::hash]. The design is
/// inspired by the `nohash-hasher` crate.
Expand Down
20 changes: 4 additions & 16 deletions crates/hstr/src/global_store.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
use std::{borrow::Cow, cell::RefCell};
use std::borrow::Cow;

use crate::{Atom, AtomStore};

fn atom(text: Cow<str>) -> Atom {
thread_local! {
static GLOBAL_DATA: RefCell<AtomStore> = Default::default();
}

GLOBAL_DATA.with(|global| {
let mut store = global.borrow_mut();

store.atom(text)
})
}
use crate::{dynamic::global_atom, Atom};

macro_rules! direct_from_impl {
($T:ty) => {
impl From<$T> for Atom {
fn from(s: $T) -> Self {
atom(s.into())
global_atom(s.into())
}
}
};
Expand All @@ -30,6 +18,6 @@ direct_from_impl!(String);

impl From<Box<str>> for crate::Atom {
fn from(s: Box<str>) -> Self {
atom(Cow::Owned(String::from(s)))
global_atom(Cow::Owned(String::from(s)))
}
}
Loading