forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(rcstr): Support inline string (vercel#74482)
### What? Support inline string for `RcStr`. Most of the code is copied from https://github.com/dudykr/ddbase/tree/67c7cec871073f5c8c93d030d40fb8226170797f/crates/hstr/src (I own the code) ### Why? To see at the perf difference. #### Command: `ddt profile instruments run -t Memory -- $(which node) $(which pnpm) build-turbopack` - Old: <img width="1840" alt="image" src="https://github.com/user-attachments/assets/e9946dcf-7509-42ed-8648-6f6589de6313" /> - New: <img width="1840" alt="image" src="https://github.com/user-attachments/assets/08ea782e-9c66-4537-94a3-9f63956aa11a" /> ### How?
- Loading branch information
Showing
5 changed files
with
342 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::ptr::NonNull; | ||
|
||
use triomphe::Arc; | ||
|
||
use crate::{ | ||
tagged_value::{TaggedValue, MAX_INLINE_LEN}, | ||
RcStr, INLINE_TAG_INIT, LEN_OFFSET, TAG_MASK, | ||
}; | ||
|
||
pub unsafe fn cast(ptr: TaggedValue) -> *const String { | ||
ptr.get_ptr().cast() | ||
} | ||
|
||
pub unsafe fn deref_from<'i>(ptr: TaggedValue) -> &'i String { | ||
&*cast(ptr) | ||
} | ||
|
||
/// Caller should call `forget` (or `clone`) on the returned `Arc` | ||
pub unsafe fn restore_arc(v: TaggedValue) -> Arc<String> { | ||
let ptr = v.get_ptr() as *const String; | ||
Arc::from_raw(ptr) | ||
} | ||
|
||
/// This can create any kind of [Atom], although this lives in the `dynamic` | ||
/// module. | ||
pub(crate) fn new_atom<T: AsRef<str> + Into<String>>(text: T) -> RcStr { | ||
let len = text.as_ref().len(); | ||
|
||
if len < MAX_INLINE_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_ref().as_bytes()); | ||
} | ||
return RcStr { unsafe_data }; | ||
} | ||
|
||
let entry = Arc::new(text.into()); | ||
let entry = Arc::into_raw(entry); | ||
|
||
let ptr: NonNull<String> = unsafe { | ||
// Safety: Arc::into_raw returns a non-null pointer | ||
NonNull::new_unchecked(entry as *mut String) | ||
}; | ||
debug_assert!(0 == ptr.as_ptr() as u8 & TAG_MASK); | ||
RcStr { | ||
unsafe_data: TaggedValue::new_ptr(ptr), | ||
} | ||
} |
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
Oops, something went wrong.