Skip to content

Commit

Permalink
Optimize Rc::<str>::default() implementation
Browse files Browse the repository at this point in the history
This PR lets `impl Default for Rc<str>` re-use the implementation
for `Rc::<[u8]>::default()`. The previous version only calculted the
memory layout at runtime, even though it should be known at compile
time, resulting in an additional function call.

Resolves <#135784>.
  • Loading branch information
Kijewski committed Jan 26, 2025
1 parent 15c6f7e commit 72d71e3
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,9 @@ impl Default for Rc<str> {
/// This may or may not share an allocation with other Rcs on the same thread.
#[inline]
fn default() -> Self {
Rc::from("")
let rc = Rc::<[u8]>::default();
// `[u8]` has the same layout as `str`.
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const str) }
}
}

Expand Down

0 comments on commit 72d71e3

Please sign in to comment.