Skip to content

Commit 195d3d6

Browse files
authored
perf: optimize strpos by eliminating double iteration for UTF-8 (#19572)
## 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. --> - Closes #. ## 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. --> ## 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. --> For non-ASCII strings, the original implementation used string.find() to get the byte index, then counted characters up to that byte index. This required two passes through the string. This optimization uses char_indices() to find the substring while simultaneously tracking character positions, completing the search in a single pass. Benchmark results (UTF-8 strings): - str_len_8: 188.98 µs → 140.54 µs (25.4% faster) - str_len_32: 615.69 µs → 294.15 µs (52.2% faster) - str_len_128: 2.2707 ms → 1.2462 ms (45.1% faster) - str_len_4096: 74.328 ms → 36.538 ms (50.9% faster) ASCII performance unchanged (already optimized with fast path). ## 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 bc753c2 commit 195d3d6

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

datafusion/functions/src/unicode/strpos.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,37 @@ where
215215
)
216216
}
217217
} else {
218-
// The `find` method returns the byte index of the substring.
219-
// We count the number of chars up to that byte index.
220-
T::Native::from_usize(
221-
string
222-
.find(substring)
223-
.map(|x| string[..x].chars().count() + 1)
224-
.unwrap_or(0),
225-
)
218+
// For non-ASCII, use a single-pass search that tracks both
219+
// byte position and character position simultaneously
220+
if substring.is_empty() {
221+
return T::Native::from_usize(1);
222+
}
223+
224+
let substring_bytes = substring.as_bytes();
225+
let string_bytes = string.as_bytes();
226+
227+
if substring_bytes.len() > string_bytes.len() {
228+
return T::Native::from_usize(0);
229+
}
230+
231+
// Single pass: find substring while counting characters
232+
let mut char_pos = 0;
233+
for (byte_idx, _) in string.char_indices() {
234+
char_pos += 1;
235+
if byte_idx + substring_bytes.len() <= string_bytes.len() {
236+
// SAFETY: We just checked that byte_idx + substring_bytes.len() <= string_bytes.len()
237+
let slice = unsafe {
238+
string_bytes.get_unchecked(
239+
byte_idx..byte_idx + substring_bytes.len(),
240+
)
241+
};
242+
if slice == substring_bytes {
243+
return T::Native::from_usize(char_pos);
244+
}
245+
}
246+
}
247+
248+
T::Native::from_usize(0)
226249
}
227250
}
228251
_ => None,

0 commit comments

Comments
 (0)