kevinmingtarja commented on code in PR #9973:
URL: https://github.com/apache/arrow-datafusion/pull/9973#discussion_r1554655376


##########
datafusion/functions/src/unicode/substrindex.rs:
##########
@@ -112,18 +112,22 @@ pub fn substr_index<T: OffsetSizeTrait>(args: 
&[ArrayRef]) -> Result<ArrayRef> {
                     return Some(String::new());
                 }
 
-                let splitted: Box<dyn Iterator<Item = _>> = if n > 0 {
-                    Box::new(string.split(delimiter))
+                let occurrences = 
usize::try_from(n.unsigned_abs()).unwrap_or(usize::MAX);
+                let length = if n > 0 {
+                    let splitted = string.split(delimiter);
+                    splitted
+                        .take(occurrences)
+                        .map(|s| s.len() + delimiter.len())
+                        .sum::<usize>()
+                        - delimiter.len()
                 } else {
-                    Box::new(string.rsplit(delimiter))
+                    let splitted = string.rsplit(delimiter);
+                    splitted
+                        .take(occurrences)
+                        .map(|s| s.len() + delimiter.len())
+                        .sum::<usize>()
+                        - delimiter.len()
                 };
-                let occurrences = 
usize::try_from(n.unsigned_abs()).unwrap_or(usize::MAX);
-                // The length of the substring covered by substr_index.
-                let length = splitted
-                    .take(occurrences) // at least 1 element, since n != 0
-                    .map(|s| s.len() + delimiter.len())
-                    .sum::<usize>()
-                    - delimiter.len();
                 if n > 0 {
                     Some(string[..length].to_owned())

Review Comment:
   Awesome, managed to achieve 50% speedup now in total!
   
   ```bash
   substr_index_array_array_1000
                           time:   [57.082 µs 57.407 µs 57.770 µs]
                           change: [-53.118% -52.034% -51.077%] (p = 0.00 < 
0.05)
                           Performance has improved.
   ```
   
   I've just pushed the new commit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to