Mryange opened a new pull request, #62678:
URL: https://github.com/apache/doris/pull/62678

   ## Problem
   
   `FunctionStringConcatWs::_execute_string` (and `_execute_array`) used 
`fmt::join` to
   concatenate strings per row:
   
   ```cpp
   // For every row:
   buffer.clear();
   views.clear();
   for (size_t j = 1; j < argument_size; ++j) {
       if (!current_nullmap[i]) views.emplace_back(ptr, size);
   }
   fmt::format_to(buffer, "{}", fmt::join(views, sep));   // per-row heap write
   StringOP::push_value_string(..., i, res_data, res_offset);  // copy again
   ```
   
   This incurred multiple per-row overhead costs:
   1. **`views` vector rebuild**: `clear()` + `push_back()` on every row, 
causing redundant
      branch and iterator work inside `fmt::join`.
   2. **`fmt::memory_buffer` intermediate copy**: each row's output is first 
materialized into
      a temporary buffer, then copied again into `res_data` via 
`push_value_string`.
   3. **`push_value_string` growth policy**: `res_data` grows incrementally 
(one row at a time),
      causing multiple reallocations over a batch.
   
   ## Solution
   
   Replace with a **two-pass columnar approach**:
   
   - **Pass 1** — scan all rows to accumulate the exact total output byte count 
(string lengths +
     separator contributions, null rows skipped).
   - **Single allocation** — `res_data.resize(total_size)` once via 
`ColumnString::check_chars_length`.
   - **Pass 2** — write directly into `res_data` with `memcpy`, interleaving 
separators inline.
     No intermediate buffer, no `views` vector, no fmt overhead.
   
   The same pattern is applied to both `_execute_string` (fixed-argument list) 
and
   `_execute_array` (array argument).
   
   ## Benchmark
   
   10 000 rows (3-arg case) or 5 000 rows (8-arg and medium-string cases). 
Constant separator `,`.
   Release build on a 192-core x86 server (192 × 3800 MHz).
   
   ```
   Run on (192 X 3800 MHz CPU s)
   CPU Caches:
     L1 Data 48 KiB (x96)
     L1 Instruction 32 KiB (x96)
     L2 Unified 2048 KiB (x96)
     L3 Unified 99840 KiB (x2)
   -----------------------------------------------------------------------
   Benchmark                             Time             CPU   Iterations
   -----------------------------------------------------------------------
   BM_ConcatWs_Old_3Args_Small     1743164 ns      1728609 ns          371
   BM_ConcatWs_New_3Args_Small      296626 ns       294089 ns         2324   
x5.9
   BM_ConcatWs_Old_3Args_Medium    4511487 ns      4480359 ns          160
   BM_ConcatWs_New_3Args_Medium     193811 ns       191969 ns         3691   
x23.3
   BM_ConcatWs_Old_8Args_Small     2299644 ns      2283128 ns          297
   BM_ConcatWs_New_8Args_Small      381030 ns       377581 ns         1880   
x6.0
   BM_ConcatWs_Old_3Args_Null      1469414 ns      1450171 ns          513
   BM_ConcatWs_New_3Args_Null       445277 ns       442122 ns         1467   
x3.3
   ```
   
   - **Small strings** (~20 bytes/arg): **5.9x – 6.0x** faster — fmt overhead 
dominates even for
     tiny payloads.
   - **Medium strings** (~100 bytes/arg): **23.3x** faster — intermediate 
buffer reallocation
     cost amplified with larger data.
   - **With 30% null args**: **3.3x** faster — null skipping reduces effective 
work per row but
     fmt overhead still dominates.
   
   ### 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]

Reply via email to