{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/32098715","id":32098715,"diff_hunk":"@@ -37,23 +40,74 @@\n  */\n public final class CompressedXContent {\n \n+    private static int crc32(BytesReference data) {\n+        OutputStream dummy = new OutputStream() {\n+            @Override\n+            public void write(int b) throws IOException {\n+                // no-op\n+            }\n+            @Override\n+            public void write(byte[] b, int off, int len) throws IOException {\n+                // no-op\n+            }\n+        };\n+        CRC32 crc32 = new CRC32();\n+        try {\n+            data.writeTo(new CheckedOutputStream(dummy, crc32));\n+        } catch (IOException bogus) {\n+            // cannot happen\n+            throw new Error(bogus);\n+        }\n+        return (int) crc32.getValue();\n+    }\n+\n     private final byte[] bytes;\n-    private int hashCode;\n+    private final int crc32;\n \n-    public CompressedXContent(BytesReference data) throws IOException {\n+    private CompressedXContent(BytesReference data, Integer crc32) throws IOException {\n         Compressor compressor = CompressorFactory.compressor(data);\n         if (compressor != null) {\n             // already compressed...\n             this.bytes = data.toBytes();\n+            if (crc32 != null) {\n+                this.crc32 = crc32;\n+            } else {\n+                this.crc32 = crc32(new BytesArray(uncompressed()));\n+            }\n         } else {\n             BytesStreamOutput out = new BytesStreamOutput();\n-            try (StreamOutput compressedOutput = CompressorFactory.defaultCompressor().streamOutput(out)) {\n-                data.writeTo(compressedOutput);\n+            try (OutputStream compressedOutput = CompressorFactory.defaultCompressor().streamOutput(out)) {\n+                CRC32 checksum = null;\n+                OutputStream checkedOutput = compressedOutput;\n+                if (crc32 == null) {\n+                    // compute the checksum if it was not provided\n+                    checksum = new CRC32();\n+                    checkedOutput = new CheckedOutputStream(compressedOutput, checksum);\n+                }\n+                data.writeTo(checkedOutput);\n+                if (crc32 == null) {\n+                    this.crc32 = (int) checksum.getValue();\n+                } else {\n+                    this.crc32 = crc32;\n+                }\n             }\n             this.bytes = out.bytes().toBytes();\n             assert CompressorFactory.compressor(new BytesArray(bytes)) != null;\n         }\n \n+        assert this.crc32 == crc32(new BytesArray(uncompressed()));\n+    }\n+\n+    /**\n+     * Create a new {@link CompressedXContent} with an already pre-computed\n+     * crc32. This must be the crc32 of the <b>uncompressed</b> data.\n+     */\n+    public CompressedXContent(BytesReference data, int crc32) throws IOException {","path":"core/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java","position":81,"original_position":81,"commit_id":"ed349a9e943d980ac17985eaebb2a456144baf1a","original_commit_id":"ed349a9e943d980ac17985eaebb2a456144baf1a","user":{"login":"jpountz","id":299848,"avatar_url":"https://avatars.githubusercontent.com/u/299848?v=3","gravatar_id":"","url":"https://api.github.com/users/jpountz","html_url":"https://github.com/jpountz","followers_url":"https://api.github.com/users/jpountz/followers","following_url":"https://api.github.com/users/jpountz/following{/other_user}","gists_url":"https://api.github.com/users/jpountz/gists{/gist_id}","starred_url":"https://api.github.com/users/jpountz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpountz/subscriptions","organizations_url":"https://api.github.com/users/jpountz/orgs","repos_url":"https://api.github.com/users/jpountz/repos","events_url":"https://api.github.com/users/jpountz/events{/privacy}","received_events_url":"https://api.github.com/users/jpountz/received_events","type":"User","site_admin":false},"body":"I like this suggestion!","created_at":"2015-06-10T08:51:59Z","updated_at":"2015-06-10T08:51:59Z","html_url":"https://github.com/elastic/elasticsearch/pull/11428#discussion_r32098715","pull_request_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428","_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/32098715"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/11428#discussion_r32098715"},"pull_request":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428","id":36524590,"html_url":"https://github.com/elastic/elasticsearch/pull/11428","diff_url":"https://github.com/elastic/elasticsearch/pull/11428.diff","patch_url":"https://github.com/elastic/elasticsearch/pull/11428.patch","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/11428","number":11428,"state":"open","locked":false,"title":"Make CompressedXContent.equals fast again.","user":{"login":"jpountz","id":299848,"avatar_url":"https://avatars.githubusercontent.com/u/299848?v=3","gravatar_id":"","url":"https://api.github.com/users/jpountz","html_url":"https://github.com/jpountz","followers_url":"https://api.github.com/users/jpountz/followers","following_url":"https://api.github.com/users/jpountz/following{/other_user}","gists_url":"https://api.github.com/users/jpountz/gists{/gist_id}","starred_url":"https://api.github.com/users/jpountz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpountz/subscriptions","organizations_url":"https://api.github.com/users/jpountz/orgs","repos_url":"https://api.github.com/users/jpountz/repos","events_url":"https://api.github.com/users/jpountz/events{/privacy}","received_events_url":"https://api.github.com/users/jpountz/received_events","type":"User","site_admin":false},"body":"We had to make CompressedXContent.equals decompress data to fix some\r\ncorrectness issues which had the downside of making equals() slow. Now we store\r\na crc32 alongside compressed data which should help avoid decompress data in\r\nmost cases.\r\n\r\nClose #11247","created_at":"2015-05-29T16:57:31Z","updated_at":"2015-06-10T08:52:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4ea838e5f9eb76c064e81963c43b4f91927621c1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428/commits","review_comments_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428/comments","review_comment_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/11428/comments","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/faa58ac01f0f415b6bd06913c6ac11a71d848e33","head":{"label":"jpountz:fix/CompressedXConten_fast","ref":"fix/CompressedXConten_fast","sha":"faa58ac01f0f415b6bd06913c6ac11a71d848e33","user":{"login":"jpountz","id":299848,"avatar_url":"https://avatars.githubusercontent.com/u/299848?v=3","gravatar_id":"","url":"https://api.github.com/users/jpountz","html_url":"https://github.com/jpountz","followers_url":"https://api.github.com/users/jpountz/followers","following_url":"https://api.github.com/users/jpountz/following{/other_user}","gists_url":"https://api.github.com/users/jpountz/gists{/gist_id}","starred_url":"https://api.github.com/users/jpountz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpountz/subscriptions","organizations_url":"https://api.github.com/users/jpountz/orgs","repos_url":"https://api.github.com/users/jpountz/repos","events_url":"https://api.github.com/users/jpountz/events{/privacy}","received_events_url":"https://api.github.com/users/jpountz/received_events","type":"User","site_admin":false},"repo":{"id":9633891,"name":"elasticsearch","full_name":"jpountz/elasticsearch","owner":{"login":"jpountz","id":299848,"avatar_url":"https://avatars.githubusercontent.com/u/299848?v=3","gravatar_id":"","url":"https://api.github.com/users/jpountz","html_url":"https://github.com/jpountz","followers_url":"https://api.github.com/users/jpountz/followers","following_url":"https://api.github.com/users/jpountz/following{/other_user}","gists_url":"https://api.github.com/users/jpountz/gists{/gist_id}","starred_url":"https://api.github.com/users/jpountz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpountz/subscriptions","organizations_url":"https://api.github.com/users/jpountz/orgs","repos_url":"https://api.github.com/users/jpountz/repos","events_url":"https://api.github.com/users/jpountz/events{/privacy}","received_events_url":"https://api.github.com/users/jpountz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jpountz/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":true,"url":"https://api.github.com/repos/jpountz/elasticsearch","forks_url":"https://api.github.com/repos/jpountz/elasticsearch/forks","keys_url":"https://api.github.com/repos/jpountz/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jpountz/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jpountz/elasticsearch/teams","hooks_url":"https://api.github.com/repos/jpountz/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/jpountz/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/jpountz/elasticsearch/events","assignees_url":"https://api.github.com/repos/jpountz/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/jpountz/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/jpountz/elasticsearch/tags","blobs_url":"https://api.github.com/repos/jpountz/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jpountz/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jpountz/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/jpountz/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jpountz/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/jpountz/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/jpountz/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/jpountz/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/jpountz/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/jpountz/elasticsearch/subscription","commits_url":"https://api.github.com/repos/jpountz/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/jpountz/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/jpountz/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/jpountz/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/jpountz/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/jpountz/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jpountz/elasticsearch/merges","archive_url":"https://api.github.com/repos/jpountz/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jpountz/elasticsearch/downloads","issues_url":"https://api.github.com/repos/jpountz/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/jpountz/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/jpountz/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/jpountz/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jpountz/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/jpountz/elasticsearch/releases{/id}","created_at":"2013-04-23T21:37:49Z","updated_at":"2015-05-15T20:08:35Z","pushed_at":"2015-06-10T08:52:01Z","git_url":"git://github.com/jpountz/elasticsearch.git","ssh_url":"git@github.com:jpountz/elasticsearch.git","clone_url":"https://github.com/jpountz/elasticsearch.git","svn_url":"https://github.com/jpountz/elasticsearch","homepage":"http://elasticsearch.org","size":122965,"stargazers_count":2,"watchers_count":2,"language":"Java","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":2,"default_branch":"master"}},"base":{"label":"elastic:master","ref":"master","sha":"49bef19878a90e2be35cd2bcb1275e7e1073238a","user":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"repo":{"id":507775,"name":"elasticsearch","full_name":"elastic/elasticsearch","owner":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elastic/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":false,"url":"https://api.github.com/repos/elastic/elasticsearch","forks_url":"https://api.github.com/repos/elastic/elasticsearch/forks","keys_url":"https://api.github.com/repos/elastic/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elastic/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elastic/elasticsearch/teams","hooks_url":"https://api.github.com/repos/elastic/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/elastic/elasticsearch/events","assignees_url":"https://api.github.com/repos/elastic/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/elastic/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/elastic/elasticsearch/tags","blobs_url":"https://api.github.com/repos/elastic/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elastic/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elastic/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/elastic/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/elastic/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/elastic/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/elastic/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/elastic/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/elastic/elasticsearch/subscription","commits_url":"https://api.github.com/repos/elastic/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/elastic/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/elastic/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/elastic/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elastic/elasticsearch/merges","archive_url":"https://api.github.com/repos/elastic/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elastic/elasticsearch/downloads","issues_url":"https://api.github.com/repos/elastic/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/elastic/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/elastic/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/elastic/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elastic/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/elastic/elasticsearch/releases{/id}","created_at":"2010-02-08T13:20:56Z","updated_at":"2015-06-10T08:35:01Z","pushed_at":"2015-06-10T08:39:53Z","git_url":"git://github.com/elastic/elasticsearch.git","ssh_url":"git@github.com:elastic/elasticsearch.git","clone_url":"https://github.com/elastic/elasticsearch.git","svn_url":"https://github.com/elastic/elasticsearch","homepage":"https://www.elastic.co/products/elasticsearch","size":1889042,"stargazers_count":11541,"watchers_count":11541,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3661,"mirror_url":null,"open_issues_count":1094,"forks":3661,"open_issues":1094,"watchers":11541,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/11428"},"issue":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/11428"},"comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/11428/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/11428/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/elasticsearch/statuses/faa58ac01f0f415b6bd06913c6ac11a71d848e33"}}}}