{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/65676784","id":65676784,"diff_hunk":"@@ -0,0 +1,160 @@\n+/*\n+ * Copyright 2016 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.handler.codec;\n+\n+import io.netty.buffer.ByteBuf;\n+import io.netty.channel.ChannelHandlerContext;\n+\n+import java.util.List;\n+\n+/**\n+ * A decoder that mostly mimics {@link LengthFieldBasedFrameDecoder} semantics. The only difference is this decoder\n+ * batches incoming {@link ByteBuf} messages and then decodes them at once, passing a big {@link ByteBuf} which can\n+ * possibly contain N messages (where N >= 1) through the pipeline at once.\n+ */\n+public class BulkLengthFieldBasedFrameDecoder extends BulkFrameDecoder {\n+\n+    private final int maxFrameLength;\n+    private final int lengthFieldOffset;\n+    private final int lengthFieldLength;\n+    private final int lengthFieldEndOffset;\n+\n+    /**\n+     * Creates a new instance.\n+     *\n+     * @param maxFrameLength the maximum length of the frame.  If the length of the frame is greater than this value,\n+     * {@link TooLongFrameException} will be thrown.\n+     * @param lengthFieldOffset the offset of the length field\n+     * @param lengthFieldLength the length of the length field\n+     */\n+    public BulkLengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) {\n+        this(0, maxFrameLength, lengthFieldOffset, lengthFieldLength);\n+    }\n+\n+    /**\n+     * Creates a new instance.\n+     *\n+     * @param startBufferSize the start buffer size which is used for batching incoming {@link ByteBuf} messages. Size\n+     * of this buffer must be greater or equal to zero. If the size of this buffer is zero then the default size will be\n+     * used (= 256 bytes).\n+     * @param maxFrameLength the maximum length of the frame.  If the length of the frame is greater than this value,\n+     * {@link TooLongFrameException} will be thrown.\n+     * @param lengthFieldOffset the offset of the length field\n+     * @param lengthFieldLength the length of the length field\n+     */\n+    public BulkLengthFieldBasedFrameDecoder(int startBufferSize, int maxFrameLength, int lengthFieldOffset,\n+                                            int lengthFieldLength) {\n+        super(startBufferSize);\n+\n+        if (maxFrameLength <= 0) {\n+            throw new IllegalArgumentException(\n+                    \"maxFrameLength must be a positive integer: \" +\n+                    maxFrameLength);\n+        }\n+\n+        if (lengthFieldOffset < 0) {\n+            throw new IllegalArgumentException(\n+                    \"lengthFieldOffset must be a non-negative integer: \" +\n+                    lengthFieldOffset);\n+        }\n+\n+        if (lengthFieldOffset > maxFrameLength - lengthFieldLength) {\n+            throw new IllegalArgumentException(\n+                    \"maxFrameLength (\" + maxFrameLength + \") \" +\n+                    \"must be equal to or greater than \" +\n+                    \"lengthFieldOffset (\" + lengthFieldOffset + \") + \" +\n+                    \"lengthFieldLength (\" + lengthFieldLength + \").\");\n+        }\n+\n+        this.maxFrameLength = maxFrameLength;\n+        this.lengthFieldOffset = lengthFieldOffset;\n+        this.lengthFieldLength = lengthFieldLength;\n+        lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength;\n+    }\n+\n+    @Override\n+    protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {\n+        int readableBytes = buffer.readableBytes();\n+        int rdIdx = buffer.readerIndex();\n+\n+        while (readableBytes >= lengthFieldEndOffset) {\n+            int actualLengthFieldOffset = rdIdx + lengthFieldOffset;\n+            long frameLength = getUnadjustedFrameLength(buffer, actualLengthFieldOffset, lengthFieldLength);\n+\n+            if (frameLength < 0) {\n+                throw new CorruptedFrameException(\n+                        \"negative pre-adjustment length field: \" + frameLength);\n+            }\n+\n+            frameLength += lengthFieldEndOffset;\n+\n+            if (frameLength > maxFrameLength) {\n+                throw new TooLongFrameException(\n+                        \"Adjusted frame length exceeds \" + maxFrameLength +\n+                        \": \" + frameLength + \" - discarded\");\n+            }\n+\n+            int frameLengthInt = (int) frameLength;\n+\n+            if (readableBytes < frameLengthInt) {\n+                break;\n+            }\n+\n+            readableBytes -= frameLengthInt;\n+            rdIdx += frameLengthInt;\n+        }\n+\n+        if (rdIdx == 0) {\n+            return;\n+        }\n+\n+        final int previousWriterIndex = buffer.writerIndex();\n+\n+        buffer.writerIndex(rdIdx);\n+\n+        final ByteBuf chunk = ctx.alloc().buffer(rdIdx, rdIdx).writeBytes(buffer, rdIdx);\n+\n+        buffer.writerIndex(previousWriterIndex);\n+        buffer.discardReadBytes();\n+\n+        out.add(chunk);\n+    }\n+\n+    protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length) {\n+        long frameLength;\n+        switch (length) {\n+        case 1:\n+            frameLength = buf.getUnsignedByte(offset);\n+            break;\n+        case 2:\n+            frameLength = buf.getUnsignedShort(offset);\n+            break;\n+        case 3:\n+            frameLength = buf.getUnsignedMedium(offset);\n+            break;\n+        case 4:\n+            frameLength = buf.getUnsignedInt(offset);\n+            break;\n+        case 8:\n+            frameLength = buf.getLong(offset);\n+            break;\n+        default:\n+            throw new DecoderException(\n+                    \"unsupported lengthFieldLength: \" + lengthFieldLength + \" (expected: 1, 2, 3, 4, or 8)\");","path":"codec/src/main/java/io/netty/handler/codec/BulkLengthFieldBasedFrameDecoder.java","position":156,"original_position":156,"commit_id":"cbb284c10a8b02fc89e9df109b28bf262b26a3f6","original_commit_id":"cbb284c10a8b02fc89e9df109b28bf262b26a3f6","user":{"login":"maseev","id":3434800,"avatar_url":"https://avatars.githubusercontent.com/u/3434800?v=3","gravatar_id":"","url":"https://api.github.com/users/maseev","html_url":"https://github.com/maseev","followers_url":"https://api.github.com/users/maseev/followers","following_url":"https://api.github.com/users/maseev/following{/other_user}","gists_url":"https://api.github.com/users/maseev/gists{/gist_id}","starred_url":"https://api.github.com/users/maseev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maseev/subscriptions","organizations_url":"https://api.github.com/users/maseev/orgs","repos_url":"https://api.github.com/users/maseev/repos","events_url":"https://api.github.com/users/maseev/events{/privacy}","received_events_url":"https://api.github.com/users/maseev/received_events","type":"User","site_admin":false},"body":"I don't think so. Either way you have to call an appropriate method which depends on an offset value. The same approach is used in LengthFieldBasedFrameDecoder. Seems legit for me.","created_at":"2016-06-03T09:02:03Z","updated_at":"2016-06-03T09:02:03Z","html_url":"https://github.com/netty/netty/pull/5332#discussion_r65676784","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/5332","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/65676784"},"html":{"href":"https://github.com/netty/netty/pull/5332#discussion_r65676784"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/5332"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/5332","id":72038010,"html_url":"https://github.com/netty/netty/pull/5332","diff_url":"https://github.com/netty/netty/pull/5332.diff","patch_url":"https://github.com/netty/netty/pull/5332.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/5332","number":5332,"state":"open","locked":false,"title":"Implemented optimised versions of LengthFieldBasedFrameDecoder and FixedLengthFrameDecoder","user":{"login":"maseev","id":3434800,"avatar_url":"https://avatars.githubusercontent.com/u/3434800?v=3","gravatar_id":"","url":"https://api.github.com/users/maseev","html_url":"https://github.com/maseev","followers_url":"https://api.github.com/users/maseev/followers","following_url":"https://api.github.com/users/maseev/following{/other_user}","gists_url":"https://api.github.com/users/maseev/gists{/gist_id}","starred_url":"https://api.github.com/users/maseev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maseev/subscriptions","organizations_url":"https://api.github.com/users/maseev/orgs","repos_url":"https://api.github.com/users/maseev/repos","events_url":"https://api.github.com/users/maseev/events{/privacy}","received_events_url":"https://api.github.com/users/maseev/received_events","type":"User","site_admin":false},"body":"Motivation:\r\n\r\nAccording to performance tests, LengthFieldBasedFrameDecoder and FixedLengthFrameDecoder generate a lot of garbage as well as perform quite poorly in cases where clients send data at a very high-speed rate.\r\n\r\nModifications:\r\n\r\n- Introduce a base class which implements batching of all incoming ByteBuf messages as well as provides a convenient API for decoding the resulting chunk of data\r\n- Introduce an optimised version of LengthFieldBasedFrameDecoder - BulkLengthFieldBasedFrameDecoder\r\n- Introduce an optimised version of FixedLengthFrameDecoder - BulkFixedLengthFrameDecoder\r\n\r\nResult:\r\n\r\nBulkLengthFieldBasedFrameDecoder and BulkFixedLengthFrameDecoder will provide much better throughput and lower latency as compared to LengthFieldBasedFrameDecoder and FixedLengthFrameDecoder\r\nThis PR is related to #5280","created_at":"2016-05-31T15:38:40Z","updated_at":"2016-06-03T09:02:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"eb443aab457e2e4b95f6c53f362f071d7b91b4e1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/netty/netty/pulls/5332/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/5332/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/5332/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/cbb284c10a8b02fc89e9df109b28bf262b26a3f6","head":{"label":"maseev:feature/iss5280","ref":"feature/iss5280","sha":"cbb284c10a8b02fc89e9df109b28bf262b26a3f6","user":{"login":"maseev","id":3434800,"avatar_url":"https://avatars.githubusercontent.com/u/3434800?v=3","gravatar_id":"","url":"https://api.github.com/users/maseev","html_url":"https://github.com/maseev","followers_url":"https://api.github.com/users/maseev/followers","following_url":"https://api.github.com/users/maseev/following{/other_user}","gists_url":"https://api.github.com/users/maseev/gists{/gist_id}","starred_url":"https://api.github.com/users/maseev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maseev/subscriptions","organizations_url":"https://api.github.com/users/maseev/orgs","repos_url":"https://api.github.com/users/maseev/repos","events_url":"https://api.github.com/users/maseev/events{/privacy}","received_events_url":"https://api.github.com/users/maseev/received_events","type":"User","site_admin":false},"repo":{"id":59356610,"name":"netty","full_name":"maseev/netty","owner":{"login":"maseev","id":3434800,"avatar_url":"https://avatars.githubusercontent.com/u/3434800?v=3","gravatar_id":"","url":"https://api.github.com/users/maseev","html_url":"https://github.com/maseev","followers_url":"https://api.github.com/users/maseev/followers","following_url":"https://api.github.com/users/maseev/following{/other_user}","gists_url":"https://api.github.com/users/maseev/gists{/gist_id}","starred_url":"https://api.github.com/users/maseev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maseev/subscriptions","organizations_url":"https://api.github.com/users/maseev/orgs","repos_url":"https://api.github.com/users/maseev/repos","events_url":"https://api.github.com/users/maseev/events{/privacy}","received_events_url":"https://api.github.com/users/maseev/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/maseev/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/maseev/netty","forks_url":"https://api.github.com/repos/maseev/netty/forks","keys_url":"https://api.github.com/repos/maseev/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/maseev/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/maseev/netty/teams","hooks_url":"https://api.github.com/repos/maseev/netty/hooks","issue_events_url":"https://api.github.com/repos/maseev/netty/issues/events{/number}","events_url":"https://api.github.com/repos/maseev/netty/events","assignees_url":"https://api.github.com/repos/maseev/netty/assignees{/user}","branches_url":"https://api.github.com/repos/maseev/netty/branches{/branch}","tags_url":"https://api.github.com/repos/maseev/netty/tags","blobs_url":"https://api.github.com/repos/maseev/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/maseev/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/maseev/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/maseev/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/maseev/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/maseev/netty/languages","stargazers_url":"https://api.github.com/repos/maseev/netty/stargazers","contributors_url":"https://api.github.com/repos/maseev/netty/contributors","subscribers_url":"https://api.github.com/repos/maseev/netty/subscribers","subscription_url":"https://api.github.com/repos/maseev/netty/subscription","commits_url":"https://api.github.com/repos/maseev/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/maseev/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/maseev/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/maseev/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/maseev/netty/contents/{+path}","compare_url":"https://api.github.com/repos/maseev/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/maseev/netty/merges","archive_url":"https://api.github.com/repos/maseev/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/maseev/netty/downloads","issues_url":"https://api.github.com/repos/maseev/netty/issues{/number}","pulls_url":"https://api.github.com/repos/maseev/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/maseev/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/maseev/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/maseev/netty/labels{/name}","releases_url":"https://api.github.com/repos/maseev/netty/releases{/id}","deployments_url":"https://api.github.com/repos/maseev/netty/deployments","created_at":"2016-05-21T11:34:03Z","updated_at":"2016-05-21T11:34:15Z","pushed_at":"2016-06-01T04:49:51Z","git_url":"git://github.com/maseev/netty.git","ssh_url":"git@github.com:maseev/netty.git","clone_url":"https://github.com/maseev/netty.git","svn_url":"https://github.com/maseev/netty","homepage":"http://netty.io/","size":40562,"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":"4.1"}},"base":{"label":"netty:4.1","ref":"4.1","sha":"bbed330468b5b82c9e4defa59012d0fcdb70f1aa","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}","deployments_url":"https://api.github.com/repos/netty/netty/deployments","created_at":"2010-11-09T09:22:21Z","updated_at":"2016-06-03T08:48:39Z","pushed_at":"2016-06-02T16:27:20Z","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":67863,"stargazers_count":6988,"watchers_count":6988,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3570,"mirror_url":null,"open_issues_count":308,"forks":3570,"open_issues":308,"watchers":6988,"default_branch":"4.1"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/5332"},"html":{"href":"https://github.com/netty/netty/pull/5332"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/5332"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/5332/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/5332/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/5332/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/cbb284c10a8b02fc89e9df109b28bf262b26a3f6"}}}}
{"actor":{"display_login":"maseev"}}