{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/comments/59090056","id":59090056,"diff_hunk":"@@ -357,6 +358,159 @@ private static byte[] UrlEncode(byte[] bytes, int offset, int count)\n             return expandedBytes;\n         }\n \n+        private static unsafe string UrlEncode(char* s, int length)\n+        {\n+            int numUnsafe = 0;\n+            int numSpaces = 0;\n+            for (int i = 0; i < length; i++)\n+            {\n+                char c = s[i];\n+                if (c == ' ')\n+                {\n+                    numSpaces++;\n+                }\n+                else if (!IsUrlSafeChar(s[i]))\n+                {\n+                    numUnsafe += GetByteCount(s, i, length);","path":"src/System.Runtime.Extensions/src/System/Net/WebUtility.cs","position":null,"original_position":63,"commit_id":"c9f2289e76b074fb1a9ae20854717f4cc9b7ec3a","original_commit_id":"b6c75f4a0768ad6ce2848a2bb57198ed568d5f2d","user":{"login":"hughbe","id":1275900,"avatar_url":"https://avatars.githubusercontent.com/u/1275900?v=3","gravatar_id":"","url":"https://api.github.com/users/hughbe","html_url":"https://github.com/hughbe","followers_url":"https://api.github.com/users/hughbe/followers","following_url":"https://api.github.com/users/hughbe/following{/other_user}","gists_url":"https://api.github.com/users/hughbe/gists{/gist_id}","starred_url":"https://api.github.com/users/hughbe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hughbe/subscriptions","organizations_url":"https://api.github.com/users/hughbe/orgs","repos_url":"https://api.github.com/users/hughbe/repos","events_url":"https://api.github.com/users/hughbe/events{/privacy}","received_events_url":"https://api.github.com/users/hughbe/received_events","type":"User","site_admin":false},"body":"@jkotas I benchmarked my code vs yours for a long string - you're definitely right! Mine starts to perform worse almost exponentially. Therefore, its a good idea to keep using Encoding.UTF8 here","created_at":"2016-04-08T21:13:16Z","updated_at":"2016-04-08T21:13:16Z","html_url":"https://github.com/dotnet/corefx/pull/7546#discussion_r59090056","pull_request_url":"https://api.github.com/repos/dotnet/corefx/pulls/7546","_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments/59090056"},"html":{"href":"https://github.com/dotnet/corefx/pull/7546#discussion_r59090056"},"pull_request":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/7546"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/7546","id":65466888,"html_url":"https://github.com/dotnet/corefx/pull/7546","diff_url":"https://github.com/dotnet/corefx/pull/7546.diff","patch_url":"https://github.com/dotnet/corefx/pull/7546.patch","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/7546","number":7546,"state":"open","locked":false,"title":"Improve performance of WebUtility","user":{"login":"hughbe","id":1275900,"avatar_url":"https://avatars.githubusercontent.com/u/1275900?v=3","gravatar_id":"","url":"https://api.github.com/users/hughbe","html_url":"https://github.com/hughbe","followers_url":"https://api.github.com/users/hughbe/followers","following_url":"https://api.github.com/users/hughbe/following{/other_user}","gists_url":"https://api.github.com/users/hughbe/gists{/gist_id}","starred_url":"https://api.github.com/users/hughbe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hughbe/subscriptions","organizations_url":"https://api.github.com/users/hughbe/orgs","repos_url":"https://api.github.com/users/hughbe/repos","events_url":"https://api.github.com/users/hughbe/events{/privacy}","received_events_url":"https://api.github.com/users/hughbe/received_events","type":"User","site_admin":false},"body":"## Summary\r\nNo escaping needed:\r\n- x5 performance improvement\r\n- 0 allocations\r\n\r\nBasic escaping needed (single char):\r\n- 20% performance improvement\r\n- No change in allocations\r\n\r\nAdvanced escaping needed (surrogate pair):\r\n- 15% performance improvement\r\n- No change in allocations\r\n\r\n## Benchmark Results\r\n- No breaking change in behaviour in these validation tests\r\n- Times measured in seconds\r\n\r\n![benchmark](https://cloud.githubusercontent.com/assets/1275900/14318468/e4ba70d0-fc04-11e5-8a42-3604666dbab3.png)\r\n\r\n## Benchmark Code and !!Validation Tests!!\r\n- **To ensure there were no breaking changes** I ran the new output against the old output for every single char and surrogate pair in the UTF16 plane.\r\n```\r\nstatic unsafe void Main(string[] args)\r\n{\r\n    PerformanceTests();\r\n    VerificationTests();    \r\n    Console.ReadLine();\r\n}\r\n\r\nprivate static void PerformanceTests()\r\n{\r\n    Console.WriteLine(\"**** MEASURE NO ESCAPING ****\");\r\n    TimeAction(\"Old: \", () => Old.UrlEncode(\"abcxyz\"));\r\n    TimeAction(\"New: \", () => New.UrlEncode(\"abcxyz\"));\r\n\r\n    Console.WriteLine(\"**** MEASURE SPACES ****\");\r\n    TimeAction(\"Old: \", () => Old.UrlEncode(\"abc xyz\"));\r\n    TimeAction(\"New: \", () => New.UrlEncode(\"abc xyz\"));\r\n\r\n    Console.WriteLine(\"**** MEASURE ESCAPING 1 TIMES ****\");\r\n    TimeAction(\"Old: \", () => Old.UrlEncode(\"\\u007F\"));\r\n    TimeAction(\"New: \", () => New.UrlEncode(\"\\u007F\"));\r\n\r\n    Console.WriteLine(\"**** MEASURE ESCAPING 2 TIMES ****\");\r\n    TimeAction(\"Old: \", () => Old.UrlEncode(\"\\u07FF\"));\r\n    TimeAction(\"New: \", () => New.UrlEncode(\"\\u07FF\"));\r\n\r\n    Console.WriteLine(\"**** MEASURE ESCAPING 3 TIMES ****\");\r\n    TimeAction(\"Old: \", () => Old.UrlEncode(\"\\u1234\"));\r\n    TimeAction(\"New: \", () => New.UrlEncode(\"\\u1234\"));\r\n\r\n    Console.WriteLine(\"**** MEASURE ESCAPING SURROGATE PAIR ****\");\r\n    TimeAction(\"Old: \", () => Old.UrlEncode(\"\\uD801\\uDC37\"));\r\n    TimeAction(\"New: \", () => New.UrlEncode(\"\\uD801\\uDC37\"));\r\n}\r\n\r\npublic static void TimeAction(string prefix, Action action)\r\n{\r\n    action();\r\n    GC.Collect();\r\n\r\n    var sw = new Stopwatch();\r\n    for (int iter = 0; iter < 5; iter++)\r\n    {\r\n        int gen0 = GC.CollectionCount(0);\r\n        sw.Restart();\r\n        for (int i = 0; i < 100000000; i++)\r\n        {\r\n            action();\r\n        }\r\n        sw.Stop();\r\n        Console.WriteLine($\"{prefix}Time: {sw.Elapsed.TotalSeconds}\\tGC0: {GC.CollectionCount(0) - gen0}\");\r\n    }\r\n}\r\n\r\nprivate static void VerificationTests()\r\n{\r\n    for (int i = 0; i < char.MaxValue; i++)\r\n    {\r\n        char c = (char)i;\r\n\r\n        string oldString = Old.UrlEncode(c.ToString());\r\n        string newString = New.UrlEncode(c.ToString());\r\n        if (!oldString.Equals(newString))\r\n        {\r\n            Console.WriteLine($\"FAIL for char {i}. Expected: {oldString}. Actual: {newString}\");\r\n        }\r\n    }\r\n    for (int i = 0x10000; i < 0x10FFFF; i++)\r\n    {\r\n        string utf32 = char.ConvertFromUtf32(i);\r\n        string oldString = Old.UrlEncode(utf32);\r\n        string newString = New.UrlEncode(utf32);\r\n        if (!oldString.Equals(newString))\r\n        {\r\n            Console.WriteLine($\"FAIL for char {i}. Expected: {oldString}. Actual: {newString}\");\r\n        }\r\n    }\r\n    Console.WriteLine(\"Passes Validation Tests\");\r\n}\r\n```\r\n\r\n/cc @davidsh, @stephentoub, @jamesqo","created_at":"2016-04-06T13:35:32Z","updated_at":"2016-04-08T21:13:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c39f21fab05a201c176e8d6861489282e13f06b6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/corefx/pulls/7546/commits","review_comments_url":"https://api.github.com/repos/dotnet/corefx/pulls/7546/comments","review_comment_url":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/7546/comments","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/c9f2289e76b074fb1a9ae20854717f4cc9b7ec3a","head":{"label":"hughbe:webutility-speedboost","ref":"webutility-speedboost","sha":"c9f2289e76b074fb1a9ae20854717f4cc9b7ec3a","user":{"login":"hughbe","id":1275900,"avatar_url":"https://avatars.githubusercontent.com/u/1275900?v=3","gravatar_id":"","url":"https://api.github.com/users/hughbe","html_url":"https://github.com/hughbe","followers_url":"https://api.github.com/users/hughbe/followers","following_url":"https://api.github.com/users/hughbe/following{/other_user}","gists_url":"https://api.github.com/users/hughbe/gists{/gist_id}","starred_url":"https://api.github.com/users/hughbe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hughbe/subscriptions","organizations_url":"https://api.github.com/users/hughbe/orgs","repos_url":"https://api.github.com/users/hughbe/repos","events_url":"https://api.github.com/users/hughbe/events{/privacy}","received_events_url":"https://api.github.com/users/hughbe/received_events","type":"User","site_admin":false},"repo":{"id":47085914,"name":"corefx","full_name":"hughbe/corefx","owner":{"login":"hughbe","id":1275900,"avatar_url":"https://avatars.githubusercontent.com/u/1275900?v=3","gravatar_id":"","url":"https://api.github.com/users/hughbe","html_url":"https://github.com/hughbe","followers_url":"https://api.github.com/users/hughbe/followers","following_url":"https://api.github.com/users/hughbe/following{/other_user}","gists_url":"https://api.github.com/users/hughbe/gists{/gist_id}","starred_url":"https://api.github.com/users/hughbe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hughbe/subscriptions","organizations_url":"https://api.github.com/users/hughbe/orgs","repos_url":"https://api.github.com/users/hughbe/repos","events_url":"https://api.github.com/users/hughbe/events{/privacy}","received_events_url":"https://api.github.com/users/hughbe/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hughbe/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":true,"url":"https://api.github.com/repos/hughbe/corefx","forks_url":"https://api.github.com/repos/hughbe/corefx/forks","keys_url":"https://api.github.com/repos/hughbe/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hughbe/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hughbe/corefx/teams","hooks_url":"https://api.github.com/repos/hughbe/corefx/hooks","issue_events_url":"https://api.github.com/repos/hughbe/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/hughbe/corefx/events","assignees_url":"https://api.github.com/repos/hughbe/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/hughbe/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/hughbe/corefx/tags","blobs_url":"https://api.github.com/repos/hughbe/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hughbe/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hughbe/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/hughbe/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hughbe/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/hughbe/corefx/languages","stargazers_url":"https://api.github.com/repos/hughbe/corefx/stargazers","contributors_url":"https://api.github.com/repos/hughbe/corefx/contributors","subscribers_url":"https://api.github.com/repos/hughbe/corefx/subscribers","subscription_url":"https://api.github.com/repos/hughbe/corefx/subscription","commits_url":"https://api.github.com/repos/hughbe/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/hughbe/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/hughbe/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/hughbe/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/hughbe/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/hughbe/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hughbe/corefx/merges","archive_url":"https://api.github.com/repos/hughbe/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hughbe/corefx/downloads","issues_url":"https://api.github.com/repos/hughbe/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/hughbe/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/hughbe/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/hughbe/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hughbe/corefx/labels{/name}","releases_url":"https://api.github.com/repos/hughbe/corefx/releases{/id}","deployments_url":"https://api.github.com/repos/hughbe/corefx/deployments","created_at":"2015-11-30T00:15:59Z","updated_at":"2016-01-17T12:57:11Z","pushed_at":"2016-04-08T21:06:50Z","git_url":"git://github.com/hughbe/corefx.git","ssh_url":"git@github.com:hughbe/corefx.git","clone_url":"https://github.com/hughbe/corefx.git","svn_url":"https://github.com/hughbe/corefx","homepage":"http://dotnet.github.io/","size":63545,"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":"065404b13480783cf28becf5f7cc56bd35fd59d1","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":26295345,"name":"corefx","full_name":"dotnet/corefx","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/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":false,"url":"https://api.github.com/repos/dotnet/corefx","forks_url":"https://api.github.com/repos/dotnet/corefx/forks","keys_url":"https://api.github.com/repos/dotnet/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/corefx/teams","hooks_url":"https://api.github.com/repos/dotnet/corefx/hooks","issue_events_url":"https://api.github.com/repos/dotnet/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/corefx/events","assignees_url":"https://api.github.com/repos/dotnet/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/corefx/tags","blobs_url":"https://api.github.com/repos/dotnet/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/corefx/languages","stargazers_url":"https://api.github.com/repos/dotnet/corefx/stargazers","contributors_url":"https://api.github.com/repos/dotnet/corefx/contributors","subscribers_url":"https://api.github.com/repos/dotnet/corefx/subscribers","subscription_url":"https://api.github.com/repos/dotnet/corefx/subscription","commits_url":"https://api.github.com/repos/dotnet/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/corefx/merges","archive_url":"https://api.github.com/repos/dotnet/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/corefx/downloads","issues_url":"https://api.github.com/repos/dotnet/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/corefx/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/corefx/releases{/id}","deployments_url":"https://api.github.com/repos/dotnet/corefx/deployments","created_at":"2014-11-06T23:42:48Z","updated_at":"2016-04-08T20:32:28Z","pushed_at":"2016-04-08T21:09:36Z","git_url":"git://github.com/dotnet/corefx.git","ssh_url":"git@github.com:dotnet/corefx.git","clone_url":"https://github.com/dotnet/corefx.git","svn_url":"https://github.com/dotnet/corefx","homepage":"http://dotnet.github.io/","size":85550,"stargazers_count":9458,"watchers_count":9458,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2235,"mirror_url":null,"open_issues_count":924,"forks":2235,"open_issues":924,"watchers":9458,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/7546"},"html":{"href":"https://github.com/dotnet/corefx/pull/7546"},"issue":{"href":"https://api.github.com/repos/dotnet/corefx/issues/7546"},"comments":{"href":"https://api.github.com/repos/dotnet/corefx/issues/7546/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/7546/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/7546/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/corefx/statuses/c9f2289e76b074fb1a9ae20854717f4cc9b7ec3a"}}}}