Unverified Commit 6d916f99 authored by vercel[bot]'s avatar vercel[bot] Committed by GitHub

Reuse owned string buffers in RcStr

Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent cf10c50a
......@@ -46,6 +46,13 @@ fn bench_construct(c: &mut Criterion) {
g.bench_with_input("from/large", "large", |f, _| {
f.iter(|| RcStr::from("this is a long string that will take time to copy"));
});
g.bench_function("from/owned-large", |f| {
f.iter_batched(
|| String::from("this is a long string that will take time to copy"),
RcStr::from,
BatchSize::SmallInput,
);
});
}
criterion_group!(
name = benches;
......
......@@ -63,14 +63,32 @@ pub(crate) fn new_atom<T: AsRef<str> + Into<String>>(text: T) -> RcStr {
let hash = hash_bytes(text.as_bytes());
let prehashed = DynamicPrehashedString {
// NOTE: This will capture as a Box<str> which will essentially
// `shrink_to_fit` the bytes.
value: text.into(),
hash,
};
new_atom_from_prehashed(prehashed)
}
pub(crate) fn new_owned_atom(text: String) -> RcStr {
if is_atom_inlineable(&text) {
let len = text.len();
// INLINE_TAG ensures this is never zero
let tag = INLINE_TAG_INIT | ((len as u8) << LEN_OFFSET);
let mut unsafe_data = TaggedValue::new_tag(tag);
unsafe {
unsafe_data.data_mut()[..len].copy_from_slice(text.as_bytes());
}
return RcStr { unsafe_data };
}
let hash = hash_bytes(text.as_bytes());
let prehashed = DynamicPrehashedString {
value: text.into_boxed_str(),
hash,
};
new_atom_from_prehashed(prehashed)
}
/// Construct a new dynamic RcStr from a DynamicPrehashedString
pub(crate) fn new_atom_from_prehashed(prehashed: DynamicPrehashedString) -> RcStr {
let entry: Arc<DynamicPrehashedString> = Arc::new(prehashed);
......
......@@ -36,7 +36,7 @@ use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};
use crate::{
dynamic::{
DynamicPrehashedString, deref_dynamic, deref_static, hash_bytes, new_atom,
new_atom_from_prehashed, new_static_atom,
new_atom_from_prehashed, new_owned_atom, new_static_atom,
},
tagged_value::{MAX_INLINE_LEN, TaggedValue},
};
......@@ -228,7 +228,7 @@ impl From<BytesStr> for RcStr {
impl From<Arc<String>> for RcStr {
fn from(s: Arc<String>) -> Self {
match Arc::try_unwrap(s) {
Ok(v) => new_atom(Cow::Owned(v)),
Ok(v) => RcStr::from(v),
Err(arc) => new_atom(Cow::Borrowed(&**arc)),
}
}
......@@ -236,7 +236,11 @@ impl From<Arc<String>> for RcStr {
impl From<String> for RcStr {
fn from(s: String) -> Self {
new_atom(Cow::Owned(s))
if s.len() == s.capacity() {
new_owned_atom(s)
} else {
new_atom(Cow::Owned(s))
}
}
}
......@@ -248,7 +252,10 @@ impl From<&'_ str> for RcStr {
impl From<Cow<'_, str>> for RcStr {
fn from(s: Cow<str>) -> Self {
new_atom(s)
match s {
Cow::Borrowed(s) => new_atom(Cow::Borrowed(s)),
Cow::Owned(s) => RcStr::from(s),
}
}
}
......@@ -744,6 +751,33 @@ mod tests {
assert_eq!(refcount(&str), 1);
}
#[test]
fn test_from_owned_reuses_allocation() {
let text = String::from(
String::from("this owned string is too long to be stored inline").into_boxed_str(),
);
assert_eq!(text.len(), text.capacity());
let ptr = text.as_ptr();
let rcstr = RcStr::from(text);
assert_eq!(rcstr.as_ptr(), ptr);
let text = rcstr.into_owned();
assert_eq!(text.as_ptr(), ptr);
}
#[test]
fn test_from_owned_with_spare_capacity_copies() {
let mut text = String::with_capacity(128);
text.push_str("this owned string is too long to be stored inline");
assert!(text.len() < text.capacity());
let ptr = text.as_ptr();
let rcstr = RcStr::from(text);
assert_ne!(rcstr.as_ptr(), ptr);
assert_eq!(rcstr, "this owned string is too long to be stored inline");
}
#[test]
fn test_rcstr() {
// Test enough to exceed the small string optimization
......
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