You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The lpad_utf8_int32_utf8 and rpad_utf8_int32_utf8 functions have performance inefficiency and a potential memory safety issue:
Performance: Single-byte fills iterate character-by-character when memset would suffice. Multi-byte fills use O(n) iterations instead of O(log n) with a doubling strategy.
Memory safety: When the fill string is longer than the padding space needed, the code could write more bytes than allocated. Fixed preventatively.
What changes are included in this PR?
Memory safety fix: Use std::min(fill_text_len, total_fill_bytes) for the initial copy to prevent overflow
Fast path: Add single-byte fill optimization using memset
General path: Replace character-by-character loop with doubling strategy for multi-byte fills
Tests: Add comprehensive tests for the new code paths
Are these changes tested?
Yes. Added tests covering:
Large UTF-8 fill characters (4-byte emoji, 3-byte Chinese)
Single-byte fill boundaries (1 char and 65536 char padding)
Content verification for fill patterns
Doubling strategy boundaries
Partial fill scenarios (fill text longer than padding needed)
Platform: Apple M3, macOS Benchmark: cpp/src/gandiva/tests/micro_benchmarks.cc, 10 repetitions, 1 million rows per test
The original RPAD was pathologically slow compared to LPAD due to different algorithms. For 65K padding: LPAD took ~29ms while RPAD took ~992ms (34x slower for identical operation). The optimization applies the same efficient algorithm to both functions.
LPAD (mean time in μs)
Benchmark
Original
Optimized
Speedup
Minimal (9 padding chars)
147
148
-
Small (99 padding chars)
312
167
1.9x
Medium (100 padding chars)
368
219
1.7x
Large (1000 padding chars)
16,242
16,273
-
XLarge (65436 padding chars)
29,115
27,987
1.04x
RPAD (mean time in μs)
Benchmark
Original
Optimized
Speedup
Minimal (9 padding chars)
247
148
1.7x
Small (99 padding chars)
1,704
165
10x
Medium (100 padding chars)
1,773
216
8x
Large (1000 padding chars)
30,813
16,082
1.9x
XLarge (65436 padding chars)
992,334
27,724
36x
dmitry-chirkov-dremio
changed the title
GH-49438: [C++][Gandiva] Optimize lpad/rpad UTF-8 functions
GH-49438: [C++][Gandiva] Optimize LPAD/RPAD functions
Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
The
lpad_utf8_int32_utf8andrpad_utf8_int32_utf8functions have performance inefficiency and a potential memory safety issue:memsetwould suffice. Multi-byte fills use O(n) iterations instead of O(log n) with a doubling strategy.What changes are included in this PR?
std::min(fill_text_len, total_fill_bytes)for the initial copy to prevent overflowmemsetAre these changes tested?
Yes. Added tests covering:
Are there any user-facing changes?
No.