Skip to content

Commit aff626a

Browse files
authored
Rollup merge of rust-lang#48657 - sinkuu:opt_str_repeat, r=dtolnay
Optimize str::repeat Improves the performance of `str::repeat` by bulk copying. Here is the benchmarks of `"abcde".repeat(n)`: |`n`|old [ns/iter]|new [ns/iter]|diff [%]| ---|---|---|--- |1|27.205|27.421|+0.794| |2|27.500|27.516|+0.0581| |3|27.923|27.648|-0.985| |4|31.206|30.145|-3.40| |5|35.144|31.861|-9.34| |7|43.131|34.621|-19.7| |10|54.945|36.203|-34.1| |100|428.31|52.895|-87.7|
2 parents 4fcc87a + 3d58543 commit aff626a

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#![feature(allocator_internals)]
125125
#![feature(on_unimplemented)]
126126
#![feature(exact_chunks)]
127+
#![feature(pointer_methods)]
127128

128129
#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, swap_with_slice, i128))]
129130
#![cfg_attr(test, feature(test, box_heap))]

src/liballoc/str.rs

+54-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use core::str as core_str;
4343
use core::str::pattern::Pattern;
4444
use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
4545
use core::mem;
46+
use core::ptr;
4647
use core::iter::FusedIterator;
4748
use std_unicode::str::{UnicodeStr, Utf16Encoder};
4849

@@ -2066,9 +2067,59 @@ impl str {
20662067
/// ```
20672068
#[stable(feature = "repeat_str", since = "1.16.0")]
20682069
pub fn repeat(&self, n: usize) -> String {
2069-
let mut s = String::with_capacity(self.len() * n);
2070-
s.extend((0..n).map(|_| self));
2071-
s
2070+
if n == 0 {
2071+
return String::new();
2072+
}
2073+
2074+
// If `n` is larger than zero, it can be split as
2075+
// `n = 2^expn + rem (2^expn > rem, expn >= 0, rem >= 0)`.
2076+
// `2^expn` is the number represented by the leftmost '1' bit of `n`,
2077+
// and `rem` is the remaining part of `n`.
2078+
2079+
// Using `Vec` to access `set_len()`.
2080+
let mut buf = Vec::with_capacity(self.len() * n);
2081+
2082+
// `2^expn` repetition is done by doubling `buf` `expn`-times.
2083+
buf.extend(self.as_bytes());
2084+
{
2085+
let mut m = n >> 1;
2086+
// If `m > 0`, there are remaining bits up to the leftmost '1'.
2087+
while m > 0 {
2088+
// `buf.extend(buf)`:
2089+
unsafe {
2090+
ptr::copy_nonoverlapping(
2091+
buf.as_ptr(),
2092+
(buf.as_mut_ptr() as *mut u8).add(buf.len()),
2093+
buf.len(),
2094+
);
2095+
// `buf` has capacity of `self.len() * n`.
2096+
let buf_len = buf.len();
2097+
buf.set_len(buf_len * 2);
2098+
}
2099+
2100+
m >>= 1;
2101+
}
2102+
}
2103+
2104+
// `rem` (`= n - 2^expn`) repetition is done by copying
2105+
// first `rem` repetitions from `buf` itself.
2106+
let rem_len = self.len() * n - buf.len(); // `self.len() * rem`
2107+
if rem_len > 0 {
2108+
// `buf.extend(buf[0 .. rem_len])`:
2109+
unsafe {
2110+
// This is non-overlapping since `2^expn > rem`.
2111+
ptr::copy_nonoverlapping(
2112+
buf.as_ptr(),
2113+
(buf.as_mut_ptr() as *mut u8).add(buf.len()),
2114+
rem_len,
2115+
);
2116+
// `buf.len() + rem_len` equals to `buf.capacity()` (`= self.len() * n`).
2117+
let buf_cap = buf.capacity();
2118+
buf.set_len(buf_cap);
2119+
}
2120+
}
2121+
2122+
unsafe { String::from_utf8_unchecked(buf) }
20722123
}
20732124

20742125
/// Checks if all characters in this string are within the ASCII range.

0 commit comments

Comments
 (0)