-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Non allocating rand!(A) (get rid of pointer_to_array) #9174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is to avoid the small allocations of pointer_to_array, which can add up and slow down repeated calls to rand!(A) on a same array A, in particular when A is small.
|
What makes |
|
I should have given more explanations. On master, |
|
But, why do we have 2 loops? Can't we just iterate over the length of the array? Can you create a small self-contained example about the memory allocation, which may be easier to work with? Cc: @JeffBezanson |
|
By the "outer loop", I was referring to the loop |
|
Yes this is really ugly. Surely there is another way to write this. I really do not want us to keep chasing moderate speedups at any cost. |
|
I am guessing pointer_to_array allocates memory for the array metadata. |
|
Personally, I'm not sure I would want this change if it was only for getting the modest speedup. But the allocations, which grow linearly with the number of times |
|
What happens here is really pointer arithmetic/dereferencing; the master version wraps this into arrays for a nicer syntax, but uses |
|
Yes, that is essentially the issue here. I don't know if getindex and setindex for pointer has been discussed before. I suspect it is intentionally not provided to discourage such code. I think this issue will apply for all small array operations, and is not specific to rand. Perhaps the point should be noted in other issues on small array performance. |
|
I wonder how subarray fares here, with the recent improvements. |
|
I am going to close this. Need a better solution, and the issue here is not |
|
With the following type, it is enough to change Would it be an acceptable solution? (until a better general solution is found) |
|
I would be ok to go with such a solution, as it can be easily jettisoned in the future without affecting the rest of the codebase. The gains for small random arrays seem worthwhile. Would love to hear what @JeffBezanson has to say. |
|
See #3224. |
|
As noted by @JeffBezanson, |
|
So do we yet have a better way of doing this? I'd rather get the ugly fix in and have an open issue to fix it. |
fix some FIXMEs in random, and take 2 to #9174
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 ifgetindexwas defined for pointers (I have no hope that this happens), and unneeded ifpointer_to_arraywas modified to not allocate (no idea if this is possible).Here are timings I get before/after: