{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/coreclr/pulls/comments/144971603","pull_request_review_id":69703390,"id":144971603,"diff_hunk":"@@ -536,23 +537,33 @@ private static void ValidateTarget(EnvironmentVariableTarget target)\n \n         private static string GetEnvironmentVariableCore(string variable)\n         {\n-            StringBuilder sb = StringBuilderCache.Acquire(128); // A somewhat reasonable default size\n-            int requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity);\n+            Span<char> buffer = stackalloc char[128]; // A somewhat reasonable default size\n+            return GetEnvironmentVariableCoreHelper(variable, buffer);\n+        }\n+\n+        private static string GetEnvironmentVariableCoreHelper(string variable, Span<char> buffer)\n+        {\n+            int requiredSize = Win32Native.GetEnvironmentVariable(variable, buffer);\n \n-            if (requiredSize == 0 && Marshal.GetLastWin32Error() == Win32Native.ERROR_ENVVAR_NOT_FOUND)\n+            if (requiredSize > buffer.Length)\n             {\n-                StringBuilderCache.Release(sb);\n-                return null;\n+                char[] chars = ArrayPool<char>.Shared.Rent(requiredSize);\n+                try\n+                {\n+                    return GetEnvironmentVariableCoreHelper(variable, chars);\n+                }\n+                finally\n+                {\n+                    ArrayPool<char>.Shared.Return(chars, clearArray: true);\n+                }\n             }\n \n-            while (requiredSize > sb.Capacity)\n+            if (requiredSize == 0)\n             {\n-                sb.Capacity = requiredSize;\n-                sb.Length = 0;\n-                requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity);\n+                return Marshal.GetLastWin32Error() == Win32Native.ERROR_ENVVAR_NOT_FOUND ? null : string.Empty;","path":"src/mscorlib/src/System/Environment.cs","position":null,"original_position":44,"commit_id":"a8ba2f4c3d195d7e15f586aaf78629a182398e24","original_commit_id":"630f362218c723adc8426642b5e644879a91788b","user":{"login":"justinvp","id":710598,"avatar_url":"https://avatars3.githubusercontent.com/u/710598?v=4","gravatar_id":"","url":"https://api.github.com/users/justinvp","html_url":"https://github.com/justinvp","followers_url":"https://api.github.com/users/justinvp/followers","following_url":"https://api.github.com/users/justinvp/following{/other_user}","gists_url":"https://api.github.com/users/justinvp/gists{/gist_id}","starred_url":"https://api.github.com/users/justinvp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinvp/subscriptions","organizations_url":"https://api.github.com/users/justinvp/orgs","repos_url":"https://api.github.com/users/justinvp/repos","events_url":"https://api.github.com/users/justinvp/events{/privacy}","received_events_url":"https://api.github.com/users/justinvp/received_events","type":"User","site_admin":false},"body":"Done. I also changed the condition back to the way it was as the string ctor will take care of returning `string.Empty` for empty spans (when `requiredSize == 0` and the error isn’t “not found”).","created_at":"2017-10-16T21:19:07Z","updated_at":"2017-10-16T21:19:07Z","html_url":"https://github.com/dotnet/coreclr/pull/14502#discussion_r144971603","pull_request_url":"https://api.github.com/repos/dotnet/coreclr/pulls/14502","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/comments/144971603"},"html":{"href":"https://github.com/dotnet/coreclr/pull/14502#discussion_r144971603"},"pull_request":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/14502"}},"in_reply_to_id":144904151},"pull_request":{"url":"https://api.github.com/repos/dotnet/coreclr/pulls/14502","id":146577757,"html_url":"https://github.com/dotnet/coreclr/pull/14502","diff_url":"https://github.com/dotnet/coreclr/pull/14502.diff","patch_url":"https://github.com/dotnet/coreclr/pull/14502.patch","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/14502","number":14502,"state":"open","locked":false,"title":"GetEnvironmentVariable: Avoid StringBuilder marshaling overhead","user":{"login":"justinvp","id":710598,"avatar_url":"https://avatars3.githubusercontent.com/u/710598?v=4","gravatar_id":"","url":"https://api.github.com/users/justinvp","html_url":"https://github.com/justinvp","followers_url":"https://api.github.com/users/justinvp/followers","following_url":"https://api.github.com/users/justinvp/following{/other_user}","gists_url":"https://api.github.com/users/justinvp/gists{/gist_id}","starred_url":"https://api.github.com/users/justinvp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinvp/subscriptions","organizations_url":"https://api.github.com/users/justinvp/orgs","repos_url":"https://api.github.com/users/justinvp/repos","events_url":"https://api.github.com/users/justinvp/events{/privacy}","received_events_url":"https://api.github.com/users/justinvp/received_events","type":"User","site_admin":false},"body":"Try a stack allocated buffer first, then fallback to using the shared array pool.\r\n\r\nResults on my machine:\r\n\r\n\r\n|             Method |      Mean |     Error |    StdDev |  Gen 0 | Allocated |\r\n|------------------- |----------:|----------:|----------:|-------:|----------:|\r\n|  `\"TMP\"` Before | 156.51 ns | 2.0132 ns | 1.8831 ns | 0.0875 |     368 B |\r\n|  `\"TMP\"` After |  92.88 ns | 0.5002 ns | 0.4434 ns | 0.0209 |      88 B |\r\n| `\"PATH\"` Before | 621.59 ns | 2.2668 ns | 2.1204 ns | 0.9565 |    4016 B |\r\n| `\"PATH\"` After | 287.02 ns | 1.0377 ns | 0.8666 ns | 0.2704 |    1136 B |\r\n\r\n\r\n`\"TMP\"` is under the stack limit and `\"PATH\"` is over the limit (uses the pool).","created_at":"2017-10-14T00:30:37Z","updated_at":"2017-10-16T21:19:07Z","closed_at":null,"merged_at":null,"merge_commit_sha":"303a5fff571a933a41aad155cd143246e74b9987","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/coreclr/pulls/14502/commits","review_comments_url":"https://api.github.com/repos/dotnet/coreclr/pulls/14502/comments","review_comment_url":"https://api.github.com/repos/dotnet/coreclr/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/14502/comments","statuses_url":"https://api.github.com/repos/dotnet/coreclr/statuses/a8ba2f4c3d195d7e15f586aaf78629a182398e24","head":{"label":"justinvp:getenvironmentvariable","ref":"getenvironmentvariable","sha":"a8ba2f4c3d195d7e15f586aaf78629a182398e24","user":{"login":"justinvp","id":710598,"avatar_url":"https://avatars3.githubusercontent.com/u/710598?v=4","gravatar_id":"","url":"https://api.github.com/users/justinvp","html_url":"https://github.com/justinvp","followers_url":"https://api.github.com/users/justinvp/followers","following_url":"https://api.github.com/users/justinvp/following{/other_user}","gists_url":"https://api.github.com/users/justinvp/gists{/gist_id}","starred_url":"https://api.github.com/users/justinvp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinvp/subscriptions","organizations_url":"https://api.github.com/users/justinvp/orgs","repos_url":"https://api.github.com/users/justinvp/repos","events_url":"https://api.github.com/users/justinvp/events{/privacy}","received_events_url":"https://api.github.com/users/justinvp/received_events","type":"User","site_admin":false},"repo":{"id":30260188,"name":"coreclr","full_name":"justinvp/coreclr","owner":{"login":"justinvp","id":710598,"avatar_url":"https://avatars3.githubusercontent.com/u/710598?v=4","gravatar_id":"","url":"https://api.github.com/users/justinvp","html_url":"https://github.com/justinvp","followers_url":"https://api.github.com/users/justinvp/followers","following_url":"https://api.github.com/users/justinvp/following{/other_user}","gists_url":"https://api.github.com/users/justinvp/gists{/gist_id}","starred_url":"https://api.github.com/users/justinvp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/justinvp/subscriptions","organizations_url":"https://api.github.com/users/justinvp/orgs","repos_url":"https://api.github.com/users/justinvp/repos","events_url":"https://api.github.com/users/justinvp/events{/privacy}","received_events_url":"https://api.github.com/users/justinvp/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/justinvp/coreclr","description":"This repo contains the .NET Core runtime, called CoreCLR. It is an execution engine for .NET apps, performing functions such as IL byte code loading, compilation to machine code and garbage collection. The repo contains the complete up-to-date CoreCLR codebase.","fork":true,"url":"https://api.github.com/repos/justinvp/coreclr","forks_url":"https://api.github.com/repos/justinvp/coreclr/forks","keys_url":"https://api.github.com/repos/justinvp/coreclr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/justinvp/coreclr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/justinvp/coreclr/teams","hooks_url":"https://api.github.com/repos/justinvp/coreclr/hooks","issue_events_url":"https://api.github.com/repos/justinvp/coreclr/issues/events{/number}","events_url":"https://api.github.com/repos/justinvp/coreclr/events","assignees_url":"https://api.github.com/repos/justinvp/coreclr/assignees{/user}","branches_url":"https://api.github.com/repos/justinvp/coreclr/branches{/branch}","tags_url":"https://api.github.com/repos/justinvp/coreclr/tags","blobs_url":"https://api.github.com/repos/justinvp/coreclr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/justinvp/coreclr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/justinvp/coreclr/git/refs{/sha}","trees_url":"https://api.github.com/repos/justinvp/coreclr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/justinvp/coreclr/statuses/{sha}","languages_url":"https://api.github.com/repos/justinvp/coreclr/languages","stargazers_url":"https://api.github.com/repos/justinvp/coreclr/stargazers","contributors_url":"https://api.github.com/repos/justinvp/coreclr/contributors","subscribers_url":"https://api.github.com/repos/justinvp/coreclr/subscribers","subscription_url":"https://api.github.com/repos/justinvp/coreclr/subscription","commits_url":"https://api.github.com/repos/justinvp/coreclr/commits{/sha}","git_commits_url":"https://api.github.com/repos/justinvp/coreclr/git/commits{/sha}","comments_url":"https://api.github.com/repos/justinvp/coreclr/comments{/number}","issue_comment_url":"https://api.github.com/repos/justinvp/coreclr/issues/comments{/number}","contents_url":"https://api.github.com/repos/justinvp/coreclr/contents/{+path}","compare_url":"https://api.github.com/repos/justinvp/coreclr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/justinvp/coreclr/merges","archive_url":"https://api.github.com/repos/justinvp/coreclr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/justinvp/coreclr/downloads","issues_url":"https://api.github.com/repos/justinvp/coreclr/issues{/number}","pulls_url":"https://api.github.com/repos/justinvp/coreclr/pulls{/number}","milestones_url":"https://api.github.com/repos/justinvp/coreclr/milestones{/number}","notifications_url":"https://api.github.com/repos/justinvp/coreclr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/justinvp/coreclr/labels{/name}","releases_url":"https://api.github.com/repos/justinvp/coreclr/releases{/id}","deployments_url":"https://api.github.com/repos/justinvp/coreclr/deployments","created_at":"2015-02-03T19:23:40Z","updated_at":"2016-01-11T17:50:22Z","pushed_at":"2017-10-16T18:42:21Z","git_url":"git://github.com/justinvp/coreclr.git","ssh_url":"git@github.com:justinvp/coreclr.git","clone_url":"https://github.com/justinvp/coreclr.git","svn_url":"https://github.com/justinvp/coreclr","homepage":"","size":183692,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_projects":true,"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":"a78940216170cdf5f9b97ccbf6edaca2ac0758c9","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars0.githubusercontent.com/u/9141961?v=4","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://avatars0.githubusercontent.com/u/9141961?v=4","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 System.Private.Corelib (or mscorlib). It includes the garbage collector, JIT compiler, base .NET data types and many low-level classes. We welcome contributions.","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}","deployments_url":"https://api.github.com/repos/dotnet/coreclr/deployments","created_at":"2015-01-30T22:10:02Z","updated_at":"2017-10-16T21:07:03Z","pushed_at":"2017-10-16T21:13:49Z","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":"https://github.com/dotnet/core","size":185722,"stargazers_count":8475,"watchers_count":8475,"language":"C#","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2130,"mirror_url":null,"open_issues_count":1489,"forks":2130,"open_issues":1489,"watchers":8475,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/14502"},"html":{"href":"https://github.com/dotnet/coreclr/pull/14502"},"issue":{"href":"https://api.github.com/repos/dotnet/coreclr/issues/14502"},"comments":{"href":"https://api.github.com/repos/dotnet/coreclr/issues/14502/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/14502/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/coreclr/pulls/14502/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/coreclr/statuses/a8ba2f4c3d195d7e15f586aaf78629a182398e24"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"justinvp"}}