Skip to content

Commit e32f90c

Browse files
committed
Mark String::clone as inline-able
It calls `Vec::clone` and performs a newtype-wrap around it, so it should be trivial enough that it doesn't cause major size regressions. It also fixes rust-lang#88905
1 parent b0ee495 commit e32f90c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

library/alloc/src/string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,10 +1834,14 @@ impl fmt::Display for FromUtf16Error {
18341834
#[cfg(not(no_global_oom_handling))]
18351835
#[stable(feature = "rust1", since = "1.0.0")]
18361836
impl Clone for String {
1837+
// Not marking it inline causes worse assembly to be generated.
1838+
// https://github.com/rust-lang/rust/issues/88905
1839+
#[inline]
18371840
fn clone(&self) -> Self {
18381841
String { vec: self.vec.clone() }
18391842
}
18401843

1844+
#[inline]
18411845
fn clone_from(&mut self, source: &Self) {
18421846
self.vec.clone_from(&source.vec);
18431847
}

src/test/codegen/issue-88905.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// min-llvm-version: 10.0.1
3+
// compile-flags: -C opt-level=3
4+
#![crate_type="lib"]
5+
6+
#[no_mangle]
7+
pub fn alloc_test(a: &String) {
8+
// CHECK-LABEL: @alloc_test
9+
// CHECK-NEXT: start:
10+
// CHECK-NEXT: ret void
11+
let x = String::clone(a);
12+
drop(x);
13+
}

0 commit comments

Comments
 (0)