Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7197#discussion_r34048458
--- Diff:
unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java ---
@@ -106,102 +132,203 @@ public int length() {
* @param until the position after last code point, exclusive.
*/
public UTF8String substring(final int start, final int until) {
- if (until <= start || start >= bytes.length) {
- return UTF8String.fromBytes(new byte[0]);
+ if (until <= start || start >= numBytes) {
+ return fromBytes(new byte[0]);
}
int i = 0;
int c = 0;
- for (; i < bytes.length && c < start; i += numBytes(bytes[i])) {
+ while (i < numBytes && c < start) {
+ i += numBytesForFirstByte(getByte(i));
c += 1;
}
int j = i;
- for (; j < bytes.length && c < until; j += numBytes(bytes[i])) {
+ while (i < numBytes && c < until) {
+ i += numBytesForFirstByte(getByte(i));
c += 1;
}
- return UTF8String.fromBytes(Arrays.copyOfRange(bytes, i, j));
+ byte[] bytes = new byte[i - j];
+ copyMemory(base, offset + j, bytes, BYTE_ARRAY_OFFSET, i - j);
+ return fromBytes(bytes);
}
+ /**
+ * Returns whether this contains `substring` or not.
+ */
public boolean contains(final UTF8String substring) {
- final byte[] b = substring.getBytes();
- if (b.length == 0) {
+ if (substring.numBytes == 0) {
return true;
}
- for (int i = 0; i <= bytes.length - b.length; i++) {
- if (bytes[i] == b[0] && startsWith(b, i)) {
+ byte first = substring.getByte(0);
+ for (int i = 0; i <= numBytes - substring.numBytes; i++) {
+ if (getByte(i) == first && matchAt(substring, i)) {
return true;
}
}
return false;
}
- private boolean startsWith(final byte[] prefix, int offsetInBytes) {
- if (prefix.length + offsetInBytes > bytes.length || offsetInBytes < 0)
{
+ /**
+ * Returns the byte at position `i`.
+ */
+ private byte getByte(int i) {
+ return UNSAFE.getByte(base, offset + i);
+ }
+
+ private boolean matchAt(final UTF8String s, int pos) {
+ if (s.numBytes + pos > numBytes || pos < 0) {
return false;
}
- int i = 0;
- while (i < prefix.length && prefix[i] == bytes[i + offsetInBytes]) {
- i++;
- }
- return i == prefix.length;
+ return ByteArrayMethods.arrayEquals(base, offset + pos, s.base,
s.offset, s.numBytes);
}
public boolean startsWith(final UTF8String prefix) {
- return startsWith(prefix.getBytes(), 0);
+ return matchAt(prefix, 0);
}
public boolean endsWith(final UTF8String suffix) {
- return startsWith(suffix.getBytes(), bytes.length -
suffix.getBytes().length);
+ return matchAt(suffix, numBytes - suffix.numBytes);
}
+ private static int mask(int numBits) {
+ return (1 << numBits) - 1;
+ }
+
+ /**
+ * Returns the code point that starts at `start`
+ */
+ private int codePointAt(int start, int num) {
+ byte first = getByte(start);
+ if (num == 1) {
+ return (int) first;
+ } else if (num == 2) {
+ // fast path
+ return ((first & 0x1F) << 6) | (getByte(start + 1) & 0x3F);
+ } else {
+ int code = first & mask(7 - num);
+ for (int i = 1; i < num; i ++) {
+ code <<= 6;
+ code += getByte(start + i) & 0x3F;
+ }
+ return code;
+ }
+ }
+
+ /**
+ * Update code point using UTF-8 encoding at (base, offset).
+ */
+ private static void updateCodePoint(Object base, long offset, int code,
int num) {
+ if (num == 1) {
+ UNSAFE.putByte(base, offset, (byte) code);
+ } else {
+ for (int i = 1; i < num; i++) {
+ UNSAFE.putByte(base, offset + num - i, (byte) (code & 0x3F |
0x80));
+ code >>>= 6;
+ }
+ int first = (code & mask(7 - num)) | ~mask(8 - num);
+ UNSAFE.putByte(base, offset, (byte) first);
+ }
+ }
+
+ private static final String lang = Locale.getDefault().getLanguage();
+ private static final boolean localeDependent = lang == "tr" || lang ==
"az" || lang == "lt";
+
+ /**
+ * Returns the upper case of this string
+ */
public UTF8String toUpperCase() {
- return UTF8String.fromString(toString().toUpperCase());
+ if (localeDependent) {
+ // fallback to String.toUpperCase() to handle locale
+ return fromString(toString().toUpperCase());
+ }
+
+ byte[] buf = null;
+ for (int i = 0; i < numBytes; ){
+ int n = numBytesForFirstByte(getByte(i));
+ int code = codePointAt(i, n);
+ int upper = Character.toUpperCase(code);
+ if (upper != code) {
+ if (buf == null) {
+ // It's always have the same number of bytes for upper case
+ buf = new byte[numBytes];
+ copyMemory(base, offset, buf, BYTE_ARRAY_OFFSET, numBytes);
+ }
+ updateCodePoint(buf, BYTE_ARRAY_OFFSET + i, upper, n);
+ }
+ i += n;
+ }
+ return buf != null ? fromBytes(buf) : this;
}
+ /**
+ * Returns the lower case of this string
+ */
public UTF8String toLowerCase() {
- return UTF8String.fromString(toString().toLowerCase());
+ if (localeDependent) {
+ // fallback to String.toLowerCase() to handle locale
+ return fromString(toString().toLowerCase());
+ }
+
+ byte[] buf = null;
+ for (int i = 0; i < numBytes; ){
+ int n = numBytesForFirstByte(getByte(i));
+ int code = codePointAt(i, n);
+ int lower = Character.toLowerCase(code);
+ if (lower != code) {
+ if (buf == null) {
+ // It's always have the same number of bytes for lower case
+ buf = new byte[numBytes];
+ copyMemory(base, offset, buf, BYTE_ARRAY_OFFSET, numBytes);
+ }
+ updateCodePoint(buf, BYTE_ARRAY_OFFSET + i, lower, n);
+ }
+ i += n;
+ }
+ return buf != null ? fromBytes(buf) : this;
}
@Override
public String toString() {
try {
- return new String(bytes, "utf-8");
+ return new String(getBytes(), "utf-8");
--- End diff --
String has the constructor like `public String(byte bytes[], int offset,
int length, String charsetName)`, will it be faster if we pass the bytes
directly? as In `getBytes`, it's possible to duplicate the byte array in some
case.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]