Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7197#discussion_r33830851
  
    --- Diff: 
unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java ---
    @@ -106,92 +105,126 @@ 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) {
    +    if (until <= start || start >= size) {
           return UTF8String.fromBytes(new byte[0]);
         }
     
         int i = 0;
         int c = 0;
    -    for (; i < bytes.length && c < start; i += numBytes(bytes[i])) {
    +    for (; i < size && c < start; i += numBytes(getByte(i))) {
           c += 1;
         }
     
         int j = i;
    -    for (; j < bytes.length && c < until; j += numBytes(bytes[i])) {
    +    for (; j < size && c < until; j += numBytes(getByte(i))) {
           c += 1;
         }
     
    -    return UTF8String.fromBytes(Arrays.copyOfRange(bytes, i, j));
    +    byte[] bytes = new byte[j - i];
    +    copyMemory(base, offset + i, bytes, BYTE_ARRAY_OFFSET, j - i);
    +    return UTF8String.fromBytes(bytes);
       }
     
       public boolean contains(final UTF8String substring) {
    -    final byte[] b = substring.getBytes();
    -    if (b.length == 0) {
    +    if (substring.size == 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 <= size - substring.size; 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) 
{
    +  private long getLong(int i) {
    +    return UNSAFE.getLong(base, offset + i);
    +  }
    +
    +  private byte getByte(int i) {
    +    return UNSAFE.getByte(base, offset + i);
    +  }
    +
    +  private boolean matchAt(final UTF8String s, int pos) {
    --- End diff --
    
    The loops here in `matchAt`, could probably be replaced by a single call to 
`ByteArrayMethods.arrayEquals`, provided that we make a non-word-aligned 
version of that method.  There is some example code in Guava that you could 
base this on. We can defer this for later, too, but I figure that since we've 
basically already written this method here then we might as well put it into a 
place where it can be used more generically.


---
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]

Reply via email to