{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/33984870","id":33984870,"diff_hunk":"@@ -106,92 +168,125 @@ public int length() {\n    * @param until the position after last code point, exclusive.\n    */\n   public UTF8String substring(final int start, final int until) {\n-    if (until <= start || start >= bytes.length) {\n+    if (until <= start || start >= numBytes) {\n       return UTF8String.fromBytes(new byte[0]);\n     }\n \n     int i = 0;\n     int c = 0;\n-    for (; i < bytes.length && c < start; i += numBytes(bytes[i])) {\n+    for (; i < numBytes && c < start; i += numBytesForFirstByte(getByte(i))) {\n       c += 1;\n     }\n \n     int j = i;\n-    for (; j < bytes.length && c < until; j += numBytes(bytes[i])) {\n+    for (; j < numBytes && c < until; j += numBytesForFirstByte(getByte(i))) {\n       c += 1;\n     }\n \n-    return UTF8String.fromBytes(Arrays.copyOfRange(bytes, i, j));\n+    byte[] bytes = new byte[j - i];\n+    copyMemory(base, offset + i, bytes, BYTE_ARRAY_OFFSET, j - i);\n+    return UTF8String.fromBytes(bytes);\n   }\n \n   public boolean contains(final UTF8String substring) {\n-    final byte[] b = substring.getBytes();\n-    if (b.length == 0) {\n+    if (substring.numBytes == 0) {\n       return true;\n     }\n \n-    for (int i = 0; i <= bytes.length - b.length; i++) {\n-      if (bytes[i] == b[0] && startsWith(b, i)) {\n+    byte first = substring.getByte(0);\n+    for (int i = 0; i <= numBytes - substring.numBytes; i++) {\n+      if (getByte(i) == first && matchAt(substring, i)) {\n         return true;\n       }\n     }\n     return false;\n   }\n \n-  private boolean startsWith(final byte[] prefix, int offsetInBytes) {\n-    if (prefix.length + offsetInBytes > bytes.length || offsetInBytes < 0) {\n+  private byte getByte(int i) {\n+    return UNSAFE.getByte(base, offset + i);\n+  }\n+\n+  private boolean matchAt(final UTF8String s, int pos) {\n+    if (s.numBytes + pos > numBytes || pos < 0) {\n       return false;\n     }\n-    int i = 0;\n-    while (i < prefix.length && prefix[i] == bytes[i + offsetInBytes]) {\n-      i++;\n-    }\n-    return i == prefix.length;\n+    return ByteArrayMethods.arrayEquals(base, offset + pos, s.base, s.offset, s.numBytes);\n   }\n \n   public boolean startsWith(final UTF8String prefix) {\n-    return startsWith(prefix.getBytes(), 0);\n+    return matchAt(prefix, 0);\n   }\n \n   public boolean endsWith(final UTF8String suffix) {\n-    return startsWith(suffix.getBytes(), bytes.length - suffix.getBytes().length);\n+    return matchAt(suffix, numBytes - suffix.numBytes);\n   }\n \n   public UTF8String toUpperCase() {\n-    return UTF8String.fromString(toString().toUpperCase());\n+    // It's always have the same number of bytes for upper case\n+    byte[] buf = new byte[numBytes];\n+    copyMemory(base, offset, buf, BYTE_ARRAY_OFFSET, numBytes);\n+\n+    for (int i = 0; i < numBytes; ){\n+      int n = numBytesForFirstByte(getByte(i));\n+      int code = codePointAt(i, n);\n+      int upper = Character.toUpperCase(code);\n+      if (upper != code) {\n+        updateCodePoint(buf, BYTE_ARRAY_OFFSET + i, upper, n);\n+      }\n+      i += n;\n+    }\n+\n+    return fromBytes(buf);\n   }\n \n   public UTF8String toLowerCase() {\n-    return UTF8String.fromString(toString().toLowerCase());\n+    // It's always have the same number of bytes for upper case\n+    byte[] buf = new byte[numBytes];\n+    copyMemory(base, offset, buf, BYTE_ARRAY_OFFSET, numBytes);\n+\n+    for (int i = 0; i < numBytes; ){\n+      int n = numBytesForFirstByte(getByte(i));\n+      int code = codePointAt(i, n);\n+      int lower = Character.toLowerCase(code);\n+      if (lower != code) {\n+        updateCodePoint(buf, BYTE_ARRAY_OFFSET + i, lower, n);\n+      }\n+      i += n;\n+    }\n+    return fromBytes(buf);\n   }\n \n   @Override\n   public String toString() {\n     try {\n-      return new String(bytes, \"utf-8\");\n+      // this is slow\n+      return new String(getBytes(), \"utf-8\");\n     } catch (UnsupportedEncodingException e) {\n       // Turn the exception into unchecked so we can find out about it at runtime, but\n       // don't need to add lots of boilerplate code everywhere.\n-      PlatformDependent.throwException(e);\n+      throwException(e);\n       return \"unknown\";  // we will never reach here.\n     }\n   }\n \n   @Override\n   public UTF8String clone() {\n-    return new UTF8String().set(bytes);\n+    return fromBytes(getBytes());\n   }\n \n   @Override\n   public int compareTo(final UTF8String other) {\n-    final byte[] b = other.getBytes();\n-    for (int i = 0; i < bytes.length && i < b.length; i++) {\n-      int res = bytes[i] - b[i];\n+    int len = numBytes < other.numBytes ? numBytes : other.numBytes;\n+    int i = 0;\n+    // TODO: compare 8 bytes as unsigned long\n+    while (i < len) {\n+      int res = (getByte(i) & 0xFF) - (other.getByte(i) & 0xFF);","path":"unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java","position":306,"original_position":306,"commit_id":"32c3de03dfa23e58bd485563cbdfa5278996588e","original_commit_id":"32c3de03dfa23e58bd485563cbdfa5278996588e","user":{"login":"JoshRosen","id":50748,"avatar_url":"https://avatars.githubusercontent.com/u/50748?v=3","gravatar_id":"","url":"https://api.github.com/users/JoshRosen","html_url":"https://github.com/JoshRosen","followers_url":"https://api.github.com/users/JoshRosen/followers","following_url":"https://api.github.com/users/JoshRosen/following{/other_user}","gists_url":"https://api.github.com/users/JoshRosen/gists{/gist_id}","starred_url":"https://api.github.com/users/JoshRosen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JoshRosen/subscriptions","organizations_url":"https://api.github.com/users/JoshRosen/orgs","repos_url":"https://api.github.com/users/JoshRosen/repos","events_url":"https://api.github.com/users/JoshRosen/events{/privacy}","received_events_url":"https://api.github.com/users/JoshRosen/received_events","type":"User","site_admin":false},"body":"Maybe we should add a comment here to explain that this is performing unsigned comparison.","created_at":"2015-07-06T21:30:20Z","updated_at":"2015-07-06T21:30:20Z","html_url":"https://github.com/apache/spark/pull/7197#discussion_r33984870","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/7197","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/33984870"},"html":{"href":"https://github.com/apache/spark/pull/7197#discussion_r33984870"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/7197"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/7197","id":39121173,"html_url":"https://github.com/apache/spark/pull/7197","diff_url":"https://github.com/apache/spark/pull/7197.diff","patch_url":"https://github.com/apache/spark/pull/7197.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/7197","number":7197,"state":"open","locked":false,"title":"[SPARK-7190] [SPARK-8804] [SPARK-7815] unsafe UTF8String","user":{"login":"davies","id":40902,"avatar_url":"https://avatars.githubusercontent.com/u/40902?v=3","gravatar_id":"","url":"https://api.github.com/users/davies","html_url":"https://github.com/davies","followers_url":"https://api.github.com/users/davies/followers","following_url":"https://api.github.com/users/davies/following{/other_user}","gists_url":"https://api.github.com/users/davies/gists{/gist_id}","starred_url":"https://api.github.com/users/davies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davies/subscriptions","organizations_url":"https://api.github.com/users/davies/orgs","repos_url":"https://api.github.com/users/davies/repos","events_url":"https://api.github.com/users/davies/events{/privacy}","received_events_url":"https://api.github.com/users/davies/received_events","type":"User","site_admin":false},"body":"Let UTF8String work with binary buffer, avoid the copy when get a UTF8String from UnsafeRow\r\n\r\ncc @rxin @JoshRosen ","created_at":"2015-07-02T20:10:51Z","updated_at":"2015-07-06T21:30:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"873c687169808e03f2494cffb4f1d5e279f5da7c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/7197/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/7197/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/7197/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/32c3de03dfa23e58bd485563cbdfa5278996588e","head":{"label":"davies:unsafe_string","ref":"unsafe_string","sha":"32c3de03dfa23e58bd485563cbdfa5278996588e","user":{"login":"davies","id":40902,"avatar_url":"https://avatars.githubusercontent.com/u/40902?v=3","gravatar_id":"","url":"https://api.github.com/users/davies","html_url":"https://github.com/davies","followers_url":"https://api.github.com/users/davies/followers","following_url":"https://api.github.com/users/davies/following{/other_user}","gists_url":"https://api.github.com/users/davies/gists{/gist_id}","starred_url":"https://api.github.com/users/davies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davies/subscriptions","organizations_url":"https://api.github.com/users/davies/orgs","repos_url":"https://api.github.com/users/davies/repos","events_url":"https://api.github.com/users/davies/events{/privacy}","received_events_url":"https://api.github.com/users/davies/received_events","type":"User","site_admin":false},"repo":{"id":21724875,"name":"spark","full_name":"davies/spark","owner":{"login":"davies","id":40902,"avatar_url":"https://avatars.githubusercontent.com/u/40902?v=3","gravatar_id":"","url":"https://api.github.com/users/davies","html_url":"https://github.com/davies","followers_url":"https://api.github.com/users/davies/followers","following_url":"https://api.github.com/users/davies/following{/other_user}","gists_url":"https://api.github.com/users/davies/gists{/gist_id}","starred_url":"https://api.github.com/users/davies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davies/subscriptions","organizations_url":"https://api.github.com/users/davies/orgs","repos_url":"https://api.github.com/users/davies/repos","events_url":"https://api.github.com/users/davies/events{/privacy}","received_events_url":"https://api.github.com/users/davies/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/davies/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/davies/spark","forks_url":"https://api.github.com/repos/davies/spark/forks","keys_url":"https://api.github.com/repos/davies/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/davies/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/davies/spark/teams","hooks_url":"https://api.github.com/repos/davies/spark/hooks","issue_events_url":"https://api.github.com/repos/davies/spark/issues/events{/number}","events_url":"https://api.github.com/repos/davies/spark/events","assignees_url":"https://api.github.com/repos/davies/spark/assignees{/user}","branches_url":"https://api.github.com/repos/davies/spark/branches{/branch}","tags_url":"https://api.github.com/repos/davies/spark/tags","blobs_url":"https://api.github.com/repos/davies/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/davies/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/davies/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/davies/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/davies/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/davies/spark/languages","stargazers_url":"https://api.github.com/repos/davies/spark/stargazers","contributors_url":"https://api.github.com/repos/davies/spark/contributors","subscribers_url":"https://api.github.com/repos/davies/spark/subscribers","subscription_url":"https://api.github.com/repos/davies/spark/subscription","commits_url":"https://api.github.com/repos/davies/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/davies/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/davies/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/davies/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/davies/spark/contents/{+path}","compare_url":"https://api.github.com/repos/davies/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/davies/spark/merges","archive_url":"https://api.github.com/repos/davies/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/davies/spark/downloads","issues_url":"https://api.github.com/repos/davies/spark/issues{/number}","pulls_url":"https://api.github.com/repos/davies/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/davies/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/davies/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/davies/spark/labels{/name}","releases_url":"https://api.github.com/repos/davies/spark/releases{/id}","created_at":"2014-07-11T06:39:41Z","updated_at":"2015-04-08T19:45:01Z","pushed_at":"2015-07-06T20:48:34Z","git_url":"git://github.com/davies/spark.git","ssh_url":"git@github.com:davies/spark.git","clone_url":"https://github.com/davies/spark.git","svn_url":"https://github.com/davies/spark","homepage":null,"size":119432,"stargazers_count":1,"watchers_count":1,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"37e4d92142a6309e2df7d36883e0c7892c3d792d","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-07-06T19:54:10Z","pushed_at":"2015-07-06T20:49:55Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1499032,"stargazers_count":4646,"watchers_count":4646,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4123,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":349,"forks":4123,"open_issues":349,"watchers":4646,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/7197"},"html":{"href":"https://github.com/apache/spark/pull/7197"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/7197"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/7197/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/7197/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/7197/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/32c3de03dfa23e58bd485563cbdfa5278996588e"}}}}