Skip to content

Commit 747e655

Browse files
committed
Auto merge of rust-lang#50340 - Emerentius:master, r=alexcrichton
optimize joining for slices This improves the speed of string joining up to 3x. It removes the boolean flag check every iteration, eliminates repeated bounds checks and adds a fast paths for small separators up to a len of 4 bytes These optimizations gave me ~10%, ~50% and ~80% improvements respectively over the previous speed. Those are multiplicative. 3x improvement happens for the optimal case of joining many small strings together in my microbenchmarks. Improvements flatten out for larger strings of course as more time is spent copying bits around. I've run a few benchmarks [with this code](https://github.com/Emerentius/join_bench). They are pretty noise despite high iteration counts, but in total one can see the trends. ``` len_separator len_string n_strings speedup 4 10 10 2.38 4 10 100 3.41 4 10 1000 3.43 4 10 10000 3.25 4 100 10 2.23 4 100 100 2.73 4 100 1000 1.33 4 100 10000 1.14 4 1000 10 1.33 4 1000 100 1.15 4 1000 1000 1.08 4 1000 10000 1.04 10 10 10 1.61 10 10 100 1.74 10 10 1000 1.77 10 10 10000 1.75 10 100 10 1.58 10 100 100 1.65 10 100 1000 1.24 10 100 10000 1.12 10 1000 10 1.23 10 1000 100 1.11 10 1000 1000 1.05 10 1000 10000 0.997 100 10 10 1.66 100 10 100 1.78 100 10 1000 1.28 100 10 10000 1.16 100 100 10 1.37 100 100 100 1.26 100 100 1000 1.09 100 100 10000 1.0 100 1000 10 1.19 100 1000 100 1.12 100 1000 1000 1.05 100 1000 10000 1.12 ``` The string joining with small or empty separators is now ~50% faster than the old concatenation (small strings). The same approach can also improve the performance of joining into vectors. If this approach is acceptable, I can apply it for concatenation and for vectors as well. Alternatively, concat could just call `.join("")`.
2 parents f913231 + 12bd288 commit 747e655

File tree

4 files changed

+122
-42
lines changed

4 files changed

+122
-42
lines changed

src/liballoc/slice.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,17 @@ impl<T: Clone, V: Borrow<[T]>> SliceConcatExt<T> for [V] {
566566
}
567567

568568
fn join(&self, sep: &T) -> Vec<T> {
569+
let mut iter = self.iter();
570+
let first = match iter.next() {
571+
Some(first) => first,
572+
None => return vec![],
573+
};
569574
let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
570575
let mut result = Vec::with_capacity(size + self.len());
571-
let mut first = true;
572-
for v in self {
573-
if first {
574-
first = false
575-
} else {
576-
result.push(sep.clone())
577-
}
576+
result.extend_from_slice(first.borrow());
577+
578+
for v in iter {
579+
result.push(sep.clone());
578580
result.extend_from_slice(v.borrow())
579581
}
580582
result

src/liballoc/str.rs

+91-35
Original file line numberDiff line numberDiff line change
@@ -86,52 +86,108 @@ impl<S: Borrow<str>> SliceConcatExt<str> for [S] {
8686
type Output = String;
8787

8888
fn concat(&self) -> String {
89-
if self.is_empty() {
90-
return String::new();
91-
}
92-
93-
// `len` calculation may overflow but push_str will check boundaries
94-
let len = self.iter().map(|s| s.borrow().len()).sum();
95-
let mut result = String::with_capacity(len);
96-
97-
for s in self {
98-
result.push_str(s.borrow())
99-
}
100-
101-
result
89+
self.join("")
10290
}
10391

10492
fn join(&self, sep: &str) -> String {
105-
if self.is_empty() {
106-
return String::new();
107-
}
108-
109-
// concat is faster
110-
if sep.is_empty() {
111-
return self.concat();
93+
unsafe {
94+
String::from_utf8_unchecked( join_generic_copy(self, sep.as_bytes()) )
11295
}
96+
}
11397

114-
// this is wrong without the guarantee that `self` is non-empty
115-
// `len` calculation may overflow but push_str but will check boundaries
116-
let len = sep.len() * (self.len() - 1) +
117-
self.iter().map(|s| s.borrow().len()).sum::<usize>();
118-
let mut result = String::with_capacity(len);
119-
let mut first = true;
98+
fn connect(&self, sep: &str) -> String {
99+
self.join(sep)
100+
}
101+
}
120102

121-
for s in self {
122-
if first {
123-
first = false;
124-
} else {
125-
result.push_str(sep);
103+
macro_rules! spezialize_for_lengths {
104+
($separator:expr, $target:expr, $iter:expr; $($num:expr),*) => {
105+
let mut target = $target;
106+
let iter = $iter;
107+
let sep_bytes = $separator;
108+
match $separator.len() {
109+
$(
110+
// loops with hardcoded sizes run much faster
111+
// specialize the cases with small separator lengths
112+
$num => {
113+
for s in iter {
114+
copy_slice_and_advance!(target, sep_bytes);
115+
copy_slice_and_advance!(target, s.borrow().as_ref());
116+
}
117+
},
118+
)*
119+
_ => {
120+
// arbitrary non-zero size fallback
121+
for s in iter {
122+
copy_slice_and_advance!(target, sep_bytes);
123+
copy_slice_and_advance!(target, s.borrow().as_ref());
124+
}
126125
}
127-
result.push_str(s.borrow());
128126
}
129-
result
127+
};
128+
}
129+
130+
macro_rules! copy_slice_and_advance {
131+
($target:expr, $bytes:expr) => {
132+
let len = $bytes.len();
133+
let (head, tail) = {$target}.split_at_mut(len);
134+
head.copy_from_slice($bytes);
135+
$target = tail;
130136
}
137+
}
131138

132-
fn connect(&self, sep: &str) -> String {
133-
self.join(sep)
139+
// Optimized join implementation that works for both Vec<T> (T: Copy) and String's inner vec
140+
// Currently (2018-05-13) there is a bug with type inference and specialization (see issue #36262)
141+
// For this reason SliceConcatExt<T> is not specialized for T: Copy and SliceConcatExt<str> is the
142+
// only user of this function. It is left in place for the time when that is fixed.
143+
//
144+
// the bounds for String-join are S: Borrow<str> and for Vec-join Borrow<[T]>
145+
// [T] and str both impl AsRef<[T]> for some T
146+
// => s.borrow().as_ref() and we always have slices
147+
fn join_generic_copy<B, T, S>(slice: &[S], sep: &[T]) -> Vec<T>
148+
where
149+
T: Copy,
150+
B: AsRef<[T]> + ?Sized,
151+
S: Borrow<B>,
152+
{
153+
let sep_len = sep.len();
154+
let mut iter = slice.iter();
155+
156+
// the first slice is the only one without a separator preceding it
157+
let first = match iter.next() {
158+
Some(first) => first,
159+
None => return vec![],
160+
};
161+
162+
// compute the exact total length of the joined Vec
163+
// if the `len` calculation overflows, we'll panic
164+
// we would have run out of memory anyway and the rest of the function requires
165+
// the entire Vec pre-allocated for safety
166+
let len = sep_len.checked_mul(iter.len()).and_then(|n| {
167+
slice.iter()
168+
.map(|s| s.borrow().as_ref().len())
169+
.try_fold(n, usize::checked_add)
170+
}).expect("attempt to join into collection with len > usize::MAX");
171+
172+
// crucial for safety
173+
let mut result = Vec::with_capacity(len);
174+
assert!(result.capacity() >= len);
175+
176+
result.extend_from_slice(first.borrow().as_ref());
177+
178+
unsafe {
179+
{
180+
let pos = result.len();
181+
let target = result.get_unchecked_mut(pos..len);
182+
183+
// copy separator and slices over without bounds checks
184+
// generate loops with hardcoded offsets for small separators
185+
// massive improvements possible (~ x2)
186+
spezialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
187+
}
188+
result.set_len(len);
134189
}
190+
result
135191
}
136192

137193
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/tests/slice.rs

+9
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,15 @@ fn test_join() {
609609
assert_eq!(v.join(&0), [1, 0, 2, 0, 3]);
610610
}
611611

612+
#[test]
613+
fn test_join_nocopy() {
614+
let v: [String; 0] = [];
615+
assert_eq!(v.join(","), "");
616+
assert_eq!(["a".to_string(), "ab".into()].join(","), "a,ab");
617+
assert_eq!(["a".to_string(), "ab".into(), "abc".into()].join(","), "a,ab,abc");
618+
assert_eq!(["a".to_string(), "ab".into(), "".into()].join(","), "a,ab,");
619+
}
620+
612621
#[test]
613622
fn test_insert() {
614623
let mut a = vec![1, 2, 4];

src/liballoc/tests/str.rs

+13
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ fn test_join_for_different_lengths() {
162162
test_join!("-a-bc", ["", "a", "bc"], "-");
163163
}
164164

165+
// join has fast paths for small separators up to 4 bytes
166+
// this tests the slow paths.
167+
#[test]
168+
fn test_join_for_different_lengths_with_long_separator() {
169+
assert_eq!("~~~~~".len(), 15);
170+
171+
let empty: &[&str] = &[];
172+
test_join!("", empty, "~~~~~");
173+
test_join!("a", ["a"], "~~~~~");
174+
test_join!("a~~~~~b", ["a", "b"], "~~~~~");
175+
test_join!("~~~~~a~~~~~bc", ["", "a", "bc"], "~~~~~");
176+
}
177+
165178
#[test]
166179
fn test_unsafe_slice() {
167180
assert_eq!("ab", unsafe {"abc".slice_unchecked(0, 2)});

0 commit comments

Comments
 (0)