{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/35401208","id":35401208,"diff_hunk":"@@ -149,31 +158,141 @@ private[joins] object HashedRelation {\n   }\n }\n \n+/**\n+ * An extended CompactBuffer that could grow and update.\n+ */\n+private[joins] class MutableCompactBuffer[T: ClassTag] extends CompactBuffer[T] {\n+  override def growToSize(newSize: Int): Unit = super.growToSize(newSize)\n+  override def update(i: Int, v: T): Unit = super.update(i, v)\n+}\n \n /**\n  * A HashedRelation for UnsafeRow, which is backed by BytesToBytesMap that maps the key into a\n  * sequence of values.\n- *\n- * TODO(davies): use BytesToBytesMap\n  */\n private[joins] final class UnsafeHashedRelation(\n     private var hashTable: JavaHashMap[UnsafeRow, CompactBuffer[UnsafeRow]])\n   extends HashedRelation with Externalizable {\n \n-  def this() = this(null)  // Needed for serialization\n+  private[joins] def this() = this(null)  // Needed for serialization\n+\n+  // Use BytesToBytesMap in executor for better performance (it's created when deserialization)\n+  @transient private[this] var binaryMap: BytesToBytesMap = _\n+\n+  // A pool of compact buffers to reduce memory garbage\n+  @transient private[this] val bufferPool = new ThreadLocal[MutableCompactBuffer[UnsafeRow]]\n \n-  override def get(key: InternalRow): CompactBuffer[InternalRow] = {\n+  override def get(key: InternalRow): Seq[InternalRow] = {\n     val unsafeKey = key.asInstanceOf[UnsafeRow]\n-    // Thanks to type eraser\n-    hashTable.get(unsafeKey).asInstanceOf[CompactBuffer[InternalRow]]\n+\n+    if (binaryMap != null) {\n+      // Used in Broadcast join\n+      val loc = binaryMap.lookup(unsafeKey.getBaseObject, unsafeKey.getBaseOffset,\n+        unsafeKey.getSizeInBytes)\n+      if (loc.isDefined) {\n+        // thread-local buffer\n+        var buffer = bufferPool.get()\n+        if (buffer == null) {\n+          buffer = new MutableCompactBuffer[UnsafeRow]\n+          bufferPool.set(buffer)\n+        }\n+\n+        val base = loc.getValueAddress.getBaseObject\n+        var offset = loc.getValueAddress.getBaseOffset\n+        val last = loc.getValueAddress.getBaseOffset + loc.getValueLength\n+        var i = 0\n+        while (offset < last) {\n+          val numFields = PlatformDependent.UNSAFE.getInt(base, offset)\n+          val sizeInBytes = PlatformDependent.UNSAFE.getInt(base, offset + 4)\n+          offset += 8\n+\n+          // try to re-use the UnsafeRow in buffer, to reduce garbage\n+          buffer.growToSize(i + 1)\n+          if (buffer(i) == null) {\n+            buffer(i) = new UnsafeRow\n+          }\n+          buffer(i).pointTo(base, offset, numFields, sizeInBytes, null)\n+          i += 1\n+          offset += sizeInBytes\n+        }\n+        buffer\n+      } else {\n+        null\n+      }\n+\n+    } else {\n+      // Use the JavaHashMap in Local mode or ShuffleHashJoin\n+      hashTable.get(unsafeKey)\n+    }\n   }\n \n   override def writeExternal(out: ObjectOutput): Unit = {\n-    writeBytes(out, SparkSqlSerializer.serialize(hashTable))\n+    out.writeInt(hashTable.size())\n+\n+    val iter = hashTable.entrySet().iterator()\n+    while (iter.hasNext) {\n+      val entry = iter.next()\n+      val key = entry.getKey\n+      val values = entry.getValue\n+\n+      // write all the values as single byte array\n+      var totalSize = 0L\n+      var i = 0\n+      while (i < values.size) {\n+        totalSize += values(i).getSizeInBytes + 4 + 4\n+        i += 1\n+      }\n+      assert(totalSize < Integer.MAX_VALUE, \"values are too big\")\n+\n+      // [key size] [values size] [key bytes] [values bytes]\n+      out.writeInt(key.getSizeInBytes)\n+      out.writeInt(totalSize.toInt)\n+      out.write(key.getBytes)\n+      i = 0\n+      while (i < values.size) {\n+        // [num of fields] [num of bytes] [row bytes]\n+        // write the integer in native order, so they can be read by UNSAFE.getInt()\n+        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {\n+          out.writeInt(values(i).length())\n+          out.writeInt(values(i).getSizeInBytes)\n+        } else {\n+          out.writeInt(Integer.reverseBytes(values(i).length()))\n+          out.writeInt(Integer.reverseBytes(values(i).getSizeInBytes))\n+        }\n+        out.write(values(i).getBytes)","path":"sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala","position":176,"original_position":176,"commit_id":"1c5ad8dc04c12513eab2345064aa2de1bfb0873f","original_commit_id":"1c5ad8dc04c12513eab2345064aa2de1bfb0873f","user":{"login":"rxin","id":323388,"avatar_url":"https://avatars.githubusercontent.com/u/323388?v=3","gravatar_id":"","url":"https://api.github.com/users/rxin","html_url":"https://github.com/rxin","followers_url":"https://api.github.com/users/rxin/followers","following_url":"https://api.github.com/users/rxin/following{/other_user}","gists_url":"https://api.github.com/users/rxin/gists{/gist_id}","starred_url":"https://api.github.com/users/rxin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rxin/subscriptions","organizations_url":"https://api.github.com/users/rxin/orgs","repos_url":"https://api.github.com/users/rxin/repos","events_url":"https://api.github.com/users/rxin/events{/privacy}","received_events_url":"https://api.github.com/users/rxin/received_events","type":"User","site_admin":false},"body":"can you add a new method to UnsafeRow that writes to an ObjectOutput? It should be similar to Unsafe.writeToStream.\r\n ","created_at":"2015-07-24T07:36:25Z","updated_at":"2015-07-24T07:36:25Z","html_url":"https://github.com/apache/spark/pull/7592#discussion_r35401208","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/7592","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/35401208"},"html":{"href":"https://github.com/apache/spark/pull/7592#discussion_r35401208"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/7592"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/7592","id":40540937,"html_url":"https://github.com/apache/spark/pull/7592","diff_url":"https://github.com/apache/spark/pull/7592.diff","patch_url":"https://github.com/apache/spark/pull/7592.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/7592","number":7592,"state":"open","locked":false,"title":"[SPARK-9247] [SQL] Use BytesToBytesMap for broadcast join","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":"This PR introduce BytesToBytesMap to UnsafeHashedRelation, use it in executor for better performance.\r\n\r\nIt serialize all the key and values from java HashMap, put them into a BytesToBytesMap while deserializing. All the values for a same key are stored continuous to have better memory locality.\r\n\r\nThis PR also address the comments for #7480 , do some clean up.","created_at":"2015-07-22T08:25:49Z","updated_at":"2015-07-24T07:36:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ebb006db4a297d7abb85ab98620d0347635e8f66","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/7592/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/7592/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/7592/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/1c5ad8dc04c12513eab2345064aa2de1bfb0873f","head":{"label":"davies:unsafe_map2","ref":"unsafe_map2","sha":"1c5ad8dc04c12513eab2345064aa2de1bfb0873f","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-24T06:37:25Z","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":122599,"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":"a721ee52705100dbd7852f80f92cde4375517e48","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-24T07:16:57Z","pushed_at":"2015-07-24T07:30:51Z","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":1568031,"stargazers_count":4843,"watchers_count":4843,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4351,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":344,"forks":4351,"open_issues":344,"watchers":4843,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/7592"},"html":{"href":"https://github.com/apache/spark/pull/7592"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/7592"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/7592/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/7592/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/7592/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/1c5ad8dc04c12513eab2345064aa2de1bfb0873f"}}}}