JuliaLang/julia — pull request #9174 by rfourquet (2014-11-27 07:27:25+00:00)
Title: rand!(A): use pointer directly instead of pointer_to_array

I discovered that `rand!(A::Array{Int})` was allocating memory in a debugging session, thinking that the allocations came from a type instability. I think this shouldn't, but this patch is quite not pretty. It would be less so if `getindex` was defined for pointers (I have no hope that this happens), and unneeded if `pointer_to_array` was modified to not allocate (no idea if this is possible).

Here are timings I get before/after:
```
julia> function arr{T}(::Type{T}, n, r=1)
a = zeros(T, n)
m = MersenneTwister()
tic()
a = @allocated for i in 1:r
rand!(m, a)
end
toq(), a/r
end

julia> [arr(Int, 10^i, 10^(9-i)) for i in 1:8] # master:
8-element Array{(Float64,Any),1}:
(24.119626243,144.0)
(5.701144431,144.0)
(1.941585068,160.0)
(1.328115407,176.0)
(1.345973947,176.0)
(1.344547687,176.0)
(2.675291505,176.0)
(2.861680458,176.0)

julia> [arr(Int, 10^i, 10^(9-i)) for i in 1:8] # this PR
8-element Array{(Float64,Any),1}:
(6.939816844,0.0)
(3.781428907,0.0)
(1.62118118,0.0)
(1.174099115,0.0)
(1.241850295,0.0)
(1.351900588,0.0)
(2.648783596,0.0)
(2.839158974,0.0)
```
Note: the timings for bigger arrays (length >10^5) are bad, I have a fix for that.