Mryange opened a new pull request, #62551:
URL: https://github.com/apache/doris/pull/62551
### What problem does this PR solve?
The previous `replace(col, 'literal', 'literal')` implementation processed
each row independently:
1. `to_string()` — copy row data into a temporary `std::string`
2. `std::string::find` + `std::string::replace` — in-place realloc per match
3. `insert_data()` — copy result back into ColumnString
This caused three heap allocations per row and poor cache utilization.
### What was done
Added `_replace_const_pattern()` as a fast path that fires when both
`old_str` and `new_str` are constants and `old_str` is non-empty (the common
case in SQL queries). The fast path:
- Works directly on `ColumnString` chars/offsets — **zero per-row
`std::string` allocation**
- Uses a **two-level search strategy**:
- Level-1: `memchr` (glibc AVX512) scans for the needle's first byte; if
absent, the entire row is bulk-copied with a single `memcpy` and the SSE4.1
loop is skipped entirely
- Level-2: `ASCIICaseSensitiveStringSearcher` (SSE4.1, prebuilt once
outside the row loop) handles full needle matching for rows that pass the
pre-filter
- Uses `StringOP::push_value_string` / `push_empty_string` for consistent
offset management
- Applies to both `replace` and `replace_empty` — when `old_str` is
non-empty these two functions behave identically
### Benchmark results (Release build, 192-core machine)
| Case | Old | New | Speedup |
|---|---|---|---|
| SmallStr (10k rows, 50B avg, hits) | 1,717,467 ns | 1,278,740 ns |
**1.34x** |
| MedStr (5k rows, 200B avg, hits) | 2,099,738 ns | 1,247,593 ns | **1.68x**
|
| LargeStr (1k rows, 1000B avg, hits) | 1,230,671 ns | 586,966 ns |
**2.10x** |
| NoMatch (10k rows, no needle present) | 378,851 ns | 217,587 ns |
**1.74x** |
### Release note
None
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [ ] Regression test
- [ ] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [ ] Yes. <!-- Explain the behavior change -->
- Does this need documentation?
- [ ] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR should
merge into -->
--
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]