{"action":"created","comment":{"url":"https://api.github.com/repos/nodejs/io.js/pulls/comments/34874693","id":34874693,"diff_hunk":"@@ -150,62 +150,83 @@ static const int unbase64_table[] =\n     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1\n   };\n-#define unbase64(x) unbase64_table[(uint8_t)(x)]\n+#define unbase64(x)                                                           \\\n+    static_cast<uint8_t>(unbase64_table[static_cast<uint8_t>(x)])\n \n \n template <typename TypeName>\n-size_t base64_decode(char* buf,\n-                     size_t len,\n-                     const TypeName* src,\n-                     const size_t srcLen) {\n-  char a, b, c, d;\n-  char* dst = buf;\n-  char* dstEnd = buf + len;\n-  const TypeName* srcEnd = src + srcLen;\n-\n-  while (src < srcEnd && dst < dstEnd) {\n-    int remaining = srcEnd - src;\n-\n-    while (unbase64(*src) < 0 && src < srcEnd)\n-      src++, remaining--;\n-    if (remaining == 0 || *src == '=')\n-      break;\n-    a = unbase64(*src++);\n-\n-    while (unbase64(*src) < 0 && src < srcEnd)\n-      src++, remaining--;\n-    if (remaining <= 1 || *src == '=')\n-      break;\n-    b = unbase64(*src++);\n-\n-    *dst++ = (a << 2) | ((b & 0x30) >> 4);\n-    if (dst == dstEnd)\n-      break;\n-\n-    while (unbase64(*src) < 0 && src < srcEnd)\n-      src++, remaining--;\n-    if (remaining <= 2 || *src == '=')\n-      break;\n-    c = unbase64(*src++);\n+size_t base64_decode_slow(char* dst, size_t dstlen,\n+                          const TypeName* src, size_t srclen) {\n+  uint8_t hi;\n+  uint8_t lo;\n+  size_t i = 0;\n+  size_t k = 0;\n+  for (;;) {\n+#define V(expr)                                                               \\\n+    while (i < srclen) {                                                      \\\n+      const uint8_t c = src[i];                                               \\\n+      lo = unbase64(c);                                                       \\\n+      i += 1;                                                                 \\\n+      if (lo < 64)                                                            \\\n+        break;  /* Legal character. */                                        \\\n+      if (c == '=')                                                           \\\n+        return k;                                                             \\\n+    }                                                                         \\\n+    expr;                                                                     \\\n+    if (i >= srclen)                                                          \\\n+      return k;                                                               \\\n+    if (k >= dstlen)                                                          \\\n+      return k;                                                               \\\n+    hi = lo;\n+    V(/* Nothing. */);\n+    V(dst[k++] = ((hi & 0x3F) << 2) | ((lo & 0x30) >> 4));\n+    V(dst[k++] = ((hi & 0x0F) << 4) | ((lo & 0x3C) >> 2));\n+    V(dst[k++] = ((hi & 0x03) << 6) | ((lo & 0x3F) >> 0));\n+#undef V\n+  }\n+  UNREACHABLE();\n+}\n \n-    *dst++ = ((b & 0x0F) << 4) | ((c & 0x3C) >> 2);\n-    if (dst == dstEnd)\n-      break;\n \n-    while (unbase64(*src) < 0 && src < srcEnd)\n-      src++, remaining--;\n-    if (remaining <= 3 || *src == '=')\n+template <typename TypeName>\n+size_t base64_decode_fast(char* const dst, const size_t dstlen,\n+                          const TypeName* const src, const size_t srclen,\n+                          const size_t decoded_size) {\n+  const size_t available = dstlen < decoded_size ? dstlen : decoded_size;\n+  const size_t max_i = srclen / 4 * 4;\n+  const size_t max_k = available / 3 * 3;\n+  size_t i = 0;\n+  size_t k = 0;\n+  while (i < max_i && k < max_k) {\n+    const uint32_t v =\n+        unbase64(src[i + 0]) << 24 |\n+        unbase64(src[i + 1]) << 16 |\n+        unbase64(src[i + 2]) << 8 |\n+        unbase64(src[i + 3]);\n+    // If MSB is set, input contains whitespace or is not valid base64.\n+    if (v & 0x80808080) {\n       break;\n-    d = unbase64(*src++);\n-\n-    *dst++ = ((c & 0x03) << 6) | (d & 0x3F);\n+    }\n+    dst[k + 0] = ((v >> 22) & 252) | ((v >> 20) & 3);","path":"src/string_bytes.cc","position":113,"original_position":113,"commit_id":"a5df4680f3ca7d74de4a80de3aded9b7d3dbf787","original_commit_id":"7f8acb6450ba1522af649587854e1eec55268b54","user":{"login":"bnoordhuis","id":275871,"avatar_url":"https://avatars.githubusercontent.com/u/275871?v=3","gravatar_id":"","url":"https://api.github.com/users/bnoordhuis","html_url":"https://github.com/bnoordhuis","followers_url":"https://api.github.com/users/bnoordhuis/followers","following_url":"https://api.github.com/users/bnoordhuis/following{/other_user}","gists_url":"https://api.github.com/users/bnoordhuis/gists{/gist_id}","starred_url":"https://api.github.com/users/bnoordhuis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bnoordhuis/subscriptions","organizations_url":"https://api.github.com/users/bnoordhuis/orgs","repos_url":"https://api.github.com/users/bnoordhuis/repos","events_url":"https://api.github.com/users/bnoordhuis/events{/privacy}","received_events_url":"https://api.github.com/users/bnoordhuis/received_events","type":"User","site_admin":false},"body":"Maybe it's me but I see the bit representation of small (< ~512) numbers right in front of me, whereas I have to mentally translate most hex values.","created_at":"2015-07-17T09:26:47Z","updated_at":"2015-07-17T09:26:47Z","html_url":"https://github.com/nodejs/io.js/pull/2193#discussion_r34874693","pull_request_url":"https://api.github.com/repos/nodejs/io.js/pulls/2193","_links":{"self":{"href":"https://api.github.com/repos/nodejs/io.js/pulls/comments/34874693"},"html":{"href":"https://github.com/nodejs/io.js/pull/2193#discussion_r34874693"},"pull_request":{"href":"https://api.github.com/repos/nodejs/io.js/pulls/2193"}}},"pull_request":{"url":"https://api.github.com/repos/nodejs/io.js/pulls/2193","id":40137394,"html_url":"https://github.com/nodejs/io.js/pull/2193","diff_url":"https://github.com/nodejs/io.js/pull/2193.diff","patch_url":"https://github.com/nodejs/io.js/pull/2193.patch","issue_url":"https://api.github.com/repos/nodejs/io.js/issues/2193","number":2193,"state":"open","locked":false,"title":"src: make base64 decoding 10-15% faster","user":{"login":"bnoordhuis","id":275871,"avatar_url":"https://avatars.githubusercontent.com/u/275871?v=3","gravatar_id":"","url":"https://api.github.com/users/bnoordhuis","html_url":"https://github.com/bnoordhuis","followers_url":"https://api.github.com/users/bnoordhuis/followers","following_url":"https://api.github.com/users/bnoordhuis/following{/other_user}","gists_url":"https://api.github.com/users/bnoordhuis/gists{/gist_id}","starred_url":"https://api.github.com/users/bnoordhuis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bnoordhuis/subscriptions","organizations_url":"https://api.github.com/users/bnoordhuis/orgs","repos_url":"https://api.github.com/users/bnoordhuis/repos","events_url":"https://api.github.com/users/bnoordhuis/events{/privacy}","received_events_url":"https://api.github.com/users/bnoordhuis/received_events","type":"User","site_admin":false},"body":"Make the inner loop execute fewer compare-and-branch executions per\r\nprocessed byte, resulting in a 10-15% speedup.\r\n\r\nThis coincidentally fixes an out-of-bounds read:\r\n\r\n    while (unbase64(*src) < 0 && src < srcEnd)\r\n\r\nShould have read:\r\n\r\n    while (src < srcEnd && unbase64(*src) < 0)\r\n\r\nBut this commit removes the offending code altogether.\r\n\r\nFixes: #2166\r\n\r\nR=@trevnorris?\r\n\r\nCI: https://jenkins-iojs.nodesource.com/view/iojs/job/iojs+any-pr+multi/155/","created_at":"2015-07-16T14:49:14Z","updated_at":"2015-07-17T09:26:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fa1f14c81d9d61f5dbdc55f983e3688a09ea20c4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nodejs/io.js/pulls/2193/commits","review_comments_url":"https://api.github.com/repos/nodejs/io.js/pulls/2193/comments","review_comment_url":"https://api.github.com/repos/nodejs/io.js/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nodejs/io.js/issues/2193/comments","statuses_url":"https://api.github.com/repos/nodejs/io.js/statuses/a5df4680f3ca7d74de4a80de3aded9b7d3dbf787","head":{"label":"bnoordhuis:optimize-base64-decode","ref":"optimize-base64-decode","sha":"a5df4680f3ca7d74de4a80de3aded9b7d3dbf787","user":{"login":"bnoordhuis","id":275871,"avatar_url":"https://avatars.githubusercontent.com/u/275871?v=3","gravatar_id":"","url":"https://api.github.com/users/bnoordhuis","html_url":"https://github.com/bnoordhuis","followers_url":"https://api.github.com/users/bnoordhuis/followers","following_url":"https://api.github.com/users/bnoordhuis/following{/other_user}","gists_url":"https://api.github.com/users/bnoordhuis/gists{/gist_id}","starred_url":"https://api.github.com/users/bnoordhuis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bnoordhuis/subscriptions","organizations_url":"https://api.github.com/users/bnoordhuis/orgs","repos_url":"https://api.github.com/users/bnoordhuis/repos","events_url":"https://api.github.com/users/bnoordhuis/events{/privacy}","received_events_url":"https://api.github.com/users/bnoordhuis/received_events","type":"User","site_admin":false},"repo":{"id":27560441,"name":"io.js","full_name":"bnoordhuis/io.js","owner":{"login":"bnoordhuis","id":275871,"avatar_url":"https://avatars.githubusercontent.com/u/275871?v=3","gravatar_id":"","url":"https://api.github.com/users/bnoordhuis","html_url":"https://github.com/bnoordhuis","followers_url":"https://api.github.com/users/bnoordhuis/followers","following_url":"https://api.github.com/users/bnoordhuis/following{/other_user}","gists_url":"https://api.github.com/users/bnoordhuis/gists{/gist_id}","starred_url":"https://api.github.com/users/bnoordhuis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bnoordhuis/subscriptions","organizations_url":"https://api.github.com/users/bnoordhuis/orgs","repos_url":"https://api.github.com/users/bnoordhuis/repos","events_url":"https://api.github.com/users/bnoordhuis/events{/privacy}","received_events_url":"https://api.github.com/users/bnoordhuis/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bnoordhuis/io.js","description":"Evented IO for V8 JavaScript","fork":true,"url":"https://api.github.com/repos/bnoordhuis/io.js","forks_url":"https://api.github.com/repos/bnoordhuis/io.js/forks","keys_url":"https://api.github.com/repos/bnoordhuis/io.js/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bnoordhuis/io.js/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bnoordhuis/io.js/teams","hooks_url":"https://api.github.com/repos/bnoordhuis/io.js/hooks","issue_events_url":"https://api.github.com/repos/bnoordhuis/io.js/issues/events{/number}","events_url":"https://api.github.com/repos/bnoordhuis/io.js/events","assignees_url":"https://api.github.com/repos/bnoordhuis/io.js/assignees{/user}","branches_url":"https://api.github.com/repos/bnoordhuis/io.js/branches{/branch}","tags_url":"https://api.github.com/repos/bnoordhuis/io.js/tags","blobs_url":"https://api.github.com/repos/bnoordhuis/io.js/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bnoordhuis/io.js/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bnoordhuis/io.js/git/refs{/sha}","trees_url":"https://api.github.com/repos/bnoordhuis/io.js/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bnoordhuis/io.js/statuses/{sha}","languages_url":"https://api.github.com/repos/bnoordhuis/io.js/languages","stargazers_url":"https://api.github.com/repos/bnoordhuis/io.js/stargazers","contributors_url":"https://api.github.com/repos/bnoordhuis/io.js/contributors","subscribers_url":"https://api.github.com/repos/bnoordhuis/io.js/subscribers","subscription_url":"https://api.github.com/repos/bnoordhuis/io.js/subscription","commits_url":"https://api.github.com/repos/bnoordhuis/io.js/commits{/sha}","git_commits_url":"https://api.github.com/repos/bnoordhuis/io.js/git/commits{/sha}","comments_url":"https://api.github.com/repos/bnoordhuis/io.js/comments{/number}","issue_comment_url":"https://api.github.com/repos/bnoordhuis/io.js/issues/comments{/number}","contents_url":"https://api.github.com/repos/bnoordhuis/io.js/contents/{+path}","compare_url":"https://api.github.com/repos/bnoordhuis/io.js/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bnoordhuis/io.js/merges","archive_url":"https://api.github.com/repos/bnoordhuis/io.js/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bnoordhuis/io.js/downloads","issues_url":"https://api.github.com/repos/bnoordhuis/io.js/issues{/number}","pulls_url":"https://api.github.com/repos/bnoordhuis/io.js/pulls{/number}","milestones_url":"https://api.github.com/repos/bnoordhuis/io.js/milestones{/number}","notifications_url":"https://api.github.com/repos/bnoordhuis/io.js/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bnoordhuis/io.js/labels{/name}","releases_url":"https://api.github.com/repos/bnoordhuis/io.js/releases{/id}","created_at":"2014-12-04T21:15:25Z","updated_at":"2014-12-30T16:28:26Z","pushed_at":"2015-07-17T08:42:32Z","git_url":"git://github.com/bnoordhuis/io.js.git","ssh_url":"git@github.com:bnoordhuis/io.js.git","clone_url":"https://github.com/bnoordhuis/io.js.git","svn_url":"https://github.com/bnoordhuis/io.js","homepage":null,"size":140025,"stargazers_count":2,"watchers_count":2,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":2,"default_branch":"v0.12"}},"base":{"label":"nodejs:master","ref":"master","sha":"0ab81e6f582ecb1817d3f30604304233d99dc0ca","user":{"login":"nodejs","id":9950313,"avatar_url":"https://avatars.githubusercontent.com/u/9950313?v=3","gravatar_id":"","url":"https://api.github.com/users/nodejs","html_url":"https://github.com/nodejs","followers_url":"https://api.github.com/users/nodejs/followers","following_url":"https://api.github.com/users/nodejs/following{/other_user}","gists_url":"https://api.github.com/users/nodejs/gists{/gist_id}","starred_url":"https://api.github.com/users/nodejs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nodejs/subscriptions","organizations_url":"https://api.github.com/users/nodejs/orgs","repos_url":"https://api.github.com/users/nodejs/repos","events_url":"https://api.github.com/users/nodejs/events{/privacy}","received_events_url":"https://api.github.com/users/nodejs/received_events","type":"Organization","site_admin":false},"repo":{"id":27193779,"name":"io.js","full_name":"nodejs/io.js","owner":{"login":"nodejs","id":9950313,"avatar_url":"https://avatars.githubusercontent.com/u/9950313?v=3","gravatar_id":"","url":"https://api.github.com/users/nodejs","html_url":"https://github.com/nodejs","followers_url":"https://api.github.com/users/nodejs/followers","following_url":"https://api.github.com/users/nodejs/following{/other_user}","gists_url":"https://api.github.com/users/nodejs/gists{/gist_id}","starred_url":"https://api.github.com/users/nodejs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nodejs/subscriptions","organizations_url":"https://api.github.com/users/nodejs/orgs","repos_url":"https://api.github.com/users/nodejs/repos","events_url":"https://api.github.com/users/nodejs/events{/privacy}","received_events_url":"https://api.github.com/users/nodejs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nodejs/io.js","description":"Future Node.js releases will be based on io.js","fork":false,"url":"https://api.github.com/repos/nodejs/io.js","forks_url":"https://api.github.com/repos/nodejs/io.js/forks","keys_url":"https://api.github.com/repos/nodejs/io.js/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nodejs/io.js/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nodejs/io.js/teams","hooks_url":"https://api.github.com/repos/nodejs/io.js/hooks","issue_events_url":"https://api.github.com/repos/nodejs/io.js/issues/events{/number}","events_url":"https://api.github.com/repos/nodejs/io.js/events","assignees_url":"https://api.github.com/repos/nodejs/io.js/assignees{/user}","branches_url":"https://api.github.com/repos/nodejs/io.js/branches{/branch}","tags_url":"https://api.github.com/repos/nodejs/io.js/tags","blobs_url":"https://api.github.com/repos/nodejs/io.js/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nodejs/io.js/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nodejs/io.js/git/refs{/sha}","trees_url":"https://api.github.com/repos/nodejs/io.js/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nodejs/io.js/statuses/{sha}","languages_url":"https://api.github.com/repos/nodejs/io.js/languages","stargazers_url":"https://api.github.com/repos/nodejs/io.js/stargazers","contributors_url":"https://api.github.com/repos/nodejs/io.js/contributors","subscribers_url":"https://api.github.com/repos/nodejs/io.js/subscribers","subscription_url":"https://api.github.com/repos/nodejs/io.js/subscription","commits_url":"https://api.github.com/repos/nodejs/io.js/commits{/sha}","git_commits_url":"https://api.github.com/repos/nodejs/io.js/git/commits{/sha}","comments_url":"https://api.github.com/repos/nodejs/io.js/comments{/number}","issue_comment_url":"https://api.github.com/repos/nodejs/io.js/issues/comments{/number}","contents_url":"https://api.github.com/repos/nodejs/io.js/contents/{+path}","compare_url":"https://api.github.com/repos/nodejs/io.js/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nodejs/io.js/merges","archive_url":"https://api.github.com/repos/nodejs/io.js/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nodejs/io.js/downloads","issues_url":"https://api.github.com/repos/nodejs/io.js/issues{/number}","pulls_url":"https://api.github.com/repos/nodejs/io.js/pulls{/number}","milestones_url":"https://api.github.com/repos/nodejs/io.js/milestones{/number}","notifications_url":"https://api.github.com/repos/nodejs/io.js/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nodejs/io.js/labels{/name}","releases_url":"https://api.github.com/repos/nodejs/io.js/releases{/id}","created_at":"2014-11-26T19:57:11Z","updated_at":"2015-07-17T08:57:08Z","pushed_at":"2015-07-17T08:42:34Z","git_url":"git://github.com/nodejs/io.js.git","ssh_url":"git@github.com:nodejs/io.js.git","clone_url":"https://github.com/nodejs/io.js.git","svn_url":"https://github.com/nodejs/io.js","homepage":"https://iojs.org","size":189309,"stargazers_count":12869,"watchers_count":12869,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1025,"mirror_url":null,"open_issues_count":292,"forks":1025,"open_issues":292,"watchers":12869,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nodejs/io.js/pulls/2193"},"html":{"href":"https://github.com/nodejs/io.js/pull/2193"},"issue":{"href":"https://api.github.com/repos/nodejs/io.js/issues/2193"},"comments":{"href":"https://api.github.com/repos/nodejs/io.js/issues/2193/comments"},"review_comments":{"href":"https://api.github.com/repos/nodejs/io.js/pulls/2193/comments"},"review_comment":{"href":"https://api.github.com/repos/nodejs/io.js/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nodejs/io.js/pulls/2193/commits"},"statuses":{"href":"https://api.github.com/repos/nodejs/io.js/statuses/a5df4680f3ca7d74de4a80de3aded9b7d3dbf787"}}}}