Skip to content

Commit 5b90cee

Browse files
authored
perf: improve performance of string repeat (#19502)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> N/A ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Use a re-usable string buffer instead of allocating a new string for each input value. | Benchmark | Main (µs) | Optimized (µs) | Improvement | |----------------------|-----------|----------------|-----------------| | size=1024, repeat=3 | | | | | repeat_string_view | 76.51 | 70.14 | -8.3% | | repeat_string | 78.63 | 71.41 | -9.2% | | repeat_large_string | 76.40 | 71.08 | -7.0% | | size=1024, repeat=30 | | | | | repeat_string_view | 109.02 | 93.51 | -14.2% | | repeat_string | 108.46 | 92.12 | -15.1% | | repeat_large_string | 105.99 | 91.66 | -13.5% | | size=4096, repeat=3 | | | | | repeat_string_view | 139.44 | 113.95 | -18.3% | | repeat_string | 133.62 | 112.25 | -16.0% | | repeat_large_string | 131.94 | 108.41 | -17.8% | | size=4096, repeat=30 | | | | | repeat_string_view | 251.77 | 193.95 | -23.0% | | repeat_string | 250.58 | 191.86 | -23.4% | | repeat_large_string | 248.88 | 188.43 | -24.3% | | overflow tests | | | | | size=1024 | 58.14 | 58.02 | ~0% (no change) | | size=4096 | 58.26 | 58.08 | ~0% (no change) | ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent d20c5d6 commit 5b90cee

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

datafusion/functions/src/string/repeat.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ where
153153
S: StringArrayType<'a>,
154154
{
155155
let mut total_capacity = 0;
156+
let mut max_item_capacity = 0;
156157
string_array.iter().zip(number_array.iter()).try_for_each(
157158
|(string, number)| -> Result<(), DataFusionError> {
158159
match (string, number) {
@@ -166,6 +167,7 @@ where
166167
);
167168
}
168169
total_capacity += item_capacity;
170+
max_item_capacity = max_item_capacity.max(item_capacity);
169171
}
170172
_ => (),
171173
}
@@ -176,18 +178,37 @@ where
176178
let mut builder =
177179
GenericStringBuilder::<T>::with_capacity(string_array.len(), total_capacity);
178180

179-
string_array.iter().zip(number_array.iter()).try_for_each(
180-
|(string, number)| -> Result<(), DataFusionError> {
181+
// Reusable buffer to avoid allocations in string.repeat()
182+
let mut buffer = Vec::<u8>::with_capacity(max_item_capacity);
183+
184+
string_array
185+
.iter()
186+
.zip(number_array.iter())
187+
.for_each(|(string, number)| {
181188
match (string, number) {
182189
(Some(string), Some(number)) if number >= 0 => {
183-
builder.append_value(string.repeat(number as usize));
190+
buffer.clear();
191+
let count = number as usize;
192+
if count > 0 && !string.is_empty() {
193+
let src = string.as_bytes();
194+
// Initial copy
195+
buffer.extend_from_slice(src);
196+
// Doubling strategy: copy what we have so far until we reach the target
197+
while buffer.len() < src.len() * count {
198+
let copy_len =
199+
buffer.len().min(src.len() * count - buffer.len());
200+
// SAFETY: we're copying valid UTF-8 bytes that we already verified
201+
buffer.extend_from_within(..copy_len);
202+
}
203+
}
204+
// SAFETY: buffer contains valid UTF-8 since we only ever copy from a valid &str
205+
builder
206+
.append_value(unsafe { std::str::from_utf8_unchecked(&buffer) });
184207
}
185208
(Some(_), Some(_)) => builder.append_value(""),
186209
_ => builder.append_null(),
187210
}
188-
Ok(())
189-
},
190-
)?;
211+
});
191212
let array = builder.finish();
192213

193214
Ok(Arc::new(array) as ArrayRef)

0 commit comments

Comments
 (0)