mirror of
https://github.com/rust-lang/rust.git
synced 2026-02-15 00:44:18 +00:00
Optimize Rc::<str>::default() implementation
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. The same optimization is done for `Rc<CStr>`. Generated byte code: <https://godbolt.org/z/dfq73jsoP>. Resolves <https://github.com/rust-lang/rust/issues/135784>.
This commit is contained in:
parent
15c6f7e1a3
commit
e090db8d22
@ -965,8 +965,9 @@ impl Default for Rc<CStr> {
|
||||
/// This may or may not share an allocation with other Rcs on the same thread.
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
let c_str: &CStr = Default::default();
|
||||
Rc::from(c_str)
|
||||
let rc = Rc::<[u8]>::from(*b"\0");
|
||||
// `[u8]` has the same layout as `CStr`, and it is `NUL` terminated.
|
||||
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user