Skip to content

[opt](function) Optimize lpad/rpad string padding performance#62291

Closed
Mryange wants to merge 1 commit intoapache:masterfrom
Mryange:opt-pad-function
Closed

[opt](function) Optimize lpad/rpad string padding performance#62291
Mryange wants to merge 1 commit intoapache:masterfrom
Mryange:opt-pad-function

Conversation

@Mryange
Copy link
Copy Markdown
Contributor

@Mryange Mryange commented Apr 9, 2026

What problem does this PR solve?

Problem Summary:
lpad/rpad had several avoidable overheads compared to the equivalent ClickHouse implementation:

  1. An intermediate fmt::memory_buffer was used to accumulate results and then copied into the final ColumnString at the end of each block — one redundant full write.
  2. UTF-8 character scanning was always performed, even for pure ASCII input.
  3. No output capacity pre-reservation when the target length is constant.
  4. Short pad strings were memcpy'd one repetition at a time.

Changes

  • Remove fmt::memory_buffer intermediary: both ASCII and UTF-8 paths now write directly into res_chars using reserve + resize_assume_reserved, eliminating the extra copy.
  • Add ASCII fast path: if the input column and pad column are both ASCII (ColumnString::is_ascii()), character count equals byte count — iterate_utf8_with_limit_length and get_char_len are skipped entirely.
  • Pre-reserve output capacity: when len is a constant column, res_chars is reserved upfront (len * rows for ASCII, len * 3 * rows for UTF-8).
  • Pre-expand short pad strings: for pad_const paths, pads shorter than 512 bytes are expanded to ≥ 512 bytes once before the row loop (build_expanded_pad), reducing the number of memcpy calls per row — equivalent to ClickHouse's doubling strategy in PaddingChars::init().
  • Type cleanup: local variables for offset differences use uint32_t/int32_t matching ColumnString::Offsets element type and fast_repeat's parameter type, removing scattered static_cast.

Release note

None

Check List (For Author)

  • Test

    • 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
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

@hello-stephen
Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@Mryange Mryange marked this pull request as draft April 10, 2026 02:07
@Mryange Mryange closed this Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants