github-actions[bot] commented on code in PR #62551:
URL: https://github.com/apache/doris/pull/62551#discussion_r3091411239


##########
be/src/exprs/function/function_string_replace.h:
##########
@@ -112,6 +130,68 @@ class FunctionReplace : public IFunction {
     }
 
 private:
+    // Optimized replace path for constant old_str (non-empty) and constant 
new_str.
+    // Avoids per-row std::string allocation by working directly on 
ColumnString
+    // chars/offsets.  Two-level search strategy:
+    //  1. memchr (glibc AVX512) scans for the needle's first byte.  If absent,
+    //     the row is guaranteed no-match and is bulk-copied with a single 
memcpy.
+    //  2. When the first byte is present, ASCIICaseSensitiveStringSearcher
+    //     (SSE4.1, prebuilt once outside the row loop) does the full needle 
scan.
+    static void _replace_const_pattern(const ColumnString& src, StringRef 
old_ref,
+                                       StringRef new_ref, ColumnString& dst,
+                                       size_t input_rows_count, bool 
src_const) {
+        auto& dst_chars = dst.get_chars();
+        auto& dst_offsets = dst.get_offsets();
+
+        dst_chars.reserve(src_const ? (src.get_data_at(0).size * 
input_rows_count)
+                                    : src.get_chars().size());
+        dst_offsets.resize(input_rows_count);
+
+        // Build SSE4.1 searcher once — first+second byte masks precomputed 
here.
+        ASCIICaseSensitiveStringSearcher searcher(old_ref.data, old_ref.size);
+        const size_t needle_size = old_ref.size;
+        const size_t replacement_size = new_ref.size;
+        const char* replacement_data = new_ref.data;
+        const auto needle_first = static_cast<unsigned char>(old_ref.data[0]);
+
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            StringRef row = src.get_data_at(src_const ? 0 : i);
+            const char* const row_end = row.data + row.size;
+
+            // Level-1: memchr for needle's first byte (glibc uses AVX512 
internally).
+            // If the first byte is absent the entire row cannot contain the 
needle;
+            // bulk-copy it and move to the next row without entering the 
SSE4.1 loop.
+            if (memchr(row.data, needle_first, row.size) == nullptr) {
+                StringOP::push_value_string({row.data, row.size}, i, 
dst_chars, dst_offsets);
+                continue;
+            }
+
+            // Level-2: SSE4.1 searcher handles needle matching for this row.
+            const char* pos = row.data;
+            while (pos < row_end) {
+                const char* match = searcher.search(pos, row_end);
+                // Copy prefix before match

Review Comment:
   This fast path bypasses `ColumnString::insert_data()` / 
`StringOP::push_value_string()` and never calls 
`ColumnString::check_chars_length()` before growing `dst_chars`. The old 
implementation would raise `STRING_OVERFLOW_IN_VEC_ENGINE` once the result 
column exceeded 4 GiB, but this path will `resize()` past that and then 
truncate offsets when `push_empty_string()` stores `chars.size()` into the 
32-bit offsets array. A query that expands strings significantly can therefore 
silently corrupt results instead of failing. Please preserve the overflow check 
before each manual `resize()` (or validate the final row growth up front) so 
the fast path matches the existing `ColumnString` contract.



-- 
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]


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

Reply via email to