{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/coreclr/pulls/comments/37767272","id":37767272,"diff_hunk":"@@ -1358,6 +1358,24 @@ private void FlushOSBuffer()\n             }\n         }\n \n+\n+        // This method allows the user to specify an external buffer, which can then be pooled or shared\n+        // among mulitple instances of FileStream. Note that this buffer can _only_ be used by a single\n+        // instance of FileStream at a time. \n+        // This should be called before any action is done on the FileStream, otherwise a buffer will \n+        // be allocation, and we'll miss this optimization\n+        [System.Security.SecuritySafeCritical]  // auto-generated\n+        public override void SetBuffer(byte[] externalBuffer)","path":"src/mscorlib/src/System/IO/FileStream.cs","position":11,"original_position":11,"commit_id":"aaff9e0e77ed02d9cae699f106f136f49c7178cc","original_commit_id":"aaff9e0e77ed02d9cae699f106f136f49c7178cc","user":{"login":"akoeplinger","id":1376924,"avatar_url":"https://avatars.githubusercontent.com/u/1376924?v=3","gravatar_id":"","url":"https://api.github.com/users/akoeplinger","html_url":"https://github.com/akoeplinger","followers_url":"https://api.github.com/users/akoeplinger/followers","following_url":"https://api.github.com/users/akoeplinger/following{/other_user}","gists_url":"https://api.github.com/users/akoeplinger/gists{/gist_id}","starred_url":"https://api.github.com/users/akoeplinger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akoeplinger/subscriptions","organizations_url":"https://api.github.com/users/akoeplinger/orgs","repos_url":"https://api.github.com/users/akoeplinger/repos","events_url":"https://api.github.com/users/akoeplinger/events{/privacy}","received_events_url":"https://api.github.com/users/akoeplinger/received_events","type":"User","site_admin":false},"body":"In your example, the method is called `SetExternalBuffer`","created_at":"2015-08-24T15:57:10Z","updated_at":"2015-08-24T15:57:10Z","html_url":"https://github.com/dotnet/coreclr/pull/1429#discussion_r37767272","pull_request_url":"https://api.github.com/repos/dotnet/coreclr/pulls/1429","_links":{"self":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/comments/37767272"},"html":{"href":"https://github.com/dotnet/coreclr/pull/1429#discussion_r37767272"},"pull_request":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/1429"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/coreclr/pulls/1429","id":43189580,"html_url":"https://github.com/dotnet/coreclr/pull/1429","diff_url":"https://github.com/dotnet/coreclr/pull/1429.diff","patch_url":"https://github.com/dotnet/coreclr/pull/1429.patch","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/1429","number":1429,"state":"open","locked":false,"title":"Allow to supply an external buffer to FileStream","user":{"login":"ayende","id":116915,"avatar_url":"https://avatars.githubusercontent.com/u/116915?v=3","gravatar_id":"","url":"https://api.github.com/users/ayende","html_url":"https://github.com/ayende","followers_url":"https://api.github.com/users/ayende/followers","following_url":"https://api.github.com/users/ayende/following{/other_user}","gists_url":"https://api.github.com/users/ayende/gists{/gist_id}","starred_url":"https://api.github.com/users/ayende/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ayende/subscriptions","organizations_url":"https://api.github.com/users/ayende/orgs","repos_url":"https://api.github.com/users/ayende/repos","events_url":"https://api.github.com/users/ayende/events{/privacy}","received_events_url":"https://api.github.com/users/ayende/received_events","type":"User","site_admin":false},"body":"The idea is to avoid 4KB allocation for buffer whenever we need to work with large number of files.\r\nConsider the following code:\r\n\r\n\tforeach (var file in System.IO.Directory.GetFiles(dirToCheck, \"*.dat\"))\r\n\t{\r\n\t    using (var fs = new FileStream(file, FileMode.Open))\r\n\t    {\r\n                 // do something to read from the stream\r\n\t    }\r\n\t}\r\n\r\nThe problem is that each instance of FileStream will allocate an independent buffer. If we are reading 10,000 files, that will result in 40MB(!) being allocated, even if we are very careful about allocations in general.\r\n\r\nThis PR adds a method, SetExternalBuffer(byte[]) which allows the caller to send an external buffer, which they can manage on their own, to the FileStream.\r\n\r\nThis gives callers the chance to pool the buffer or share it between multiple FileStream instances (obviously with only one stream using the buffer at at time.\r\n\r\nSample usage:\r\n\r\n\tbyte[] buffer = new byte[4096];\r\n\tforeach (var file in System.IO.Directory.GetFiles(dirToCheck, \"*.dat\"))\r\n\t{\r\n\t    using (var fs = new FileStream(file, FileMode.Open))\r\n\t    {\r\n                 fs.SetExternalBuffer(buffer);\r\n                 // do something to read from the stream\r\n\t    }\r\n\t}\r\n\r\nAnd now we have no more allocations. The cost of calling this code went from 40MB to 4KB.","created_at":"2015-08-24T15:48:07Z","updated_at":"2015-08-24T15:57:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f06c5afa707b0e3217f5a589d4f348a72571076b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/coreclr/pulls/1429/commits","review_comments_url":"https://api.github.com/repos/dotnet/coreclr/pulls/1429/comments","review_comment_url":"https://api.github.com/repos/dotnet/coreclr/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/1429/comments","statuses_url":"https://api.github.com/repos/dotnet/coreclr/statuses/aaff9e0e77ed02d9cae699f106f136f49c7178cc","head":{"label":"ayende:master","ref":"master","sha":"aaff9e0e77ed02d9cae699f106f136f49c7178cc","user":{"login":"ayende","id":116915,"avatar_url":"https://avatars.githubusercontent.com/u/116915?v=3","gravatar_id":"","url":"https://api.github.com/users/ayende","html_url":"https://github.com/ayende","followers_url":"https://api.github.com/users/ayende/followers","following_url":"https://api.github.com/users/ayende/following{/other_user}","gists_url":"https://api.github.com/users/ayende/gists{/gist_id}","starred_url":"https://api.github.com/users/ayende/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ayende/subscriptions","organizations_url":"https://api.github.com/users/ayende/orgs","repos_url":"https://api.github.com/users/ayende/repos","events_url":"https://api.github.com/users/ayende/events{/privacy}","received_events_url":"https://api.github.com/users/ayende/received_events","type":"User","site_admin":false},"repo":{"id":41309100,"name":"coreclr","full_name":"ayende/coreclr","owner":{"login":"ayende","id":116915,"avatar_url":"https://avatars.githubusercontent.com/u/116915?v=3","gravatar_id":"","url":"https://api.github.com/users/ayende","html_url":"https://github.com/ayende","followers_url":"https://api.github.com/users/ayende/followers","following_url":"https://api.github.com/users/ayende/following{/other_user}","gists_url":"https://api.github.com/users/ayende/gists{/gist_id}","starred_url":"https://api.github.com/users/ayende/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ayende/subscriptions","organizations_url":"https://api.github.com/users/ayende/orgs","repos_url":"https://api.github.com/users/ayende/repos","events_url":"https://api.github.com/users/ayende/events{/privacy}","received_events_url":"https://api.github.com/users/ayende/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ayende/coreclr","description":"This repo contains the .NET Core runtime, called CoreCLR, and the base library, called mscorlib. It includes the garbage collector, JIT compiler, base .NET data types and many low-level classes.","fork":true,"url":"https://api.github.com/repos/ayende/coreclr","forks_url":"https://api.github.com/repos/ayende/coreclr/forks","keys_url":"https://api.github.com/repos/ayende/coreclr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ayende/coreclr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ayende/coreclr/teams","hooks_url":"https://api.github.com/repos/ayende/coreclr/hooks","issue_events_url":"https://api.github.com/repos/ayende/coreclr/issues/events{/number}","events_url":"https://api.github.com/repos/ayende/coreclr/events","assignees_url":"https://api.github.com/repos/ayende/coreclr/assignees{/user}","branches_url":"https://api.github.com/repos/ayende/coreclr/branches{/branch}","tags_url":"https://api.github.com/repos/ayende/coreclr/tags","blobs_url":"https://api.github.com/repos/ayende/coreclr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ayende/coreclr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ayende/coreclr/git/refs{/sha}","trees_url":"https://api.github.com/repos/ayende/coreclr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ayende/coreclr/statuses/{sha}","languages_url":"https://api.github.com/repos/ayende/coreclr/languages","stargazers_url":"https://api.github.com/repos/ayende/coreclr/stargazers","contributors_url":"https://api.github.com/repos/ayende/coreclr/contributors","subscribers_url":"https://api.github.com/repos/ayende/coreclr/subscribers","subscription_url":"https://api.github.com/repos/ayende/coreclr/subscription","commits_url":"https://api.github.com/repos/ayende/coreclr/commits{/sha}","git_commits_url":"https://api.github.com/repos/ayende/coreclr/git/commits{/sha}","comments_url":"https://api.github.com/repos/ayende/coreclr/comments{/number}","issue_comment_url":"https://api.github.com/repos/ayende/coreclr/issues/comments{/number}","contents_url":"https://api.github.com/repos/ayende/coreclr/contents/{+path}","compare_url":"https://api.github.com/repos/ayende/coreclr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ayende/coreclr/merges","archive_url":"https://api.github.com/repos/ayende/coreclr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ayende/coreclr/downloads","issues_url":"https://api.github.com/repos/ayende/coreclr/issues{/number}","pulls_url":"https://api.github.com/repos/ayende/coreclr/pulls{/number}","milestones_url":"https://api.github.com/repos/ayende/coreclr/milestones{/number}","notifications_url":"https://api.github.com/repos/ayende/coreclr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ayende/coreclr/labels{/name}","releases_url":"https://api.github.com/repos/ayende/coreclr/releases{/id}","created_at":"2015-08-24T14:51:18Z","updated_at":"2015-08-24T14:51:55Z","pushed_at":"2015-08-24T15:42:46Z","git_url":"git://github.com/ayende/coreclr.git","ssh_url":"git@github.com:ayende/coreclr.git","clone_url":"https://github.com/ayende/coreclr.git","svn_url":"https://github.com/ayende/coreclr","homepage":"http://dotnet.github.io/core/","size":52683,"stargazers_count":0,"watchers_count":0,"language":"C#","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":"dotnet:master","ref":"master","sha":"5d8b6c70296a4433745e81eaaff44238abb468bf","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":30092893,"name":"coreclr","full_name":"dotnet/coreclr","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/coreclr","description":"This repo contains the .NET Core runtime, called CoreCLR, and the base library, called mscorlib. It includes the garbage collector, JIT compiler, base .NET data types and many low-level classes.","fork":false,"url":"https://api.github.com/repos/dotnet/coreclr","forks_url":"https://api.github.com/repos/dotnet/coreclr/forks","keys_url":"https://api.github.com/repos/dotnet/coreclr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/coreclr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/coreclr/teams","hooks_url":"https://api.github.com/repos/dotnet/coreclr/hooks","issue_events_url":"https://api.github.com/repos/dotnet/coreclr/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/coreclr/events","assignees_url":"https://api.github.com/repos/dotnet/coreclr/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/coreclr/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/coreclr/tags","blobs_url":"https://api.github.com/repos/dotnet/coreclr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/coreclr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/coreclr/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/coreclr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/coreclr/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/coreclr/languages","stargazers_url":"https://api.github.com/repos/dotnet/coreclr/stargazers","contributors_url":"https://api.github.com/repos/dotnet/coreclr/contributors","subscribers_url":"https://api.github.com/repos/dotnet/coreclr/subscribers","subscription_url":"https://api.github.com/repos/dotnet/coreclr/subscription","commits_url":"https://api.github.com/repos/dotnet/coreclr/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/coreclr/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/coreclr/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/coreclr/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/coreclr/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/coreclr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/coreclr/merges","archive_url":"https://api.github.com/repos/dotnet/coreclr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/coreclr/downloads","issues_url":"https://api.github.com/repos/dotnet/coreclr/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/coreclr/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/coreclr/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/coreclr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/coreclr/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/coreclr/releases{/id}","created_at":"2015-01-30T22:10:02Z","updated_at":"2015-08-24T14:27:02Z","pushed_at":"2015-08-24T15:48:07Z","git_url":"git://github.com/dotnet/coreclr.git","ssh_url":"git@github.com:dotnet/coreclr.git","clone_url":"https://github.com/dotnet/coreclr.git","svn_url":"https://github.com/dotnet/coreclr","homepage":"http://dotnet.github.io/core/","size":52683,"stargazers_count":5073,"watchers_count":5073,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1010,"mirror_url":null,"open_issues_count":242,"forks":1010,"open_issues":242,"watchers":5073,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/1429"},"html":{"href":"https://github.com/dotnet/coreclr/pull/1429"},"issue":{"href":"https://api.github.com/repos/dotnet/coreclr/issues/1429"},"comments":{"href":"https://api.github.com/repos/dotnet/coreclr/issues/1429/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/1429/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/1429/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/coreclr/statuses/aaff9e0e77ed02d9cae699f106f136f49c7178cc"}}}}