Repository navigation
Commit 29580fa
authored
refactor: share a single pad kernel between lpad and rpad (apache#26367)
## Which issue does this PR close?
- Closes apache#26303.
## Rationale for this change
lpad and rpad had four copies of the same per-row padding loop, plus two
copies of the scalar fast path. The only real difference between them
was whether the fill gets written before or after the string.
## What changes are included in this PR?
- New `unicode/pad.rs` with a single kernel, generic over a `PadSide`
trait (`LeftPad` / `RightPad`, same idea as `LeftRightSlicer` in
`common.rs`). `lpad.rs` and `rpad.rs` are now thin wrappers with just
the docs and tests.
- The no-fill case goes through the same loop with `" "` as the fill, so
the second loop is gone.
- `parse_target_len` replaces the six copies of the `i32::MAX` /
negative length check. Error text is unchanged.
Two things came out of benchmarking:
- Once the shared length check had its error branch inline, it stopped
getting inlined into the hot loop and the no-fill path got noticeably
slower. Moving the error into a `#[cold]` function fixed that.
- The non-ASCII path now writes the fill in whole copies instead of one
char at a time with a `%` per char. That's where the speedup below comes
from.
I also added `no fill` cases to the pad benchmarks (first commit). None
of the existing cases covered a 2-arg call with a length column, which
is the path this changes the most.
## What is the testing strategy for this PR?
Existing lpad/rpad unit tests and sqllogictests pass unchanged. I added
tests in `pad.rs` that pass array arguments to both functions, since
most of the existing ones go through the scalar fast path: no fill,
fill, nulls, zero length, truncation, string view input, a partial
multi-char unicode fill (`rpad('josé', 6, 'ñéx')`), and the too-large
length error.
## Benchmarks
`cargo bench -p datafusion-functions --features unicode_expressions
--bench unicode_expressions -- "pad size="`, main vs this branch, run
back to back on the same machine:
- non-ASCII string with a fill (8 cases): 31–48% faster
- everything else (40 cases): somewhere between −16% and +14% in both
directions, which is about the noise on my machine. The scalar cases,
whose logic didn't change, moved by the same amount.
Happy to re-run anything if someone wants different numbers.
## Are there any user-facing changes?
No.
---
Separately, I found that a negative length coming from a column panics
on main (`pad_data_capacity` does `len as usize`). That's not part of
this PR, and I'll open its own issue.1 parent 3bc3d0b commit 29580fa
6 files changed
Lines changed: 646 additions & 859 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
363 | 363 | | |
364 | 364 | | |
365 | 365 | | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
366 | 420 | | |
367 | 421 | | |
368 | 422 | | |
| |||
641 | 695 | | |
642 | 696 | | |
643 | 697 | | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
644 | 752 | | |
645 | 753 | | |
646 | 754 | | |
| |||
0 commit comments