{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/13162","labels_url":"https://api.github.com/repos/golang/go/issues/13162/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/13162/comments","events_url":"https://api.github.com/repos/golang/go/issues/13162/events","html_url":"https://github.com/golang/go/issues/13162","id":115326261,"number":13162,"title":"cmd/compile: performance issue with ranging over string","user":{"login":"mpvl","id":6445383,"avatar_url":"https://avatars.githubusercontent.com/u/6445383?v=3","gravatar_id":"","url":"https://api.github.com/users/mpvl","html_url":"https://github.com/mpvl","followers_url":"https://api.github.com/users/mpvl/followers","following_url":"https://api.github.com/users/mpvl/following{/other_user}","gists_url":"https://api.github.com/users/mpvl/gists{/gist_id}","starred_url":"https://api.github.com/users/mpvl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mpvl/subscriptions","organizations_url":"https://api.github.com/users/mpvl/orgs","repos_url":"https://api.github.com/users/mpvl/repos","events_url":"https://api.github.com/users/mpvl/events{/privacy}","received_events_url":"https://api.github.com/users/mpvl/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-05T16:50:56Z","updated_at":"2015-11-05T17:02:33Z","closed_at":null,"body":"Looping over a string with range is significantly slower than using utf8.DecodeRuneInString.\r\n\r\nSo using \r\nfor i, r := range s { ... }\r\n\r\nis significantly slower than \r\n\r\nvar size int\r\nfor i := 0; i < len(s); {\r\n\t\tr := rune(s[i])\r\n\t\tif r < RuneSelf {\r\n\t\t\ti++\r\n\t\t} else {\r\n\t\t\tr, size = DecodeRune(s[i:])\r\n\t\t\ti += size\r\n\t\t}\r\n}\r\n\r\nAn (approximate) example of is one gets by comparing the benchmarks of the implementations for bytes and strings in unicode/utf8:\r\n\r\nBenchmarkRuneCountTenASCIIChars-8           \t100000000\t        16.6 ns/op\r\nBenchmarkRuneCountInStringTenASCIIChars-8   \t30000000\t        43.1 ns/op\r\n\r\nBenchmarkValidTenASCIIChars-8               \t100000000\t        13.8 ns/op\r\nBenchmarkValidStringTenASCIIChars-8         \t30000000\t        52.5 ns/op\r\n\r\nThe benchmarks for the *String versions are considerably slower than their bytes counterparts, when processing ASCII."},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/154123074","html_url":"https://github.com/golang/go/issues/13162#issuecomment-154123074","issue_url":"https://api.github.com/repos/golang/go/issues/13162","id":154123074,"user":{"login":"gopherbot","id":8566911,"avatar_url":"https://avatars.githubusercontent.com/u/8566911?v=3","gravatar_id":"","url":"https://api.github.com/users/gopherbot","html_url":"https://github.com/gopherbot","followers_url":"https://api.github.com/users/gopherbot/followers","following_url":"https://api.github.com/users/gopherbot/following{/other_user}","gists_url":"https://api.github.com/users/gopherbot/gists{/gist_id}","starred_url":"https://api.github.com/users/gopherbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gopherbot/subscriptions","organizations_url":"https://api.github.com/users/gopherbot/orgs","repos_url":"https://api.github.com/users/gopherbot/repos","events_url":"https://api.github.com/users/gopherbot/events{/privacy}","received_events_url":"https://api.github.com/users/gopherbot/received_events","type":"User","site_admin":false},"created_at":"2015-11-05T17:02:33Z","updated_at":"2015-11-05T17:02:33Z","body":"CL https://golang.org/cl/16695 mentions this issue."}}