{"action":"created","comment":{"url":"https://api.github.com/repos/docker/docker/pulls/comments/58477556","id":58477556,"diff_hunk":"@@ -107,46 +120,70 @@ func (bp *BytesPipe) Close() error {\n \treturn bp.CloseWithError(nil)\n }\n \n-func (bp *BytesPipe) len() int {\n-\treturn bp.bufLen - bp.lastRead\n-}\n-\n // Read reads bytes from BytesPipe.\n // Data could be read only once.\n func (bp *BytesPipe) Read(p []byte) (n int, err error) {\n \tbp.mu.Lock()\n-\tdefer bp.mu.Unlock()\n-\tif bp.len() == 0 {\n+\tif bp.bufLen == 0 {\n \t\tif bp.closeErr != nil {\n+\t\t\tbp.mu.Unlock()\n \t\t\treturn 0, bp.closeErr\n \t\t}\n \t\tbp.wait.Wait()\n-\t\tif bp.len() == 0 && bp.closeErr != nil {\n+\t\tif bp.bufLen == 0 && bp.closeErr != nil {\n+\t\t\tbp.mu.Unlock()\n \t\t\treturn 0, bp.closeErr\n \t\t}\n \t}\n+\n \tfor {\n-\t\tread := copy(p, bp.buf[0][bp.lastRead:])\n+\t\tb := bp.buf[0]\n+\t\tread, err := b.Read(p)\n \t\tn += read\n-\t\tbp.lastRead += read\n-\t\tif bp.len() == 0 {\n-\t\t\t// we have read everything. reset to the beginning.\n-\t\t\tbp.lastRead = 0\n-\t\t\tbp.bufLen -= len(bp.buf[0])\n-\t\t\tbp.buf[0] = bp.buf[0][:0]\n-\t\t\tbreak\n+\t\tbp.bufLen -= read\n+\n+\t\tif b.Len() == 0 {\n+\t\t\treturnBuffer(b)\n+\t\t\tbp.buf[0] = nil\n+\t\t\tbp.buf = bp.buf[1:]\n+\t\t}\n+\n+\t\tif err != nil {\n+\t\t\tbp.wait.Broadcast()\n+\t\t\tbp.mu.Unlock()\n+\t\t\treturn n, err\n \t\t}\n-\t\t// break if everything was read\n+\n \t\tif len(p) == read {\n \t\t\tbreak\n \t\t}\n-\t\t// more buffered data and more asked. read from next slice.\n+\n \t\tp = p[read:]\n-\t\tbp.lastRead = 0\n-\t\tbp.bufLen -= len(bp.buf[0])\n-\t\tbp.buf[0] = nil     // throw away old slice\n-\t\tbp.buf = bp.buf[1:] // switch to next\n+\n+\t\tif bp.bufLen == 0 {\n+\t\t\tbreak\n+\t\t}","path":"pkg/ioutils/bytespipe.go","position":181,"original_position":181,"commit_id":"cff83964820dce4b53005182d63aad8279b7c196","original_commit_id":"cff83964820dce4b53005182d63aad8279b7c196","user":{"login":"aaronlehmann","id":10601430,"avatar_url":"https://avatars.githubusercontent.com/u/10601430?v=3","gravatar_id":"","url":"https://api.github.com/users/aaronlehmann","html_url":"https://github.com/aaronlehmann","followers_url":"https://api.github.com/users/aaronlehmann/followers","following_url":"https://api.github.com/users/aaronlehmann/following{/other_user}","gists_url":"https://api.github.com/users/aaronlehmann/gists{/gist_id}","starred_url":"https://api.github.com/users/aaronlehmann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaronlehmann/subscriptions","organizations_url":"https://api.github.com/users/aaronlehmann/orgs","repos_url":"https://api.github.com/users/aaronlehmann/repos","events_url":"https://api.github.com/users/aaronlehmann/events{/privacy}","received_events_url":"https://api.github.com/users/aaronlehmann/received_events","type":"User","site_admin":false},"body":"How about making the loop `for bp.bufLen > 0`?","created_at":"2016-04-05T02:00:52Z","updated_at":"2016-04-05T02:00:52Z","html_url":"https://github.com/docker/docker/pull/21755#discussion_r58477556","pull_request_url":"https://api.github.com/repos/docker/docker/pulls/21755","_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/comments/58477556"},"html":{"href":"https://github.com/docker/docker/pull/21755#discussion_r58477556"},"pull_request":{"href":"https://api.github.com/repos/docker/docker/pulls/21755"}}},"pull_request":{"url":"https://api.github.com/repos/docker/docker/pulls/21755","id":65235951,"html_url":"https://github.com/docker/docker/pull/21755","diff_url":"https://github.com/docker/docker/pull/21755.diff","patch_url":"https://github.com/docker/docker/pull/21755.patch","issue_url":"https://api.github.com/repos/docker/docker/issues/21755","number":21755,"state":"open","locked":false,"title":"Improve performance/reduce allocs of bytespipe","user":{"login":"cpuguy83","id":799078,"avatar_url":"https://avatars.githubusercontent.com/u/799078?v=3","gravatar_id":"","url":"https://api.github.com/users/cpuguy83","html_url":"https://github.com/cpuguy83","followers_url":"https://api.github.com/users/cpuguy83/followers","following_url":"https://api.github.com/users/cpuguy83/following{/other_user}","gists_url":"https://api.github.com/users/cpuguy83/gists{/gist_id}","starred_url":"https://api.github.com/users/cpuguy83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpuguy83/subscriptions","organizations_url":"https://api.github.com/users/cpuguy83/orgs","repos_url":"https://api.github.com/users/cpuguy83/repos","events_url":"https://api.github.com/users/cpuguy83/events{/privacy}","received_events_url":"https://api.github.com/users/cpuguy83/received_events","type":"User","site_admin":false},"body":"Creates a `fixedBuffer` type that is used to encapsulate functionality\r\nfor reading/writing from the underlying byte slices.\r\n\r\nUses lazily-loaded set of sync.Pools for storing buffers that are no\r\nlonger needed so they can be re-used.\r\n\r\n```\r\nbenchmark                     old ns/op     new ns/op     delta\r\nBenchmarkBytesPipeWrite-8     138469        48985         -64.62%\r\nBenchmarkBytesPipeRead-8      130922        56601         -56.77%\r\n\r\nbenchmark                     old allocs     new allocs     delta\r\nBenchmarkBytesPipeWrite-8     18             8              -55.56%\r\nBenchmarkBytesPipeRead-8      0              0              +0.00%\r\n\r\nbenchmark                     old bytes     new bytes     delta\r\nBenchmarkBytesPipeWrite-8     66903         1649          -97.54%\r\nBenchmarkBytesPipeRead-8      0             1             +Inf%\r\n```\r\n![](http://animals.sandiegozoo.org/sites/default/files/juicebox_slides/cheetah_02.jpg)\r\n\r\nSigned-off-by: Brian Goff <cpuguy83@gmail.com>","created_at":"2016-04-05T00:52:31Z","updated_at":"2016-04-05T02:00:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"dce6b6dbac0bcfbcdc64fdb2864622dac39f4395","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/docker/docker/pulls/21755/commits","review_comments_url":"https://api.github.com/repos/docker/docker/pulls/21755/comments","review_comment_url":"https://api.github.com/repos/docker/docker/pulls/comments{/number}","comments_url":"https://api.github.com/repos/docker/docker/issues/21755/comments","statuses_url":"https://api.github.com/repos/docker/docker/statuses/cff83964820dce4b53005182d63aad8279b7c196","head":{"label":"cpuguy83:bytespipe_allocs","ref":"bytespipe_allocs","sha":"cff83964820dce4b53005182d63aad8279b7c196","user":{"login":"cpuguy83","id":799078,"avatar_url":"https://avatars.githubusercontent.com/u/799078?v=3","gravatar_id":"","url":"https://api.github.com/users/cpuguy83","html_url":"https://github.com/cpuguy83","followers_url":"https://api.github.com/users/cpuguy83/followers","following_url":"https://api.github.com/users/cpuguy83/following{/other_user}","gists_url":"https://api.github.com/users/cpuguy83/gists{/gist_id}","starred_url":"https://api.github.com/users/cpuguy83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpuguy83/subscriptions","organizations_url":"https://api.github.com/users/cpuguy83/orgs","repos_url":"https://api.github.com/users/cpuguy83/repos","events_url":"https://api.github.com/users/cpuguy83/events{/privacy}","received_events_url":"https://api.github.com/users/cpuguy83/received_events","type":"User","site_admin":false},"repo":{"id":15300899,"name":"docker","full_name":"cpuguy83/docker","owner":{"login":"cpuguy83","id":799078,"avatar_url":"https://avatars.githubusercontent.com/u/799078?v=3","gravatar_id":"","url":"https://api.github.com/users/cpuguy83","html_url":"https://github.com/cpuguy83","followers_url":"https://api.github.com/users/cpuguy83/followers","following_url":"https://api.github.com/users/cpuguy83/following{/other_user}","gists_url":"https://api.github.com/users/cpuguy83/gists{/gist_id}","starred_url":"https://api.github.com/users/cpuguy83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpuguy83/subscriptions","organizations_url":"https://api.github.com/users/cpuguy83/orgs","repos_url":"https://api.github.com/users/cpuguy83/repos","events_url":"https://api.github.com/users/cpuguy83/events{/privacy}","received_events_url":"https://api.github.com/users/cpuguy83/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cpuguy83/docker","description":"Docker - the open-source application container engine","fork":true,"url":"https://api.github.com/repos/cpuguy83/docker","forks_url":"https://api.github.com/repos/cpuguy83/docker/forks","keys_url":"https://api.github.com/repos/cpuguy83/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cpuguy83/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cpuguy83/docker/teams","hooks_url":"https://api.github.com/repos/cpuguy83/docker/hooks","issue_events_url":"https://api.github.com/repos/cpuguy83/docker/issues/events{/number}","events_url":"https://api.github.com/repos/cpuguy83/docker/events","assignees_url":"https://api.github.com/repos/cpuguy83/docker/assignees{/user}","branches_url":"https://api.github.com/repos/cpuguy83/docker/branches{/branch}","tags_url":"https://api.github.com/repos/cpuguy83/docker/tags","blobs_url":"https://api.github.com/repos/cpuguy83/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cpuguy83/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cpuguy83/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/cpuguy83/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cpuguy83/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/cpuguy83/docker/languages","stargazers_url":"https://api.github.com/repos/cpuguy83/docker/stargazers","contributors_url":"https://api.github.com/repos/cpuguy83/docker/contributors","subscribers_url":"https://api.github.com/repos/cpuguy83/docker/subscribers","subscription_url":"https://api.github.com/repos/cpuguy83/docker/subscription","commits_url":"https://api.github.com/repos/cpuguy83/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/cpuguy83/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/cpuguy83/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/cpuguy83/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/cpuguy83/docker/contents/{+path}","compare_url":"https://api.github.com/repos/cpuguy83/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cpuguy83/docker/merges","archive_url":"https://api.github.com/repos/cpuguy83/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cpuguy83/docker/downloads","issues_url":"https://api.github.com/repos/cpuguy83/docker/issues{/number}","pulls_url":"https://api.github.com/repos/cpuguy83/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/cpuguy83/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/cpuguy83/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cpuguy83/docker/labels{/name}","releases_url":"https://api.github.com/repos/cpuguy83/docker/releases{/id}","deployments_url":"https://api.github.com/repos/cpuguy83/docker/deployments","created_at":"2013-12-19T02:58:40Z","updated_at":"2015-04-27T13:11:12Z","pushed_at":"2016-04-05T01:13:10Z","git_url":"git://github.com/cpuguy83/docker.git","ssh_url":"git@github.com:cpuguy83/docker.git","clone_url":"https://github.com/cpuguy83/docker.git","svn_url":"https://github.com/cpuguy83/docker","homepage":"http://www.docker.io","size":87121,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":0,"forks":2,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"docker:master","ref":"master","sha":"d20933c46159d93ee532ebb229d990fa20b1009c","user":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"repo":{"id":7691631,"name":"docker","full_name":"docker/docker","owner":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/docker/docker","description":"Docker - the open-source application container engine","fork":false,"url":"https://api.github.com/repos/docker/docker","forks_url":"https://api.github.com/repos/docker/docker/forks","keys_url":"https://api.github.com/repos/docker/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/docker/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/docker/docker/teams","hooks_url":"https://api.github.com/repos/docker/docker/hooks","issue_events_url":"https://api.github.com/repos/docker/docker/issues/events{/number}","events_url":"https://api.github.com/repos/docker/docker/events","assignees_url":"https://api.github.com/repos/docker/docker/assignees{/user}","branches_url":"https://api.github.com/repos/docker/docker/branches{/branch}","tags_url":"https://api.github.com/repos/docker/docker/tags","blobs_url":"https://api.github.com/repos/docker/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/docker/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/docker/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/docker/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/docker/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/docker/docker/languages","stargazers_url":"https://api.github.com/repos/docker/docker/stargazers","contributors_url":"https://api.github.com/repos/docker/docker/contributors","subscribers_url":"https://api.github.com/repos/docker/docker/subscribers","subscription_url":"https://api.github.com/repos/docker/docker/subscription","commits_url":"https://api.github.com/repos/docker/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/docker/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/docker/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/docker/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/docker/docker/contents/{+path}","compare_url":"https://api.github.com/repos/docker/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/docker/docker/merges","archive_url":"https://api.github.com/repos/docker/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/docker/docker/downloads","issues_url":"https://api.github.com/repos/docker/docker/issues{/number}","pulls_url":"https://api.github.com/repos/docker/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/docker/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/docker/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/docker/docker/labels{/name}","releases_url":"https://api.github.com/repos/docker/docker/releases{/id}","deployments_url":"https://api.github.com/repos/docker/docker/deployments","created_at":"2013-01-18T18:10:57Z","updated_at":"2016-04-05T01:46:41Z","pushed_at":"2016-04-05T01:56:54Z","git_url":"git://github.com/docker/docker.git","ssh_url":"git@github.com:docker/docker.git","clone_url":"https://github.com/docker/docker.git","svn_url":"https://github.com/docker/docker","homepage":"http://www.docker.com","size":144789,"stargazers_count":30116,"watchers_count":30116,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":8505,"mirror_url":null,"open_issues_count":1448,"forks":8505,"open_issues":1448,"watchers":30116,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/21755"},"html":{"href":"https://github.com/docker/docker/pull/21755"},"issue":{"href":"https://api.github.com/repos/docker/docker/issues/21755"},"comments":{"href":"https://api.github.com/repos/docker/docker/issues/21755/comments"},"review_comments":{"href":"https://api.github.com/repos/docker/docker/pulls/21755/comments"},"review_comment":{"href":"https://api.github.com/repos/docker/docker/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/docker/docker/pulls/21755/commits"},"statuses":{"href":"https://api.github.com/repos/docker/docker/statuses/cff83964820dce4b53005182d63aad8279b7c196"}}}}