Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7533#discussion_r35211154
--- Diff:
unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java ---
@@ -352,6 +373,180 @@ public int indexOf(UTF8String v, int start) {
return -1;
}
+ private enum ByteType {FIRSTBYTE, MIDBYTE, SINGLEBYTECHAR};
+
+ private ByteType checkByteType(Byte b) {
+ int firstTwoBits = (b >>> 6) & 0x03;
+ if (firstTwoBits == 3) {
+ return ByteType.FIRSTBYTE;
+ } else if (firstTwoBits == 2) {
+ return ByteType.MIDBYTE;
+ } else {
+ return ByteType.SINGLEBYTECHAR;
+ }
+ }
+
+ /**
+ * Return the first byte position for a given byte which shared the same
code point.
+ * @param bytePos any byte within the code point
+ * @return the first byte position of a given code point, throw
exception if not a valid UTF8 str
+ */
+ private int firstOfCurrentCodePoint(int bytePos) {
+ while (bytePos >= 0) {
+ if (ByteType.FIRSTBYTE == checkByteType(getByte(bytePos))
+ || ByteType.SINGLEBYTECHAR == checkByteType(getByte(bytePos))) {
+ return bytePos;
+ }
+ bytePos--;
+ }
+ throw new RuntimeException("Invalid utf8 string");
+ }
+
+ private int indexEnd(int startCodePoint) {
+ int i = numBytes -1; // position in byte
+ int c = numChars() - 1; // position in character
+ while (i >=0 && c > startCodePoint) {
+ i = firstOfCurrentCodePoint(i) - 1;
+ c -= 1;
+ }
+ return i;
+ }
+
+ /**
+ * Returns the index within this string of the last occurrence of the
+ * specified substring, searching backward starting at the specified
index.
+ * @param v the substring to search for.
+ * @param startCodePoint the index to start search from
+ * @return the index of the last occurrence of the specified substring,
+ * searching backward from the specified index,
+ * or {@code -1} if there is no such occurrence.
+ */
+ public int lastIndexOf(UTF8String v, int startCodePoint) {
+ return lastIndexOf(v, v.numChars(), startCodePoint);
+ }
+ public int lastIndexOf(UTF8String v, int vNumChars, int startCodePoint) {
+ if (v.numBytes == 0) {
+ return 0;
+ }
+ if (numBytes == 0) {
+ return -1;
+ }
+ int fromIndexEnd = indexEnd(startCodePoint);
--- End diff --
Since `numChars()` will be called within `indexEnd`, which mean we will
iterate all of the current UTF8String, can we reduce the overhead by going
through the bytes directly ?
---
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]