Thanks to visit codestin.com
Credit goes to github.com

Skip to content

perf: Optimize lpad, rpad for scalar args#20657

Open
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/optimize-pad-scalars
Open

perf: Optimize lpad, rpad for scalar args#20657
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/optimize-pad-scalars

Conversation

@neilconway
Copy link
Contributor

@neilconway neilconway commented Mar 2, 2026

Which issue does this PR close?

Rationale for this change

lpad and rpad are commonly called with constant (scalar) target length and fill arguments, e.g. lpad(column, 20, '0'). We can special-case this scenario to improve performance by avoiding the overhead of make_scalar_function, and also by precomputing the padding buffer and reusing it for each row.

For scalar args, this improves performance by ~65% for ASCII inputs and ~41% for Unicode inputs.

What changes are included in this PR?

  • Add benchmarks for padding with scalar length and fill.
  • Add a scalar fast path for lpad and rpad that precomputes a padding buffer. We only use the fast path if the requested pad length is reasonably small (<= 8KB), to avoid using too much memory on a scratch buffer.
  • Code cleanup: extract and use try_as_scalar_str and try_as_scalar_i64 helpers.
  • Code cleanup: use target_len consistently instead of length, because the latter is ambiguous.
  • Code cleanup: make rpad and lpad more similar by removing needless and probably unintended differences between the two functions. We could go further and refactor them to remove the redundancy but I won't attempt that for now.

Are these changes tested?

Yes; covered by existing tests. Added new benchmarks.

Are there any user-facing changes?

No.

AI usage

Multiple AI tools were used to iterate on this PR. I have reviewed and understand the resulting code.

@github-actions github-actions bot added the functions Changes to functions implementation label Mar 2, 2026
@neilconway
Copy link
Contributor Author

Benchmarks:

⏺ ┌───────────────────────────────────────────┬──────────┬───────────┬────────┐
  │                 Benchmark                 │   Main   │ Optimized │ Change │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ lpad utf8 scalar (1024, fill='x')         │ 27.5 µs  │ 8.3 µs    │ -70%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ lpad stringview scalar (1024, fill='x')   │ 27.6 µs  │ 9.7 µs    │ -65%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ lpad utf8 scalar unicode (1024, fill='é') │ 74.3 µs  │ 44.1 µs   │ -41%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ lpad utf8 scalar (4096, fill='x')         │ 109.0 µs │ 33.1 µs   │ -70%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ lpad stringview scalar (4096, fill='x')   │ 109.5 µs │ 39.6 µs   │ -64%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ lpad utf8 scalar unicode (4096, fill='é') │ 296.6 µs │ 173.2 µs  │ -42%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ rpad utf8 scalar (1024, fill='x')         │ 30.5 µs  │ 10.6 µs   │ -65%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ rpad stringview scalar (1024, fill='x')   │ 29.0 µs  │ 11.1 µs   │ -62%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ rpad utf8 scalar unicode (1024, fill='é') │ 78.9 µs  │ 46.3 µs   │ -41%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ rpad utf8 scalar (4096, fill='x')         │ 124.2 µs │ 43.6 µs   │ -65%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ rpad stringview scalar (4096, fill='x')   │ 114.8 µs │ 44.6 µs   │ -61%   │
  ├───────────────────────────────────────────┼──────────┼───────────┼────────┤
  │ rpad utf8 scalar unicode (4096, fill='é') │ 313.5 µs │ 182.7 µs  │ -42%   │
  └───────────────────────────────────────────┴──────────┴───────────┴────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize lpad, rpad for scalar args

1 participant