{"action":"created","comment":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/69307416","id":69307416,"diff_hunk":"@@ -0,0 +1,92 @@\n+/*\n+ * Copyright 2016, Google Inc. All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or without\n+ * modification, are permitted provided that the following conditions are\n+ * met:\n+ *\n+ *    * Redistributions of source code must retain the above copyright\n+ * notice, this list of conditions and the following disclaimer.\n+ *    * Redistributions in binary form must reproduce the above\n+ * copyright notice, this list of conditions and the following disclaimer\n+ * in the documentation and/or other materials provided with the\n+ * distribution.\n+ *\n+ *    * Neither the name of Google Inc. nor the names of its\n+ * contributors may be used to endorse or promote products derived from\n+ * this software without specific prior written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+ * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+package io.grpc.netty;\n+\n+import io.netty.buffer.ByteBuf;\n+import io.netty.buffer.ByteBufAllocator;\n+import io.netty.channel.ChannelHandlerContext;\n+import io.netty.channel.ChannelOutboundHandlerAdapter;\n+import io.netty.channel.ChannelPromise;\n+import io.netty.util.ReferenceCountUtil;\n+\n+public class WriteCombiningHandler extends ChannelOutboundHandlerAdapter {\n+\n+  private static final int BUFFER_SIZE = 4096;\n+\n+  private ByteBuf buffer;\n+  private ByteBufAllocator allocator;\n+\n+  @Override\n+  public void handlerAdded(ChannelHandlerContext ctx) throws Exception {\n+    allocator = ctx.channel().alloc();\n+  }\n+\n+  @Override\n+  public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {\n+    if (buffer != null) {\n+      ReferenceCountUtil.safeRelease(buffer);\n+      buffer = null;\n+    }\n+  }\n+\n+  @Override\n+  public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {\n+    ByteBuf data = (ByteBuf) msg;\n+    if (buffer == null) {\n+      buffer = allocator.directBuffer(BUFFER_SIZE, BUFFER_SIZE);\n+    }\n+\n+    if (buffer.writableBytes() >= data.readableBytes()) {\n+      buffer.writeBytes(data);\n+      promise.setSuccess();","path":"netty/src/main/java/io/grpc/netty/WriteCombiningHandler.java","position":69,"original_position":69,"commit_id":"368a571a5ac46e1766c6acf55832fb327a232f75","original_commit_id":"368a571a5ac46e1766c6acf55832fb327a232f75","user":{"login":"nmittler","id":6493296,"avatar_url":"https://avatars.githubusercontent.com/u/6493296?v=3","gravatar_id":"","url":"https://api.github.com/users/nmittler","html_url":"https://github.com/nmittler","followers_url":"https://api.github.com/users/nmittler/followers","following_url":"https://api.github.com/users/nmittler/following{/other_user}","gists_url":"https://api.github.com/users/nmittler/gists{/gist_id}","starred_url":"https://api.github.com/users/nmittler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmittler/subscriptions","organizations_url":"https://api.github.com/users/nmittler/orgs","repos_url":"https://api.github.com/users/nmittler/repos","events_url":"https://api.github.com/users/nmittler/events{/privacy}","received_events_url":"https://api.github.com/users/nmittler/received_events","type":"User","site_admin":false},"body":"FYI, I think the [SimpleChannelPromiseAggregator](https://github.com/netty/netty/blob/4.1/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2CodecUtil.java#L218) is probably the way to go here. Perhaps we should make that a top-level class in Netty and get it out of the HTTP2 package.","created_at":"2016-07-01T14:27:32Z","updated_at":"2016-07-01T14:27:32Z","html_url":"https://github.com/grpc/grpc-java/pull/1996#discussion_r69307416","pull_request_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1996","_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/69307416"},"html":{"href":"https://github.com/grpc/grpc-java/pull/1996#discussion_r69307416"},"pull_request":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1996"}}},"pull_request":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/1996","id":75833586,"html_url":"https://github.com/grpc/grpc-java/pull/1996","diff_url":"https://github.com/grpc/grpc-java/pull/1996.diff","patch_url":"https://github.com/grpc/grpc-java/pull/1996.patch","issue_url":"https://api.github.com/repos/grpc/grpc-java/issues/1996","number":1996,"state":"open","locked":false,"title":"DNM: netty: add handler to combine small writes into larger buffer","user":{"login":"buchgr","id":1388179,"avatar_url":"https://avatars.githubusercontent.com/u/1388179?v=3","gravatar_id":"","url":"https://api.github.com/users/buchgr","html_url":"https://github.com/buchgr","followers_url":"https://api.github.com/users/buchgr/followers","following_url":"https://api.github.com/users/buchgr/following{/other_user}","gists_url":"https://api.github.com/users/buchgr/gists{/gist_id}","starred_url":"https://api.github.com/users/buchgr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/buchgr/subscriptions","organizations_url":"https://api.github.com/users/buchgr/orgs","repos_url":"https://api.github.com/users/buchgr/repos","events_url":"https://api.github.com/users/buchgr/events{/privacy}","received_events_url":"https://api.github.com/users/buchgr/received_events","type":"User","site_admin":false},"body":"This is a proof of concept that combines smaller `ByteBufs` into a larger `ByteBuf` by copying. The idea came out of a discussion with @normanmaurer today, where he dropped his wisdom on me :-). Amongst others he mentioned that passing many small buffers to `writev` can often be much slower than one big buffer to `write`. I decided to give it a try and implemented a `WriteCombiningHandler` that simply copies many smaller buffers into a bigger buffer. See the benchmark below.\r\n\r\nNow, this is still quite early and I have to experiment and benchmark a lot more. Also, this handler does not interact well with a channel's writability and promise's aren't implemented correctly (yet). Also, the handler is quite dumb in that it just combines all writes. We could instead only combine buffers less than N bytes, and have different sized buffers to optimize for memory usage (i.e. don't allocate a 4KB buffer to merge 5 writes totalling 250 bytes). Many more sophisticated heuristics are conceivable.\r\n\r\nFurthermore, @normanmaurer mentioned that if real performance benefits can be shown, this capability should be added to Netty's `ChannelOutboundBuffer`.\r\n\r\n```\r\n./qps_client --client_payload=20 --server_payload=40 --outstanding_rpcs=2000 \r\n--channels=1 --streaming_rpcs --directexecutor --address=localhost:8080\r\n```\r\n\r\n```\r\n./qps_server --directexecutor --address=localhost:8080\r\n```\r\n\r\nBest out of 3 runs:\r\n\r\nBefore:\r\n```\r\nChannels:                       1\r\nOutstanding RPCs per Channel:   2000\r\nServer Payload Size:            40\r\nClient Payload Size:            20\r\n50%ile Latency (in micros):     10367\r\n90%ile Latency (in micros):     13743\r\n95%ile Latency (in micros):     14703\r\n99%ile Latency (in micros):     18255\r\n99.9%ile Latency (in micros):   36159\r\nMaximum Latency (in micros):    129471\r\nQPS:                            189670\r\n```\r\n\r\nAfter:\r\n```\r\nChannels:                       1\r\nOutstanding RPCs per Channel:   2000\r\nServer Payload Size:            40\r\nClient Payload Size:            20\r\n50%ile Latency (in micros):     9007\r\n90%ile Latency (in micros):     10799\r\n95%ile Latency (in micros):     11895\r\n99%ile Latency (in micros):     13495\r\n99.9%ile Latency (in micros):   22431\r\nMaximum Latency (in micros):    107327\r\nQPS:                            217794\r\n```\r\n\r\nThoughts? @ejona86 @nmittler @carl-mastrangelo @louiscryan ","created_at":"2016-06-30T14:51:51Z","updated_at":"2016-07-01T14:27:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c32e874f5f1dae84bab6757b5c06acf1c4a56dea","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1996/commits","review_comments_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1996/comments","review_comment_url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/grpc/grpc-java/issues/1996/comments","statuses_url":"https://api.github.com/repos/grpc/grpc-java/statuses/368a571a5ac46e1766c6acf55832fb327a232f75","head":{"label":"buchgr:buffers","ref":"buffers","sha":"368a571a5ac46e1766c6acf55832fb327a232f75","user":{"login":"buchgr","id":1388179,"avatar_url":"https://avatars.githubusercontent.com/u/1388179?v=3","gravatar_id":"","url":"https://api.github.com/users/buchgr","html_url":"https://github.com/buchgr","followers_url":"https://api.github.com/users/buchgr/followers","following_url":"https://api.github.com/users/buchgr/following{/other_user}","gists_url":"https://api.github.com/users/buchgr/gists{/gist_id}","starred_url":"https://api.github.com/users/buchgr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/buchgr/subscriptions","organizations_url":"https://api.github.com/users/buchgr/orgs","repos_url":"https://api.github.com/users/buchgr/repos","events_url":"https://api.github.com/users/buchgr/events{/privacy}","received_events_url":"https://api.github.com/users/buchgr/received_events","type":"User","site_admin":false},"repo":{"id":31381511,"name":"grpc-java","full_name":"buchgr/grpc-java","owner":{"login":"buchgr","id":1388179,"avatar_url":"https://avatars.githubusercontent.com/u/1388179?v=3","gravatar_id":"","url":"https://api.github.com/users/buchgr","html_url":"https://github.com/buchgr","followers_url":"https://api.github.com/users/buchgr/followers","following_url":"https://api.github.com/users/buchgr/following{/other_user}","gists_url":"https://api.github.com/users/buchgr/gists{/gist_id}","starred_url":"https://api.github.com/users/buchgr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/buchgr/subscriptions","organizations_url":"https://api.github.com/users/buchgr/orgs","repos_url":"https://api.github.com/users/buchgr/repos","events_url":"https://api.github.com/users/buchgr/events{/privacy}","received_events_url":"https://api.github.com/users/buchgr/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/buchgr/grpc-java","description":"The Java gRPC implementation. HTTP/2 based RPC","fork":true,"url":"https://api.github.com/repos/buchgr/grpc-java","forks_url":"https://api.github.com/repos/buchgr/grpc-java/forks","keys_url":"https://api.github.com/repos/buchgr/grpc-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/buchgr/grpc-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/buchgr/grpc-java/teams","hooks_url":"https://api.github.com/repos/buchgr/grpc-java/hooks","issue_events_url":"https://api.github.com/repos/buchgr/grpc-java/issues/events{/number}","events_url":"https://api.github.com/repos/buchgr/grpc-java/events","assignees_url":"https://api.github.com/repos/buchgr/grpc-java/assignees{/user}","branches_url":"https://api.github.com/repos/buchgr/grpc-java/branches{/branch}","tags_url":"https://api.github.com/repos/buchgr/grpc-java/tags","blobs_url":"https://api.github.com/repos/buchgr/grpc-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/buchgr/grpc-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/buchgr/grpc-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/buchgr/grpc-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/buchgr/grpc-java/statuses/{sha}","languages_url":"https://api.github.com/repos/buchgr/grpc-java/languages","stargazers_url":"https://api.github.com/repos/buchgr/grpc-java/stargazers","contributors_url":"https://api.github.com/repos/buchgr/grpc-java/contributors","subscribers_url":"https://api.github.com/repos/buchgr/grpc-java/subscribers","subscription_url":"https://api.github.com/repos/buchgr/grpc-java/subscription","commits_url":"https://api.github.com/repos/buchgr/grpc-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/buchgr/grpc-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/buchgr/grpc-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/buchgr/grpc-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/buchgr/grpc-java/contents/{+path}","compare_url":"https://api.github.com/repos/buchgr/grpc-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/buchgr/grpc-java/merges","archive_url":"https://api.github.com/repos/buchgr/grpc-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/buchgr/grpc-java/downloads","issues_url":"https://api.github.com/repos/buchgr/grpc-java/issues{/number}","pulls_url":"https://api.github.com/repos/buchgr/grpc-java/pulls{/number}","milestones_url":"https://api.github.com/repos/buchgr/grpc-java/milestones{/number}","notifications_url":"https://api.github.com/repos/buchgr/grpc-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/buchgr/grpc-java/labels{/name}","releases_url":"https://api.github.com/repos/buchgr/grpc-java/releases{/id}","deployments_url":"https://api.github.com/repos/buchgr/grpc-java/deployments","created_at":"2015-02-26T18:18:24Z","updated_at":"2015-03-11T21:25:43Z","pushed_at":"2016-07-01T14:19:56Z","git_url":"git://github.com/buchgr/grpc-java.git","ssh_url":"git@github.com:buchgr/grpc-java.git","clone_url":"https://github.com/buchgr/grpc-java.git","svn_url":"https://github.com/buchgr/grpc-java","homepage":"","size":7343,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":false,"has_wiki":false,"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":"grpc:master","ref":"master","sha":"c5733742cea6418bd712646a8fc7f35bb1e2f953","user":{"login":"grpc","id":7802525,"avatar_url":"https://avatars.githubusercontent.com/u/7802525?v=3","gravatar_id":"","url":"https://api.github.com/users/grpc","html_url":"https://github.com/grpc","followers_url":"https://api.github.com/users/grpc/followers","following_url":"https://api.github.com/users/grpc/following{/other_user}","gists_url":"https://api.github.com/users/grpc/gists{/gist_id}","starred_url":"https://api.github.com/users/grpc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grpc/subscriptions","organizations_url":"https://api.github.com/users/grpc/orgs","repos_url":"https://api.github.com/users/grpc/repos","events_url":"https://api.github.com/users/grpc/events{/privacy}","received_events_url":"https://api.github.com/users/grpc/received_events","type":"Organization","site_admin":false},"repo":{"id":27729926,"name":"grpc-java","full_name":"grpc/grpc-java","owner":{"login":"grpc","id":7802525,"avatar_url":"https://avatars.githubusercontent.com/u/7802525?v=3","gravatar_id":"","url":"https://api.github.com/users/grpc","html_url":"https://github.com/grpc","followers_url":"https://api.github.com/users/grpc/followers","following_url":"https://api.github.com/users/grpc/following{/other_user}","gists_url":"https://api.github.com/users/grpc/gists{/gist_id}","starred_url":"https://api.github.com/users/grpc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grpc/subscriptions","organizations_url":"https://api.github.com/users/grpc/orgs","repos_url":"https://api.github.com/users/grpc/repos","events_url":"https://api.github.com/users/grpc/events{/privacy}","received_events_url":"https://api.github.com/users/grpc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/grpc/grpc-java","description":"The Java gRPC implementation. HTTP/2 based RPC","fork":false,"url":"https://api.github.com/repos/grpc/grpc-java","forks_url":"https://api.github.com/repos/grpc/grpc-java/forks","keys_url":"https://api.github.com/repos/grpc/grpc-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/grpc/grpc-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/grpc/grpc-java/teams","hooks_url":"https://api.github.com/repos/grpc/grpc-java/hooks","issue_events_url":"https://api.github.com/repos/grpc/grpc-java/issues/events{/number}","events_url":"https://api.github.com/repos/grpc/grpc-java/events","assignees_url":"https://api.github.com/repos/grpc/grpc-java/assignees{/user}","branches_url":"https://api.github.com/repos/grpc/grpc-java/branches{/branch}","tags_url":"https://api.github.com/repos/grpc/grpc-java/tags","blobs_url":"https://api.github.com/repos/grpc/grpc-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/grpc/grpc-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/grpc/grpc-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/grpc/grpc-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/grpc/grpc-java/statuses/{sha}","languages_url":"https://api.github.com/repos/grpc/grpc-java/languages","stargazers_url":"https://api.github.com/repos/grpc/grpc-java/stargazers","contributors_url":"https://api.github.com/repos/grpc/grpc-java/contributors","subscribers_url":"https://api.github.com/repos/grpc/grpc-java/subscribers","subscription_url":"https://api.github.com/repos/grpc/grpc-java/subscription","commits_url":"https://api.github.com/repos/grpc/grpc-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/grpc/grpc-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/grpc/grpc-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/grpc/grpc-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/grpc/grpc-java/contents/{+path}","compare_url":"https://api.github.com/repos/grpc/grpc-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/grpc/grpc-java/merges","archive_url":"https://api.github.com/repos/grpc/grpc-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/grpc/grpc-java/downloads","issues_url":"https://api.github.com/repos/grpc/grpc-java/issues{/number}","pulls_url":"https://api.github.com/repos/grpc/grpc-java/pulls{/number}","milestones_url":"https://api.github.com/repos/grpc/grpc-java/milestones{/number}","notifications_url":"https://api.github.com/repos/grpc/grpc-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/grpc/grpc-java/labels{/name}","releases_url":"https://api.github.com/repos/grpc/grpc-java/releases{/id}","deployments_url":"https://api.github.com/repos/grpc/grpc-java/deployments","created_at":"2014-12-08T19:00:00Z","updated_at":"2016-07-01T10:53:43Z","pushed_at":"2016-07-01T14:20:48Z","git_url":"git://github.com/grpc/grpc-java.git","ssh_url":"git@github.com:grpc/grpc-java.git","clone_url":"https://github.com/grpc/grpc-java.git","svn_url":"https://github.com/grpc/grpc-java","homepage":"http://grpc.io","size":24018,"stargazers_count":1453,"watchers_count":1453,"language":"Java","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":true,"forks_count":388,"mirror_url":null,"open_issues_count":183,"forks":388,"open_issues":183,"watchers":1453,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1996"},"html":{"href":"https://github.com/grpc/grpc-java/pull/1996"},"issue":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/1996"},"comments":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/1996/comments"},"review_comments":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1996/comments"},"review_comment":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1996/commits"},"statuses":{"href":"https://api.github.com/repos/grpc/grpc-java/statuses/368a571a5ac46e1766c6acf55832fb327a232f75"}}}}
{"actor":{"display_login":"nmittler"}}