Unverified Commit 7d3e330a authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

perf(turbopack/rcstr): Precompute hash for equality bailout (#78943)

### What?

Precompute hash for `RcStr` and use it for equality bailout.

### Why?


We have lots of `RcStr` in key positions of the hashmap, so adding a fast bailout path to `==` improved the performance.

x-ref: https://vercel.slack.com/archives/C06PPGZ0FD3/p1746715835436899?thread_ts=1746658944.635819&cid=C06PPGZ0FD3
parent 602a5a63
...@@ -9562,6 +9562,7 @@ dependencies = [ ...@@ -9562,6 +9562,7 @@ dependencies = [
"codspeed-criterion-compat", "codspeed-criterion-compat",
"napi", "napi",
"new_debug_unreachable", "new_debug_unreachable",
"rustc-hash 2.1.1",
"serde", "serde",
"shrink-to-fit", "shrink-to-fit",
"triomphe 0.1.12", "triomphe 0.1.12",
......
...@@ -16,6 +16,7 @@ serde = { workspace = true } ...@@ -16,6 +16,7 @@ serde = { workspace = true }
new_debug_unreachable = "1.0.6" new_debug_unreachable = "1.0.6"
shrink-to-fit = { workspace = true } shrink-to-fit = { workspace = true }
napi = { workspace = true, optional = true } napi = { workspace = true, optional = true }
rustc-hash = { workspace = true }
[dev-dependencies] [dev-dependencies]
criterion = { workspace = true } criterion = { workspace = true }
......
use std::{num::NonZeroU8, ptr::NonNull}; use std::{
hash::{Hash, Hasher},
num::NonZeroU8,
ptr::NonNull,
};
use rustc_hash::FxHasher;
use triomphe::Arc; use triomphe::Arc;
use crate::{ use crate::{
...@@ -7,17 +12,22 @@ use crate::{ ...@@ -7,17 +12,22 @@ use crate::{
tagged_value::{MAX_INLINE_LEN, TaggedValue}, tagged_value::{MAX_INLINE_LEN, TaggedValue},
}; };
pub unsafe fn cast(ptr: TaggedValue) -> *const String { pub(crate) struct PrehashedString {
pub value: String,
pub hash: u64,
}
pub unsafe fn cast(ptr: TaggedValue) -> *const PrehashedString {
ptr.get_ptr().cast() ptr.get_ptr().cast()
} }
pub unsafe fn deref_from<'i>(ptr: TaggedValue) -> &'i String { pub(crate) unsafe fn deref_from<'i>(ptr: TaggedValue) -> &'i PrehashedString {
unsafe { &*cast(ptr) } unsafe { &*cast(ptr) }
} }
/// Caller should call `forget` (or `clone`) on the returned `Arc` /// Caller should call `forget` (or `clone`) on the returned `Arc`
pub unsafe fn restore_arc(v: TaggedValue) -> Arc<String> { pub unsafe fn restore_arc(v: TaggedValue) -> Arc<PrehashedString> {
let ptr = v.get_ptr() as *const String; let ptr = v.get_ptr() as *const PrehashedString;
unsafe { Arc::from_raw(ptr) } unsafe { Arc::from_raw(ptr) }
} }
...@@ -36,12 +46,17 @@ pub(crate) fn new_atom<T: AsRef<str> + Into<String>>(text: T) -> RcStr { ...@@ -36,12 +46,17 @@ pub(crate) fn new_atom<T: AsRef<str> + Into<String>>(text: T) -> RcStr {
return RcStr { unsafe_data }; return RcStr { unsafe_data };
} }
let entry = Arc::new(text.into()); let hash = compute_fxhash(text.as_ref());
let entry: Arc<PrehashedString> = Arc::new(PrehashedString {
value: text.into(),
hash,
});
let entry = Arc::into_raw(entry); let entry = Arc::into_raw(entry);
let ptr: NonNull<String> = unsafe { let ptr: NonNull<PrehashedString> = unsafe {
// Safety: Arc::into_raw returns a non-null pointer // Safety: Arc::into_raw returns a non-null pointer
NonNull::new_unchecked(entry as *mut String) NonNull::new_unchecked(entry as *mut _)
}; };
debug_assert!(0 == ptr.as_ptr() as u8 & TAG_MASK); debug_assert!(0 == ptr.as_ptr() as u8 & TAG_MASK);
RcStr { RcStr {
...@@ -71,3 +86,9 @@ pub(crate) const fn inline_atom(text: &str) -> Option<RcStr> { ...@@ -71,3 +86,9 @@ pub(crate) const fn inline_atom(text: &str) -> Option<RcStr> {
} }
None None
} }
fn compute_fxhash(s: &str) -> u64 {
let mut hasher = FxHasher::default();
s.hash(&mut hasher);
hasher.finish()
}
...@@ -15,7 +15,10 @@ use shrink_to_fit::ShrinkToFit; ...@@ -15,7 +15,10 @@ use shrink_to_fit::ShrinkToFit;
use triomphe::Arc; use triomphe::Arc;
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher}; use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};
use crate::{dynamic::new_atom, tagged_value::TaggedValue}; use crate::{
dynamic::{deref_from, new_atom},
tagged_value::TaggedValue,
};
mod dynamic; mod dynamic;
mod tagged_value; mod tagged_value;
...@@ -80,7 +83,7 @@ impl RcStr { ...@@ -80,7 +83,7 @@ impl RcStr {
#[inline(never)] #[inline(never)]
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
match self.tag() { match self.tag() {
DYNAMIC_TAG => unsafe { dynamic::deref_from(self.unsafe_data) }, DYNAMIC_TAG => unsafe { dynamic::deref_from(self.unsafe_data).value.as_str() },
INLINE_TAG => { INLINE_TAG => {
let len = (self.unsafe_data.tag() & LEN_MASK) >> LEN_OFFSET; let len = (self.unsafe_data.tag() & LEN_MASK) >> LEN_OFFSET;
let src = self.unsafe_data.data(); let src = self.unsafe_data.data();
...@@ -103,8 +106,8 @@ impl RcStr { ...@@ -103,8 +106,8 @@ impl RcStr {
// convert `self` into `arc` // convert `self` into `arc`
let arc = unsafe { dynamic::restore_arc(ManuallyDrop::new(self).unsafe_data) }; let arc = unsafe { dynamic::restore_arc(ManuallyDrop::new(self).unsafe_data) };
match Arc::try_unwrap(arc) { match Arc::try_unwrap(arc) {
Ok(v) => v, Ok(v) => v.value,
Err(arc) => arc.to_string(), Err(arc) => arc.value.to_string(),
} }
} }
INLINE_TAG => self.as_str().to_string(), INLINE_TAG => self.as_str().to_string(),
...@@ -256,7 +259,15 @@ impl Default for RcStr { ...@@ -256,7 +259,15 @@ impl Default for RcStr {
impl PartialEq for RcStr { impl PartialEq for RcStr {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str() match (self.tag(), other.tag()) {
(DYNAMIC_TAG, DYNAMIC_TAG) => {
let l = unsafe { deref_from(self.unsafe_data) };
let r = unsafe { deref_from(other.unsafe_data) };
l.hash == r.hash && l.value == r.value
}
(INLINE_TAG, INLINE_TAG) => self.unsafe_data == other.unsafe_data,
_ => false,
}
} }
} }
...@@ -276,7 +287,16 @@ impl Ord for RcStr { ...@@ -276,7 +287,16 @@ impl Ord for RcStr {
impl Hash for RcStr { impl Hash for RcStr {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state); match self.tag() {
DYNAMIC_TAG => {
let l = unsafe { deref_from(self.unsafe_data) };
l.value.as_str().hash(state);
}
INLINE_TAG => {
self.as_str().hash(state);
}
_ => unsafe { debug_unreachable!() },
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment