You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "neilconway (via GitHub)" <gi...@apache.org> on 2026/04/04 17:42:37 UTC

[PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

neilconway opened a new pull request, #21366:
URL: https://github.com/apache/datafusion/pull/21366

   ## Which issue does this PR close?
   
   - Closes #21364.
   
   ## Rationale for this change
   
   For `Utf8` and `LargeUtf8` inputs, we can optimize `substr` to avoid copying the output strings; instead, we can return a `StringViewArray` that points into the input value buffer.
   
   Benchmarks (M4 Max):
   
   Array args, no count, short strings (strlen=12):
     - string_view: 25.8 → 25.2 (-2.5%)
     - string: 32.9 → 23.8 (-27.9%)
     - large_string: 35.0 → 24.0 (-31.4%)
   
   Array args, with count, long strings (count=64, strlen=128):
     - string_view: 41.5 → 41.9 (+1.0%)
     - string: 47.4 → 37.0 (-22.0%)
     - large_string: 47.6 → 37.5 (-21.2%)
   
   Array args, short count, long strings (count=6, strlen=128):
     - string_view: 48.8 → 48.7 (-0.2%)
     - string: 59.5 → 49.1 (-17.6%)
     - large_string: 65.1 → 49.2 (-24.4%)
   
   Scalar start, no count, short strings (strlen=12):
     - string_view: 26.3 → 25.9 (-1.3%)
     - string: 32.8 → 24.3 (-25.9%)
   
   Scalar start, no count, long strings (strlen=128):
     - string_view: 41.4 → 41.7 (+0.7%)
     - string: 47.2 → 37.2 (-21.2%)
   
   Scalar start=1, no count, long strings (strlen=128):
     - string_view: 42.0 → 41.9 (-0.2%)
     - string: 47.7 → 37.3 (-21.8%)
   
   Scalar args, short strings (count=6, strlen=12):
     - string_view: 47.3 → 47.8 (+1.1%)
     - string: 61.1 → 50.4 (-17.5%)
   
   Scalar args, long strings (count=64, strlen=128):
     - string_view: 42.4 → 42.6 (+0.6%)
     - string: 47.7 → 37.8 (-20.8%)
     - large_string: 51.5 → 38.4 (-25.5%)
   
   ## What changes are included in this PR?
   
   * Implement optimization
   * Other minor code cleanup
   * Add a benchmark (only somewhat related to this optimization but related to future optimization work)
   
   ## Are these changes tested?
   
   Yes.
   
   ## Are there any user-facing changes?
   
   No.
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "coderfender (via GitHub)" <gi...@apache.org>.
coderfender commented on code in PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#discussion_r3042874021


##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -296,16 +296,15 @@ fn string_view_substr(
         let count = count_array_opt.map(|a| a.value(i));
         let raw_view = string_view_array.views()[i];
 
-        let (start, end) =
-            get_true_start_end(string, start, count, enable_ascii_fast_path)?;
-        let substr = &string[start..end];
+        let (byte_start, byte_end) = get_true_start_end(string, start, count, is_ascii)?;
+        let substr = &string[byte_start..byte_end];
 
         make_and_append_view(
             &mut views_buf,
             &mut null_builder,
             &raw_view,
             substr,
-            start as u32,
+            byte_start as u32,

Review Comment:
   Could `byte_start` be < u32 ?



##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -326,17 +325,111 @@ fn string_view_substr(
     }
 }
 
-fn string_substr<'a, V>(string_array: V, args: &[ArrayRef]) -> Result<ArrayRef>
-where
-    V: StringArrayType<'a> + Copy,
-{
+fn values_fit_in_u32<T: OffsetSizeTrait>(string_array: &GenericStringArray<T>) -> bool {
+    string_array
+        .offsets()
+        .last()
+        .map(|offset| offset.as_usize() <= u32::MAX as usize)
+        .unwrap_or(true)
+}
+
+#[inline]
+fn append_new_view(
+    views_buf: &mut Vec<u128>,
+    null_builder: &mut NullBufferBuilder,
+    substr: &str,
+    byte_offset: usize,
+) -> bool {
+    let is_out_of_line = substr.len() > 12;
+    let view = if is_out_of_line {
+        let byte_offset = u32::try_from(byte_offset)
+            .expect("validated string buffer offset fits in u32");
+        make_view(substr.as_bytes(), 0, byte_offset)
+    } else {
+        make_view(substr.as_bytes(), 0, 0)
+    };
+
+    views_buf.push(view);
+    null_builder.append_non_null();
+    is_out_of_line
+}
+
+#[expect(clippy::needless_range_loop)]
+fn generic_string_substr<T: OffsetSizeTrait>(

Review Comment:
   I am assuming you have benchmarked this with `inline`ed ? 



##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -326,17 +325,111 @@ fn string_view_substr(
     }
 }
 
-fn string_substr<'a, V>(string_array: V, args: &[ArrayRef]) -> Result<ArrayRef>
-where
-    V: StringArrayType<'a> + Copy,
-{
+fn values_fit_in_u32<T: OffsetSizeTrait>(string_array: &GenericStringArray<T>) -> bool {
+    string_array
+        .offsets()
+        .last()
+        .map(|offset| offset.as_usize() <= u32::MAX as usize)
+        .unwrap_or(true)
+}
+
+#[inline]
+fn append_new_view(
+    views_buf: &mut Vec<u128>,
+    null_builder: &mut NullBufferBuilder,
+    substr: &str,
+    byte_offset: usize,
+) -> bool {
+    let is_out_of_line = substr.len() > 12;
+    let view = if is_out_of_line {
+        let byte_offset = u32::try_from(byte_offset)
+            .expect("validated string buffer offset fits in u32");
+        make_view(substr.as_bytes(), 0, byte_offset)
+    } else {
+        make_view(substr.as_bytes(), 0, 0)
+    };
+
+    views_buf.push(view);
+    null_builder.append_non_null();
+    is_out_of_line
+}
+
+#[expect(clippy::needless_range_loop)]
+fn generic_string_substr<T: OffsetSizeTrait>(
+    string_array: &GenericStringArray<T>,
+    args: &[ArrayRef],
+) -> Result<ArrayRef> {
+    // We return a StringViewArray that points into the input string array's
+    // values buffer, avoiding copies. This is only possible when the values
+    // buffer is <= 4GB, since StringView offsets are u32.
+    if !values_fit_in_u32(string_array) {

Review Comment:
    Could we add fast paths / early returns for:
     1. start = 1 with no count is a no-op, return input directly
     2. start = 1 with count >= len
     3. start > len(string)
     4. start + count results in empty range



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on code in PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#discussion_r3094452314


##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -319,15 +318,107 @@ fn string_view_substr(
     }
 }
 
-fn string_substr<'a, V>(string_array: V, args: &[ArrayRef]) -> Result<ArrayRef>
-where
-    V: StringArrayType<'a> + Copy,
-{
+fn values_fit_in_u32<T: OffsetSizeTrait>(string_array: &GenericStringArray<T>) -> bool {
+    string_array
+        .offsets()
+        .last()
+        .map(|offset| offset.as_usize() <= u32::MAX as usize)
+        .unwrap_or(true)
+}
+
+#[inline]
+fn append_view_from_buffer(
+    views_buf: &mut Vec<u128>,
+    substr: &str,
+    byte_offset: usize,
+) -> bool {
+    let is_out_of_line = substr.len() > 12;
+    let view = if is_out_of_line {
+        let byte_offset = u32::try_from(byte_offset)
+            .expect("validated string buffer offset fits in u32");
+        make_view(substr.as_bytes(), 0, byte_offset)
+    } else {
+        make_view(substr.as_bytes(), 0, 0)

Review Comment:
   No; I think you can debate whether a branch is more readable or not, but I'm happy to omit it.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#issuecomment-4266054401

   Thanks @neilconway & @coderfender 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on code in PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#discussion_r3046053034


##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -296,16 +296,15 @@ fn string_view_substr(
         let count = count_array_opt.map(|a| a.value(i));
         let raw_view = string_view_array.views()[i];
 
-        let (start, end) =
-            get_true_start_end(string, start, count, enable_ascii_fast_path)?;
-        let substr = &string[start..end];
+        let (byte_start, byte_end) = get_true_start_end(string, start, count, is_ascii)?;
+        let substr = &string[byte_start..byte_end];
 
         make_and_append_view(
             &mut views_buf,
             &mut null_builder,
             &raw_view,
             substr,
-            start as u32,
+            byte_start as u32,

Review Comment:
   This is pre-existing code, but it looks safe to me: `byte_start` is bounded by the length of a string in a `StringViewArray`, which must fit in a u32.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#issuecomment-4207154707

   @coderfender Thanks for the review! Please let me know if you have any more feedback.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#discussion_r3091329333


##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -319,15 +318,107 @@ fn string_view_substr(
     }
 }
 
-fn string_substr<'a, V>(string_array: V, args: &[ArrayRef]) -> Result<ArrayRef>
-where
-    V: StringArrayType<'a> + Copy,
-{
+fn values_fit_in_u32<T: OffsetSizeTrait>(string_array: &GenericStringArray<T>) -> bool {
+    string_array
+        .offsets()
+        .last()
+        .map(|offset| offset.as_usize() <= u32::MAX as usize)
+        .unwrap_or(true)
+}
+
+#[inline]
+fn append_view_from_buffer(
+    views_buf: &mut Vec<u128>,
+    substr: &str,
+    byte_offset: usize,
+) -> bool {
+    let is_out_of_line = substr.len() > 12;
+    let view = if is_out_of_line {
+        let byte_offset = u32::try_from(byte_offset)
+            .expect("validated string buffer offset fits in u32");
+        make_view(substr.as_bytes(), 0, byte_offset)
+    } else {
+        make_view(substr.as_bytes(), 0, 0)

Review Comment:
   Do we strictly need this branching?



##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -319,15 +318,107 @@ fn string_view_substr(
     }
 }
 
-fn string_substr<'a, V>(string_array: V, args: &[ArrayRef]) -> Result<ArrayRef>
-where
-    V: StringArrayType<'a> + Copy,
-{
+fn values_fit_in_u32<T: OffsetSizeTrait>(string_array: &GenericStringArray<T>) -> bool {
+    string_array
+        .offsets()
+        .last()
+        .map(|offset| offset.as_usize() <= u32::MAX as usize)

Review Comment:
   Same comment as on left/right PR, where we should check `i32::MAX`



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on code in PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#discussion_r3046323361


##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -326,17 +325,111 @@ fn string_view_substr(
     }
 }
 
-fn string_substr<'a, V>(string_array: V, args: &[ArrayRef]) -> Result<ArrayRef>
-where
-    V: StringArrayType<'a> + Copy,
-{
+fn values_fit_in_u32<T: OffsetSizeTrait>(string_array: &GenericStringArray<T>) -> bool {
+    string_array
+        .offsets()
+        .last()
+        .map(|offset| offset.as_usize() <= u32::MAX as usize)
+        .unwrap_or(true)
+}
+
+#[inline]
+fn append_new_view(
+    views_buf: &mut Vec<u128>,
+    null_builder: &mut NullBufferBuilder,
+    substr: &str,
+    byte_offset: usize,
+) -> bool {
+    let is_out_of_line = substr.len() > 12;
+    let view = if is_out_of_line {
+        let byte_offset = u32::try_from(byte_offset)
+            .expect("validated string buffer offset fits in u32");
+        make_view(substr.as_bytes(), 0, byte_offset)
+    } else {
+        make_view(substr.as_bytes(), 0, 0)
+    };
+
+    views_buf.push(view);
+    null_builder.append_non_null();
+    is_out_of_line
+}
+
+#[expect(clippy::needless_range_loop)]
+fn generic_string_substr<T: OffsetSizeTrait>(
+    string_array: &GenericStringArray<T>,
+    args: &[ArrayRef],
+) -> Result<ArrayRef> {
+    // We return a StringViewArray that points into the input string array's
+    // values buffer, avoiding copies. This is only possible when the values
+    // buffer is <= 4GB, since StringView offsets are u32.
+    if !values_fit_in_u32(string_array) {

Review Comment:
   I think (1) would probably be useful, although we could implement it via `simplify` so there's no runtime overhead at all.
   
   For the other cases, they seem a bit marginal to me, personally -- the existing code should handle them pretty efficiently. In any case, can we defer this to a different PR?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#issuecomment-4261379048

   @Jefffrey Thanks for the review! I addressed your comments.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #21366:
URL: https://github.com/apache/datafusion/pull/21366


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "coderfender (via GitHub)" <gi...@apache.org>.
coderfender commented on code in PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#discussion_r3042859989


##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -275,7 +275,7 @@ fn string_view_substr(
     let start_array = as_int64_array(&args[0])?;
     let count_array_opt = args.get(1).map(|a| as_int64_array(a)).transpose()?;
 
-    let enable_ascii_fast_path =
+    let is_ascii =
         enable_ascii_fast_path(&string_view_array, start_array, count_array_opt);

Review Comment:
   this is nice improvement to optimize ascii usecases 



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `substr` for Utf8, LargeUtf8 [datafusion]

Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on code in PR #21366:
URL: https://github.com/apache/datafusion/pull/21366#discussion_r3046188974


##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -326,17 +325,111 @@ fn string_view_substr(
     }
 }
 
-fn string_substr<'a, V>(string_array: V, args: &[ArrayRef]) -> Result<ArrayRef>
-where
-    V: StringArrayType<'a> + Copy,
-{
+fn values_fit_in_u32<T: OffsetSizeTrait>(string_array: &GenericStringArray<T>) -> bool {
+    string_array
+        .offsets()
+        .last()
+        .map(|offset| offset.as_usize() <= u32::MAX as usize)
+        .unwrap_or(true)
+}
+
+#[inline]
+fn append_new_view(
+    views_buf: &mut Vec<u128>,
+    null_builder: &mut NullBufferBuilder,
+    substr: &str,
+    byte_offset: usize,
+) -> bool {
+    let is_out_of_line = substr.len() > 12;
+    let view = if is_out_of_line {
+        let byte_offset = u32::try_from(byte_offset)
+            .expect("validated string buffer offset fits in u32");
+        make_view(substr.as_bytes(), 0, byte_offset)
+    } else {
+        make_view(substr.as_bytes(), 0, 0)
+    };
+
+    views_buf.push(view);
+    null_builder.append_non_null();
+    is_out_of_line
+}
+
+#[expect(clippy::needless_range_loop)]
+fn generic_string_substr<T: OffsetSizeTrait>(

Review Comment:
   I ran a quick benchmark adding `#[inline]` and it didn't have a measurable impact.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org