Skip to content
New issue

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

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

Already on GitHub? # to your account

docs(turbo-tasks): Improve RcStr docs #72825

Merged
merged 1 commit into from
Nov 14, 2024
Merged
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
43 changes: 37 additions & 6 deletions turbopack/crates/turbo-tasks/src/rcstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,36 @@ use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};

use crate::debug::{ValueDebugFormat, ValueDebugFormatString};

/// A reference counted [`String`], similar to [`Arc<String>`][std::sync::Arc].
/// An immutable reference counted [`String`], similar to [`Arc<String>`][std::sync::Arc].
///
/// This type is intentionally opaque to allow for optimizations to the
/// underlying representation. Future implementations may use inline
/// representations or interning.
/// This is the preferred immutable string type for [`turbo_task::function`][macro@crate::function]
/// arguments and inside of [`turbo_task::value`][macro@crate::value].
///
/// As turbo-tasks must store copies of function arguments to enable caching, non-reference counted
/// [`String`]s would incur frequent cloning. Reference counting typically decreases memory
/// consumption and CPU time in these cases.
///
/// ## Conversion
///
/// Converting a `String` or `&str` to an `RcStr` can be perfomed using `.into()` or
/// `RcStr::from(...)`:
///
/// ```
/// # use turbo_tasks::RcStr;
/// #
/// let s = "foo";
/// let rc_s1: RcStr = s.into();
/// let rc_s2 = RcStr::from(s);
/// assert_eq!(rc_s1, rc_s2);
/// ```
///
/// Converting from an [`RcStr`] to a `&str` should be done with [`RcStr::as_str`]. Converting to a
/// `String` should be done with [`RcStr::into_owned`].
///
/// ## Future Optimizations
///
/// This type is intentionally opaque to allow for optimizations to the underlying representation.
/// Future implementations may use inline representations or interning.
//
// If you want to change the underlying string type to `Arc<str>`, please ensure that you profile
// performance. The current implementation offers very cheap `String -> RcStr -> String`, meaning we
Expand All @@ -30,11 +55,17 @@ impl RcStr {
self.0.as_str()
}

/// This implementation is more efficient than `.to_string()`
/// Returns an owned mutable [`String`].
///
/// This implementation is more efficient than [`ToString::to_string`]:
///
/// - If the reference count is 1, the `Arc` can be unwrapped, giving ownership of the
/// underlying string without cloning in `O(1)` time.
/// - This avoids some of the potential overhead of the `Display` trait.
pub fn into_owned(self) -> String {
match Arc::try_unwrap(self.0) {
Ok(v) => v,
Err(arc) => arc.to_string(),
Err(arc) => (*arc).clone(),
}
}

Expand Down
Loading