{"action":"created","comment":{"url":"https://api.github.com/repos/google/flatbuffers/pulls/comments/60158612","id":60158612,"diff_hunk":"@@ -71,20 +83,34 @@ protected int __indirect(int offset) {\n    * @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.\n    */\n   protected String __string(int offset) {\n+    CharsetDecoder decoder = UTF8_DECODER.get();\n+    decoder.reset();\n+\n     offset += bb.getInt(offset);\n-    if (bb.hasArray()) {\n-      return new String(bb.array(), bb.arrayOffset() + offset + SIZEOF_INT, bb.getInt(offset),\n-                        FlatBufferBuilder.utf8charset);\n-    } else {\n-      // We can't access .array(), since the ByteBuffer is read-only,\n-      // off-heap or a memory map\n-      ByteBuffer bb = this.bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);\n-      // We're forced to make an extra copy:\n-      byte[] copy = new byte[bb.getInt(offset)];\n-      bb.position(offset + SIZEOF_INT);\n-      bb.get(copy);\n-      return new String(copy, 0, copy.length, FlatBufferBuilder.utf8charset);\n+    ByteBuffer src = bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);","path":"java/com/google/flatbuffers/Table.java","position":46,"original_position":46,"commit_id":"b0146b3d9a0e5f48269ad5186dc9c75023685b85","original_commit_id":"b0146b3d9a0e5f48269ad5186dc9c75023685b85","user":{"login":"pjulien","id":673277,"avatar_url":"https://avatars.githubusercontent.com/u/673277?v=3","gravatar_id":"","url":"https://api.github.com/users/pjulien","html_url":"https://github.com/pjulien","followers_url":"https://api.github.com/users/pjulien/followers","following_url":"https://api.github.com/users/pjulien/following{/other_user}","gists_url":"https://api.github.com/users/pjulien/gists{/gist_id}","starred_url":"https://api.github.com/users/pjulien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjulien/subscriptions","organizations_url":"https://api.github.com/users/pjulien/orgs","repos_url":"https://api.github.com/users/pjulien/repos","events_url":"https://api.github.com/users/pjulien/events{/privacy}","received_events_url":"https://api.github.com/users/pjulien/received_events","type":"User","site_admin":false},"body":"No, it's because if you don't duplicate it, it's not thread safe to multiple concurrent readers.  So in-between changing its position/limit and then resetting, any other reader that is using the same buffer would be susceptible to getting strange exceptions such as buffer underflow/overflow exceptions.\r\n","created_at":"2016-04-19T00:42:40Z","updated_at":"2016-04-19T00:42:40Z","html_url":"https://github.com/google/flatbuffers/pull/3854#discussion_r60158612","pull_request_url":"https://api.github.com/repos/google/flatbuffers/pulls/3854","_links":{"self":{"href":"https://api.github.com/repos/google/flatbuffers/pulls/comments/60158612"},"html":{"href":"https://github.com/google/flatbuffers/pull/3854#discussion_r60158612"},"pull_request":{"href":"https://api.github.com/repos/google/flatbuffers/pulls/3854"}}},"pull_request":{"url":"https://api.github.com/repos/google/flatbuffers/pulls/3854","id":66941746,"html_url":"https://github.com/google/flatbuffers/pull/3854","diff_url":"https://github.com/google/flatbuffers/pull/3854.diff","patch_url":"https://github.com/google/flatbuffers/pull/3854.patch","issue_url":"https://api.github.com/repos/google/flatbuffers/issues/3854","number":3854,"state":"open","locked":false,"title":"Fix for #3853","user":{"login":"pjulien","id":673277,"avatar_url":"https://avatars.githubusercontent.com/u/673277?v=3","gravatar_id":"","url":"https://api.github.com/users/pjulien","html_url":"https://github.com/pjulien","followers_url":"https://api.github.com/users/pjulien/followers","following_url":"https://api.github.com/users/pjulien/following{/other_user}","gists_url":"https://api.github.com/users/pjulien/gists{/gist_id}","starred_url":"https://api.github.com/users/pjulien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjulien/subscriptions","organizations_url":"https://api.github.com/users/pjulien/orgs","repos_url":"https://api.github.com/users/pjulien/repos","events_url":"https://api.github.com/users/pjulien/events{/privacy}","received_events_url":"https://api.github.com/users/pjulien/received_events","type":"User","site_admin":false},"body":"Removes the following allocations:\r\n- ``CharsetDecoder`` is reused between calls\r\n- ``CharBuffer#wrap`` removed in favor of heap\r\n  based char buffer that is reused\r\n- Temporary ``char[]``, an intermediate copy inside ``StringCoding``\r\n- Another ``char[]``, this is needed because ``StringCoding`` uses\r\n  a ``CharBuffer`` internally but returns a ``char[]``.  Extra\r\n  characters need to be trimmed so this means yet another allocation\r\n- Yet another ``char[]`` directly from ``__string`` for non-heap\r\n  based buffers\r\n\r\nRemoves the following copies\r\n- No copy is performed to trim the allocation since a ``CharBuffer``\r\n  is used directly\r\n- For non-heap based byte buffers, removes the copy that was\r\n  previously done in the __string function\r\n\r\nThis does need to get the TLS entry which implies at least some\r\ncontention on the thread object table and a fence.","created_at":"2016-04-19T00:04:46Z","updated_at":"2016-04-19T00:42:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"aa83c1e0cb61281bc8fdd23ce31c9465b4736ebf","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/google/flatbuffers/pulls/3854/commits","review_comments_url":"https://api.github.com/repos/google/flatbuffers/pulls/3854/comments","review_comment_url":"https://api.github.com/repos/google/flatbuffers/pulls/comments{/number}","comments_url":"https://api.github.com/repos/google/flatbuffers/issues/3854/comments","statuses_url":"https://api.github.com/repos/google/flatbuffers/statuses/b0146b3d9a0e5f48269ad5186dc9c75023685b85","head":{"label":"pjulien:3853","ref":"3853","sha":"b0146b3d9a0e5f48269ad5186dc9c75023685b85","user":{"login":"pjulien","id":673277,"avatar_url":"https://avatars.githubusercontent.com/u/673277?v=3","gravatar_id":"","url":"https://api.github.com/users/pjulien","html_url":"https://github.com/pjulien","followers_url":"https://api.github.com/users/pjulien/followers","following_url":"https://api.github.com/users/pjulien/following{/other_user}","gists_url":"https://api.github.com/users/pjulien/gists{/gist_id}","starred_url":"https://api.github.com/users/pjulien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjulien/subscriptions","organizations_url":"https://api.github.com/users/pjulien/orgs","repos_url":"https://api.github.com/users/pjulien/repos","events_url":"https://api.github.com/users/pjulien/events{/privacy}","received_events_url":"https://api.github.com/users/pjulien/received_events","type":"User","site_admin":false},"repo":{"id":29071921,"name":"flatbuffers","full_name":"pjulien/flatbuffers","owner":{"login":"pjulien","id":673277,"avatar_url":"https://avatars.githubusercontent.com/u/673277?v=3","gravatar_id":"","url":"https://api.github.com/users/pjulien","html_url":"https://github.com/pjulien","followers_url":"https://api.github.com/users/pjulien/followers","following_url":"https://api.github.com/users/pjulien/following{/other_user}","gists_url":"https://api.github.com/users/pjulien/gists{/gist_id}","starred_url":"https://api.github.com/users/pjulien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjulien/subscriptions","organizations_url":"https://api.github.com/users/pjulien/orgs","repos_url":"https://api.github.com/users/pjulien/repos","events_url":"https://api.github.com/users/pjulien/events{/privacy}","received_events_url":"https://api.github.com/users/pjulien/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/pjulien/flatbuffers","description":"Memory Efficient Serialization Library","fork":true,"url":"https://api.github.com/repos/pjulien/flatbuffers","forks_url":"https://api.github.com/repos/pjulien/flatbuffers/forks","keys_url":"https://api.github.com/repos/pjulien/flatbuffers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pjulien/flatbuffers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pjulien/flatbuffers/teams","hooks_url":"https://api.github.com/repos/pjulien/flatbuffers/hooks","issue_events_url":"https://api.github.com/repos/pjulien/flatbuffers/issues/events{/number}","events_url":"https://api.github.com/repos/pjulien/flatbuffers/events","assignees_url":"https://api.github.com/repos/pjulien/flatbuffers/assignees{/user}","branches_url":"https://api.github.com/repos/pjulien/flatbuffers/branches{/branch}","tags_url":"https://api.github.com/repos/pjulien/flatbuffers/tags","blobs_url":"https://api.github.com/repos/pjulien/flatbuffers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pjulien/flatbuffers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pjulien/flatbuffers/git/refs{/sha}","trees_url":"https://api.github.com/repos/pjulien/flatbuffers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pjulien/flatbuffers/statuses/{sha}","languages_url":"https://api.github.com/repos/pjulien/flatbuffers/languages","stargazers_url":"https://api.github.com/repos/pjulien/flatbuffers/stargazers","contributors_url":"https://api.github.com/repos/pjulien/flatbuffers/contributors","subscribers_url":"https://api.github.com/repos/pjulien/flatbuffers/subscribers","subscription_url":"https://api.github.com/repos/pjulien/flatbuffers/subscription","commits_url":"https://api.github.com/repos/pjulien/flatbuffers/commits{/sha}","git_commits_url":"https://api.github.com/repos/pjulien/flatbuffers/git/commits{/sha}","comments_url":"https://api.github.com/repos/pjulien/flatbuffers/comments{/number}","issue_comment_url":"https://api.github.com/repos/pjulien/flatbuffers/issues/comments{/number}","contents_url":"https://api.github.com/repos/pjulien/flatbuffers/contents/{+path}","compare_url":"https://api.github.com/repos/pjulien/flatbuffers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pjulien/flatbuffers/merges","archive_url":"https://api.github.com/repos/pjulien/flatbuffers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pjulien/flatbuffers/downloads","issues_url":"https://api.github.com/repos/pjulien/flatbuffers/issues{/number}","pulls_url":"https://api.github.com/repos/pjulien/flatbuffers/pulls{/number}","milestones_url":"https://api.github.com/repos/pjulien/flatbuffers/milestones{/number}","notifications_url":"https://api.github.com/repos/pjulien/flatbuffers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pjulien/flatbuffers/labels{/name}","releases_url":"https://api.github.com/repos/pjulien/flatbuffers/releases{/id}","deployments_url":"https://api.github.com/repos/pjulien/flatbuffers/deployments","created_at":"2015-01-10T21:37:23Z","updated_at":"2016-01-23T14:27:56Z","pushed_at":"2016-04-19T00:20:03Z","git_url":"git://github.com/pjulien/flatbuffers.git","ssh_url":"git@github.com:pjulien/flatbuffers.git","clone_url":"https://github.com/pjulien/flatbuffers.git","svn_url":"https://github.com/pjulien/flatbuffers","homepage":"http://google.github.io/flatbuffers/","size":2317,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"google:master","ref":"master","sha":"6f751d5d26fd566f8c9060358101a2596677f9cf","user":{"login":"google","id":1342004,"avatar_url":"https://avatars.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"repo":{"id":19953044,"name":"flatbuffers","full_name":"google/flatbuffers","owner":{"login":"google","id":1342004,"avatar_url":"https://avatars.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/google/flatbuffers","description":"Memory Efficient Serialization Library","fork":false,"url":"https://api.github.com/repos/google/flatbuffers","forks_url":"https://api.github.com/repos/google/flatbuffers/forks","keys_url":"https://api.github.com/repos/google/flatbuffers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/google/flatbuffers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/google/flatbuffers/teams","hooks_url":"https://api.github.com/repos/google/flatbuffers/hooks","issue_events_url":"https://api.github.com/repos/google/flatbuffers/issues/events{/number}","events_url":"https://api.github.com/repos/google/flatbuffers/events","assignees_url":"https://api.github.com/repos/google/flatbuffers/assignees{/user}","branches_url":"https://api.github.com/repos/google/flatbuffers/branches{/branch}","tags_url":"https://api.github.com/repos/google/flatbuffers/tags","blobs_url":"https://api.github.com/repos/google/flatbuffers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/google/flatbuffers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/google/flatbuffers/git/refs{/sha}","trees_url":"https://api.github.com/repos/google/flatbuffers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/google/flatbuffers/statuses/{sha}","languages_url":"https://api.github.com/repos/google/flatbuffers/languages","stargazers_url":"https://api.github.com/repos/google/flatbuffers/stargazers","contributors_url":"https://api.github.com/repos/google/flatbuffers/contributors","subscribers_url":"https://api.github.com/repos/google/flatbuffers/subscribers","subscription_url":"https://api.github.com/repos/google/flatbuffers/subscription","commits_url":"https://api.github.com/repos/google/flatbuffers/commits{/sha}","git_commits_url":"https://api.github.com/repos/google/flatbuffers/git/commits{/sha}","comments_url":"https://api.github.com/repos/google/flatbuffers/comments{/number}","issue_comment_url":"https://api.github.com/repos/google/flatbuffers/issues/comments{/number}","contents_url":"https://api.github.com/repos/google/flatbuffers/contents/{+path}","compare_url":"https://api.github.com/repos/google/flatbuffers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/google/flatbuffers/merges","archive_url":"https://api.github.com/repos/google/flatbuffers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/google/flatbuffers/downloads","issues_url":"https://api.github.com/repos/google/flatbuffers/issues{/number}","pulls_url":"https://api.github.com/repos/google/flatbuffers/pulls{/number}","milestones_url":"https://api.github.com/repos/google/flatbuffers/milestones{/number}","notifications_url":"https://api.github.com/repos/google/flatbuffers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/google/flatbuffers/labels{/name}","releases_url":"https://api.github.com/repos/google/flatbuffers/releases{/id}","deployments_url":"https://api.github.com/repos/google/flatbuffers/deployments","created_at":"2014-05-19T18:33:01Z","updated_at":"2016-04-18T22:31:59Z","pushed_at":"2016-04-19T00:20:03Z","git_url":"git://github.com/google/flatbuffers.git","ssh_url":"git@github.com:google/flatbuffers.git","clone_url":"https://github.com/google/flatbuffers.git","svn_url":"https://github.com/google/flatbuffers","homepage":"http://google.github.io/flatbuffers/","size":23691,"stargazers_count":5282,"watchers_count":5282,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":729,"mirror_url":null,"open_issues_count":93,"forks":729,"open_issues":93,"watchers":5282,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/google/flatbuffers/pulls/3854"},"html":{"href":"https://github.com/google/flatbuffers/pull/3854"},"issue":{"href":"https://api.github.com/repos/google/flatbuffers/issues/3854"},"comments":{"href":"https://api.github.com/repos/google/flatbuffers/issues/3854/comments"},"review_comments":{"href":"https://api.github.com/repos/google/flatbuffers/pulls/3854/comments"},"review_comment":{"href":"https://api.github.com/repos/google/flatbuffers/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/google/flatbuffers/pulls/3854/commits"},"statuses":{"href":"https://api.github.com/repos/google/flatbuffers/statuses/b0146b3d9a0e5f48269ad5186dc9c75023685b85"}}}}