{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/43752358","id":43752358,"diff_hunk":"@@ -0,0 +1,190 @@\n+/*\n+ * Copyright 2015 The Netty Project\n+ *\n+ * The Netty Project licenses this file to you under the Apache License,\n+ * version 2.0 (the \"License\"); you may not use this file except in compliance\n+ * with the License. You may obtain a copy of the License at:\n+ *\n+ *   http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+ * License for the specific language governing permissions and limitations\n+ * under the License.\n+ */\n+package io.netty.buffer;\n+\n+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;\n+\n+import io.netty.util.IllegalReferenceCountException;\n+import io.netty.util.Recycler;\n+import io.netty.util.internal.PlatformDependent;\n+\n+final class PooledSlicedByteBuf extends SlicedAbstractByteBuf {\n+\n+    private static final AtomicIntegerFieldUpdater<PooledSlicedByteBuf> refCntUpdater;\n+\n+    static {\n+        AtomicIntegerFieldUpdater<PooledSlicedByteBuf> updater = PlatformDependent\n+                .newAtomicIntegerFieldUpdater(PooledSlicedByteBuf.class, \"refCnt\");\n+        if (updater == null) {\n+            updater = AtomicIntegerFieldUpdater.newUpdater(PooledSlicedByteBuf.class, \"refCnt\");\n+        }\n+        refCntUpdater = updater;\n+    }\n+\n+    private volatile int refCnt = 1;\n+\n+    private final Recycler.Handle recyclerHandle;\n+\n+    private static final Recycler<PooledSlicedByteBuf> RECYCLER = new Recycler<PooledSlicedByteBuf>() {\n+        @Override\n+        protected PooledSlicedByteBuf newObject(Handle handle) {\n+            return new PooledSlicedByteBuf(handle);\n+        }\n+    };\n+\n+    static PooledSlicedByteBuf newInstance(ByteBuf buffer, int index, int length) {\n+        PooledSlicedByteBuf buf = RECYCLER.get();\n+        buf.init(buffer, index, length);\n+        buf.setRefCnt(1);\n+        buffer.retain();\n+        return buf;\n+    }\n+\n+    private PooledSlicedByteBuf(Recycler.Handle recyclerHandle) {\n+        this.recyclerHandle = recyclerHandle;\n+    }\n+\n+    @Override\n+    public ByteBuf duplicate(boolean retain) {\n+        return retain ? PooledDuplicatedByteBuf.newInstance(this) : duplicate();\n+    }\n+\n+    @Override\n+    public ByteBuf duplicate() {\n+        return new DuplicatedAbstractByteBuf(this);\n+    }\n+\n+    @Override\n+    public ByteBuf slice(int index, int length, boolean retain) {\n+        return retain ? newInstance(this, index, length) : slice(index, length);\n+    }\n+\n+    @Override\n+    public ByteBuf slice(boolean retain) {\n+        return slice(readerIndex(), readableBytes(), retain);\n+    }\n+\n+    @Override\n+    public ByteBuf slice(int index, int length) {\n+        checkIndex0(index, length);\n+        return new SlicedAbstractByteBuf(this, index, length);\n+    }\n+\n+    @Override\n+    public ByteBuf readSlice(int length, boolean retain) {\n+        ByteBuf slice = slice(readerIndex(), length, retain);\n+        skipBytes(length);\n+        return slice;\n+    }\n+\n+    /**\n+     * Called once {@link #refCnt()} is equals 0.\n+     */\n+    private void deallocate() {\n+        unwrap().release();\n+        RECYCLER.recycle(this, recyclerHandle);\n+    }\n+\n+    @Override\n+    public int refCnt() {\n+        return refCnt;\n+    }\n+\n+    /**\n+     * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly\n+     */\n+    protected void setRefCnt(int refCnt) {\n+        this.refCnt = refCnt;\n+    }\n+\n+    @Override\n+    public ByteBuf retain() {\n+        for (;;) {\n+            int refCnt = this.refCnt;\n+            if (refCnt == 0) {","path":"buffer/src/main/java/io/netty/buffer/PooledSlicedByteBuf.java","position":117,"original_position":117,"commit_id":"6744b97d0cffacbfcda24de283ac20e9267ba24c","original_commit_id":"6744b97d0cffacbfcda24de283ac20e9267ba24c","user":{"login":"ninja-","id":5637961,"avatar_url":"https://avatars.githubusercontent.com/u/5637961?v=3","gravatar_id":"","url":"https://api.github.com/users/ninja-","html_url":"https://github.com/ninja-","followers_url":"https://api.github.com/users/ninja-/followers","following_url":"https://api.github.com/users/ninja-/following{/other_user}","gists_url":"https://api.github.com/users/ninja-/gists{/gist_id}","starred_url":"https://api.github.com/users/ninja-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ninja-/subscriptions","organizations_url":"https://api.github.com/users/ninja-/orgs","repos_url":"https://api.github.com/users/ninja-/repos","events_url":"https://api.github.com/users/ninja-/events{/privacy}","received_events_url":"https://api.github.com/users/ninja-/received_events","type":"User","site_admin":false},"body":"not really, there's some complicated hierarchy in this pr to get the slice to be \"abstract\" bytebuf","created_at":"2015-11-03T14:25:09Z","updated_at":"2015-11-03T14:25:09Z","html_url":"https://github.com/netty/netty/pull/4421#discussion_r43752358","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/4421","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/43752358"},"html":{"href":"https://github.com/netty/netty/pull/4421#discussion_r43752358"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/4421"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/4421","id":49242303,"html_url":"https://github.com/netty/netty/pull/4421","diff_url":"https://github.com/netty/netty/pull/4421.diff","patch_url":"https://github.com/netty/netty/pull/4421.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/4421","number":4421,"state":"open","locked":false,"title":"[#4333] Re-add PooledSlicedByteBuf and DuplicatedByteBuf","user":{"login":"merlimat","id":62500,"avatar_url":"https://avatars.githubusercontent.com/u/62500?v=3","gravatar_id":"","url":"https://api.github.com/users/merlimat","html_url":"https://github.com/merlimat","followers_url":"https://api.github.com/users/merlimat/followers","following_url":"https://api.github.com/users/merlimat/following{/other_user}","gists_url":"https://api.github.com/users/merlimat/gists{/gist_id}","starred_url":"https://api.github.com/users/merlimat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/merlimat/subscriptions","organizations_url":"https://api.github.com/users/merlimat/orgs","repos_url":"https://api.github.com/users/merlimat/repos","events_url":"https://api.github.com/users/merlimat/events{/privacy}","received_events_url":"https://api.github.com/users/merlimat/received_events","type":"User","site_admin":false},"body":"Motivation:\r\n\r\nPooling ByteBufs for slice / duplicate operations can miminize object creation a lot. We should try to pool these if possible.\r\n\r\nModifications:\r\n\r\nAdd new overloaded operations to ByteBuf that can be used to obtain a retained slice or duplicate. These can be pooled internally as optimization.\r\nUse optimized version in codecs.\r\nResult:\r\n\r\nLess object creation when using PooledByteBufAllocator.\r\n\r\n\r\n---- \r\n\r\nNotes: \r\n\r\nThis is based on @normanmaurer PR #4394. I've just added the reference count in `Pooled(Sliced|Duplicated)ByteBuf` to know at which point we can recycle it.\r\nGiven that multiple inheritance is impossible, I could only reuse the `AbstractDerivedByteBuf` class, and I had to copy&paste the refCnt code from `AbstractReferenceCountedByteBuf`. I really don't know whether it might be easier to just inherit from `AbstractReferenceCountedByteBuf` and copy the `DuplicateByteBuf` methods.","created_at":"2015-10-30T08:23:13Z","updated_at":"2015-11-03T14:25:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":"81fb9aaf8792ecfea36df9477d3d9e9bd862b8ea","assignee":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/netty/netty/milestones/126","html_url":"https://github.com/netty/netty/milestones/4.1.0.CR1","labels_url":"https://api.github.com/repos/netty/netty/milestones/126/labels","id":1286123,"number":126,"title":"4.1.0.CR1","description":"","creator":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"open_issues":64,"closed_issues":22,"state":"open","created_at":"2015-09-02T12:42:53Z","updated_at":"2015-11-03T02:23:22Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/netty/netty/pulls/4421/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/4421/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/4421/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/6744b97d0cffacbfcda24de283ac20e9267ba24c","head":{"label":"merlimat:recyclable-sliced","ref":"recyclable-sliced","sha":"6744b97d0cffacbfcda24de283ac20e9267ba24c","user":{"login":"merlimat","id":62500,"avatar_url":"https://avatars.githubusercontent.com/u/62500?v=3","gravatar_id":"","url":"https://api.github.com/users/merlimat","html_url":"https://github.com/merlimat","followers_url":"https://api.github.com/users/merlimat/followers","following_url":"https://api.github.com/users/merlimat/following{/other_user}","gists_url":"https://api.github.com/users/merlimat/gists{/gist_id}","starred_url":"https://api.github.com/users/merlimat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/merlimat/subscriptions","organizations_url":"https://api.github.com/users/merlimat/orgs","repos_url":"https://api.github.com/users/merlimat/repos","events_url":"https://api.github.com/users/merlimat/events{/privacy}","received_events_url":"https://api.github.com/users/merlimat/received_events","type":"User","site_admin":false},"repo":{"id":40392815,"name":"netty","full_name":"merlimat/netty","owner":{"login":"merlimat","id":62500,"avatar_url":"https://avatars.githubusercontent.com/u/62500?v=3","gravatar_id":"","url":"https://api.github.com/users/merlimat","html_url":"https://github.com/merlimat","followers_url":"https://api.github.com/users/merlimat/followers","following_url":"https://api.github.com/users/merlimat/following{/other_user}","gists_url":"https://api.github.com/users/merlimat/gists{/gist_id}","starred_url":"https://api.github.com/users/merlimat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/merlimat/subscriptions","organizations_url":"https://api.github.com/users/merlimat/orgs","repos_url":"https://api.github.com/users/merlimat/repos","events_url":"https://api.github.com/users/merlimat/events{/privacy}","received_events_url":"https://api.github.com/users/merlimat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/merlimat/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/merlimat/netty","forks_url":"https://api.github.com/repos/merlimat/netty/forks","keys_url":"https://api.github.com/repos/merlimat/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/merlimat/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/merlimat/netty/teams","hooks_url":"https://api.github.com/repos/merlimat/netty/hooks","issue_events_url":"https://api.github.com/repos/merlimat/netty/issues/events{/number}","events_url":"https://api.github.com/repos/merlimat/netty/events","assignees_url":"https://api.github.com/repos/merlimat/netty/assignees{/user}","branches_url":"https://api.github.com/repos/merlimat/netty/branches{/branch}","tags_url":"https://api.github.com/repos/merlimat/netty/tags","blobs_url":"https://api.github.com/repos/merlimat/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/merlimat/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/merlimat/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/merlimat/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/merlimat/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/merlimat/netty/languages","stargazers_url":"https://api.github.com/repos/merlimat/netty/stargazers","contributors_url":"https://api.github.com/repos/merlimat/netty/contributors","subscribers_url":"https://api.github.com/repos/merlimat/netty/subscribers","subscription_url":"https://api.github.com/repos/merlimat/netty/subscription","commits_url":"https://api.github.com/repos/merlimat/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/merlimat/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/merlimat/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/merlimat/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/merlimat/netty/contents/{+path}","compare_url":"https://api.github.com/repos/merlimat/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/merlimat/netty/merges","archive_url":"https://api.github.com/repos/merlimat/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/merlimat/netty/downloads","issues_url":"https://api.github.com/repos/merlimat/netty/issues{/number}","pulls_url":"https://api.github.com/repos/merlimat/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/merlimat/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/merlimat/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/merlimat/netty/labels{/name}","releases_url":"https://api.github.com/repos/merlimat/netty/releases{/id}","created_at":"2015-08-08T05:38:35Z","updated_at":"2015-08-08T05:38:43Z","pushed_at":"2015-10-30T23:40:10Z","git_url":"git://github.com/merlimat/netty.git","ssh_url":"git@github.com:merlimat/netty.git","clone_url":"https://github.com/merlimat/netty.git","svn_url":"https://github.com/merlimat/netty","homepage":"http://netty.io/","size":44605,"stargazers_count":0,"watchers_count":0,"language":"Java","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":"netty:4.0","ref":"4.0","sha":"d353d5936b582681ca215d0029960e22a6d8764c","user":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","created_at":"2010-11-09T09:22:21Z","updated_at":"2015-11-03T13:59:19Z","pushed_at":"2015-11-03T13:19:47Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":137232,"stargazers_count":5500,"watchers_count":5500,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2793,"mirror_url":null,"open_issues_count":323,"forks":2793,"open_issues":323,"watchers":5500,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/4421"},"html":{"href":"https://github.com/netty/netty/pull/4421"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/4421"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/4421/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/4421/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/4421/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/6744b97d0cffacbfcda24de283ac20e9267ba24c"}}}}