{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/11350","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/11350/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/11350/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/11350/events","html_url":"https://github.com/JuliaLang/julia/issues/11350","id":78134608,"number":11350,"title":"\"for x in A\" array iteration should imply @inbounds?","user":{"login":"stevengj","id":2913679,"avatar_url":"https://avatars.githubusercontent.com/u/2913679?v=3","gravatar_id":"","url":"https://api.github.com/users/stevengj","html_url":"https://github.com/stevengj","followers_url":"https://api.github.com/users/stevengj/followers","following_url":"https://api.github.com/users/stevengj/following{/other_user}","gists_url":"https://api.github.com/users/stevengj/gists{/gist_id}","starred_url":"https://api.github.com/users/stevengj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stevengj/subscriptions","organizations_url":"https://api.github.com/users/stevengj/orgs","repos_url":"https://api.github.com/users/stevengj/repos","events_url":"https://api.github.com/users/stevengj/events{/privacy}","received_events_url":"https://api.github.com/users/stevengj/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/performance","name":"performance","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-05-19T15:27:35Z","updated_at":"2015-05-19T15:58:54Z","closed_at":null,"body":"Here's something odd I noticed when looking at #11241.  Compare the following two functions:\r\n```jl\r\nfunction foo(s::Vector{UInt8})\r\n    for c in s\r\n        c >= 128 && return false\r\n    end\r\n    return true\r\nend\r\nfunction bar(s::Vector{UInt8})\r\n    for i = 1:length(s)\r\n        @inbounds s[i] >= 128 && return false\r\n    end\r\n    return true\r\nend\r\n```\r\nI would have thought that these would be roughly equivalent in terms of speed, but `code_native` shows that vastly less code is generated for `bar` than for `foo`:\r\n```\r\njulia> code_native(foo, (Vector{UInt8},))\r\n\t.section\t__TEXT,__text,regular,pure_instructions\r\nFilename: none\r\nSource line: 2\r\n\tpushq\t%rbp\r\n\tmovq\t%rsp, %rbp\r\nSource line: 2\r\n\tcmpq\t$0, 8(%rdi)\r\n\tjle\tL65\r\n\tmovq\t8(%rdi), %rdx\r\n\txorl\t%ecx, %ecx\r\nL21:\tcmpq\t%rdx, %rcx\r\n\tjae\tL72\r\n\tmovq\t(%rdi), %rsi\r\n\txorb\t%al, %al\r\n\tcmpb\t$0, (%rsi,%rcx)\r\n\tjs\tL67\r\nSource line: 3\r\n\tleaq\t1(%rcx), %rax\r\n\taddq\t$2, %rcx\r\n\tcmpq\t%rcx, %rdx\r\n\tmovq\t%rax, %rcx\r\n\tjge\tL21\r\nL65:\tmovb\t$1, %al\r\nL67:\tmovq\t%rbp, %rsp\r\n\tpopq\t%rbp\r\n\tret\r\nSource line: 2\r\nL72:\tmovq\t%rsp, %rax\r\n\tleaq\t-16(%rax), %rsi\r\n\tmovq\t%rsi, %rsp\r\n\tincq\t%rcx\r\n\tmovq\t%rcx, -16(%rax)\r\n\tmovabsq\t$jl_bounds_error_ints, %rax\r\n\tmovl\t$1, %edx\r\n\tcallq\t*%rax\r\n\r\njulia> code_native(bar, (Vector{UInt8},))\r\n\t.section\t__TEXT,__text,regular,pure_instructions\r\nFilename: none\r\nSource line: 2\r\n\tpushq\t%rbp\r\n\tmovq\t%rsp, %rbp\r\nSource line: 2\r\n\tmovq\t8(%rdi), %rcx\r\n\ttestq\t%rcx, %rcx\r\n\tjle\tL43\r\nSource line: 3\r\n\tmovq\t(%rdi), %rdx\r\n\txorb\t%al, %al\r\nL22:\tcmpb\t$0, (%rdx)\r\n\tjs\tL45\r\n\tincq\t%rdx\r\n\tdecq\t%rcx\r\n\tjne\tL22\r\nL43:\tmovb\t$1, %al\r\nL45:\tpopq\t%rbp\r\n\tret\r\n```\r\nNote in particular that `foo` apparently still thinks that it can throw a `BoundsError`, even though `for c in s` should always be in-bounds.\r\n\r\nIs this a known issue?"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/103563660","html_url":"https://github.com/JuliaLang/julia/issues/11350#issuecomment-103563660","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/11350","id":103563660,"user":{"login":"simonster","id":470884,"avatar_url":"https://avatars.githubusercontent.com/u/470884?v=3","gravatar_id":"","url":"https://api.github.com/users/simonster","html_url":"https://github.com/simonster","followers_url":"https://api.github.com/users/simonster/followers","following_url":"https://api.github.com/users/simonster/following{/other_user}","gists_url":"https://api.github.com/users/simonster/gists{/gist_id}","starred_url":"https://api.github.com/users/simonster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simonster/subscriptions","organizations_url":"https://api.github.com/users/simonster/orgs","repos_url":"https://api.github.com/users/simonster/repos","events_url":"https://api.github.com/users/simonster/events{/privacy}","received_events_url":"https://api.github.com/users/simonster/received_events","type":"User","site_admin":false},"created_at":"2015-05-19T15:58:54Z","updated_at":"2015-05-19T15:58:54Z","body":"I don't think resizing should present an issue for Array iteration since `done` checks `i > length(a)`. But for e.g. Dicts there could be problems.\r\n\r\nSome related discussion in https://github.com/JuliaLang/julia/issues/7799#issuecomment-86039191."}}