This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 5b90ceef1c perf: improve performance of string repeat (#19502)
5b90ceef1c is described below

commit 5b90ceef1c9676c71238cf9d460778e41d24d930
Author: Andy Grove <[email protected]>
AuthorDate: Sun Dec 28 09:05:30 2025 -0700

    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.
    -->
---
 datafusion/functions/src/string/repeat.rs | 33 +++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/datafusion/functions/src/string/repeat.rs 
b/datafusion/functions/src/string/repeat.rs
index 0656a32c24..2ca5e190c6 100644
--- a/datafusion/functions/src/string/repeat.rs
+++ b/datafusion/functions/src/string/repeat.rs
@@ -153,6 +153,7 @@ where
     S: StringArrayType<'a>,
 {
     let mut total_capacity = 0;
+    let mut max_item_capacity = 0;
     string_array.iter().zip(number_array.iter()).try_for_each(
         |(string, number)| -> Result<(), DataFusionError> {
             match (string, number) {
@@ -166,6 +167,7 @@ where
                         );
                     }
                     total_capacity += item_capacity;
+                    max_item_capacity = max_item_capacity.max(item_capacity);
                 }
                 _ => (),
             }
@@ -176,18 +178,37 @@ where
     let mut builder =
         GenericStringBuilder::<T>::with_capacity(string_array.len(), 
total_capacity);
 
-    string_array.iter().zip(number_array.iter()).try_for_each(
-        |(string, number)| -> Result<(), DataFusionError> {
+    // Reusable buffer to avoid allocations in string.repeat()
+    let mut buffer = Vec::<u8>::with_capacity(max_item_capacity);
+
+    string_array
+        .iter()
+        .zip(number_array.iter())
+        .for_each(|(string, number)| {
             match (string, number) {
                 (Some(string), Some(number)) if number >= 0 => {
-                    builder.append_value(string.repeat(number as usize));
+                    buffer.clear();
+                    let count = number as usize;
+                    if count > 0 && !string.is_empty() {
+                        let src = string.as_bytes();
+                        // Initial copy
+                        buffer.extend_from_slice(src);
+                        // Doubling strategy: copy what we have so far until 
we reach the target
+                        while buffer.len() < src.len() * count {
+                            let copy_len =
+                                buffer.len().min(src.len() * count - 
buffer.len());
+                            // SAFETY: we're copying valid UTF-8 bytes that we 
already verified
+                            buffer.extend_from_within(..copy_len);
+                        }
+                    }
+                    // SAFETY: buffer contains valid UTF-8 since we only ever 
copy from a valid &str
+                    builder
+                        .append_value(unsafe { 
std::str::from_utf8_unchecked(&buffer) });
                 }
                 (Some(_), Some(_)) => builder.append_value(""),
                 _ => builder.append_null(),
             }
-            Ok(())
-        },
-    )?;
+        });
     let array = builder.finish();
 
     Ok(Arc::new(array) as ArrayRef)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to