{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/14939","repository_url":"https://api.github.com/repos/JuliaLang/julia","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/14939/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/14939/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/14939/events","html_url":"https://github.com/JuliaLang/julia/issues/14939","id":131471628,"number":14939,"title":"Extra allocation when modifying arrays","user":{"login":"samuel-massinon-invenia","id":12241720,"avatar_url":"https://avatars.githubusercontent.com/u/12241720?v=3","gravatar_id":"","url":"https://api.github.com/users/samuel-massinon-invenia","html_url":"https://github.com/samuel-massinon-invenia","followers_url":"https://api.github.com/users/samuel-massinon-invenia/followers","following_url":"https://api.github.com/users/samuel-massinon-invenia/following{/other_user}","gists_url":"https://api.github.com/users/samuel-massinon-invenia/gists{/gist_id}","starred_url":"https://api.github.com/users/samuel-massinon-invenia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samuel-massinon-invenia/subscriptions","organizations_url":"https://api.github.com/users/samuel-massinon-invenia/orgs","repos_url":"https://api.github.com/users/samuel-massinon-invenia/repos","events_url":"https://api.github.com/users/samuel-massinon-invenia/events{/privacy}","received_events_url":"https://api.github.com/users/samuel-massinon-invenia/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2016-02-04T20:55:46Z","updated_at":"2016-02-04T22:16:36Z","closed_at":"2016-02-04T22:16:36Z","body":"I notice this that doing simply operations on an array can allocate a lot of memory when I don't think it would need to. For example:\r\n```\r\njulia> function foo(a::AbstractArray, value::Int)\r\n           for i in 1:length(a)\r\n               a[i] += value\r\n           end\r\n       end\r\nfoo (generic function with 1 method)\r\n\r\njulia> len = 100000000; a = collect(1:len);\r\n\r\njulia> foo(a, 20); #compile\r\n\r\njulia> @time foo(a, 20);\r\n  0.116317 seconds (4 allocations: 160 bytes)\r\n\r\njulia> @time a[:] += 20;\r\n  1.871425 seconds (13 allocations: 1.490 GB, 6.84% gc time)\r\n\r\njulia> @time a += 20;\r\n  0.542140 seconds (8 allocations: 762.940 MB, 0.33% gc time)\r\n\r\njulia> @time a + 20;\r\n  1.095872 seconds (8 allocations: 762.940 MB, 7.90% gc time)\r\n```\r\nFrom the example above, `foo,(a, 20)`, `a[:] += 20`, and `a += 20` are all doing the same thing, but my foo function is much faster and takes a fraction of the allocated memory.\r\n\r\nShould julia's memory management be able to identify those cases and avoid the extra allocation?"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/180075777","html_url":"https://github.com/JuliaLang/julia/issues/14939#issuecomment-180075777","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/14939","id":180075777,"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},"created_at":"2016-02-04T22:16:36Z","updated_at":"2016-02-04T22:16:36Z","body":"Closing as a duplicater of #249, #7052, etc."}}