SEPURI-SAI-KRISHNA commented on code in PR #29004:
URL: https://github.com/apache/flink/pull/29004#discussion_r3837406804
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -310,28 +310,18 @@ public static String lpad(String base, int len, String
pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ // Lengths are counted in characters (code points) rather than UTF-16
code units, so that a
+ // supplementary-plane character is never truncated into an unpaired
surrogate.
+ final int keepEnd = endOfFirstCodePoints(base, len);
+ final int baseLen = base.codePointCount(0, keepEnd);
- // the length of the padding needed
- int pos = Math.max(len - base.length(), 0);
+ final StringBuilder sb = new StringBuilder(len);
Review Comment:
We don't, it's gone. I used it because the result is no longer `len` chars
once a supplementary-plane character is involved, so `new char[len]` is the
wrong size. That size is computable up front, so the exact-size `char[]` is
back:
```java
int baseEnd = endOfCodePoints(base, len);
int padChars = padLength(pad, len - base.codePointCount(0, baseEnd));
char[] data = new char[padChars + baseEnd];
```
Best-of-5 over 2M calls each, against the current implementation:
```
lpad("order-id-99213", 40, " ") current 148.5 ns/op patch 127.9 ns/op
lpad(<100k-char base>, 10, " ") current 411.5 ms patch 0.6 ms
```
The second row is the truncation path: the current code calls
`base.toCharArray()` on the whole base before discarding almost all of it.
--
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]