Skip to content
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

refactor rand on UnitRange / AbstractArray #9324

Closed
wants to merge 1 commit into from
Closed

Conversation

rfourquet
Copy link
Member

This is another approach to the (non-merged) refactoring part of #8309 and #8649.
The goal is to reduce to the minimum the different code paths taken by UnitRange and AbstractArray (UnitRanges are handled differently so that those with overflowing length still work).
In particular two rand! method are merged into one.

Only rand(::UnitRange{Bool}) becomes slower (by 10-15%), as it is no more in the group of specially supported ranges, because arithmetic on them doesn't produce Bool, so they would need individual support (which I can easily add if requested).

Note: This change suggests another one (0c6b81a): merge all other array filling methods.

@tkelman
Copy link
Contributor

tkelman commented Dec 12, 2014

@FeodorFitsner is it a bug that AppVeyor considers this PR (21a73b9) non-mergeable? Github and Travis think it's okay.

@FeodorFitsner
Copy link

Maybe it was kind of racing condition. AppVeyor checks virtual "merge" commit for existence using this call to GitHub API:

GET https://api.github.com/repos/JuliaLang/julia/git/refs/pull/9324/merge

When commit not found (404) PR is considered as non-mergeable. Maybe at the time of checking it wasn't there yet, because now it returns commit details.

@tkelman
Copy link
Contributor

tkelman commented Dec 12, 2014

Guess it was just a network hiccup then. I think I've seen the same thing happen once before, maybe Travis is trying multiple times before giving up? Or it might be working differently because they're a Github service vs AppVeyor works off a webhook. Let me see what the contents of the hook delivery were.

@FeodorFitsner
Copy link

I think small delay before checking virtual commit might be a solution (or retry). I'm going to add that with the next update.

@ivarne ivarne added the randomness Random number generation and the Random stdlib label Dec 14, 2014
@ViralBShah
Copy link
Member

@rfourquet Could you explain a little bit more about the refactoring in the commit?

I think this looks good. @ivarne ?

@ivarne
Copy link
Member

ivarne commented Dec 23, 2014

I don't see anything I would question right now. A few comments would probably make it easier to track down what is happening, and might help me see more potential issues.

@rfourquet
Copy link
Member Author

Thanks for looking at this! Ok I will explain more is the commit. @ivarne, do you mean adding comments in the source code? Really my motivation was simplification/removing duplication; previously I tried this by using endof_0 and getindex_0, and here it is kind of another way of putting it:r[rand(rng, g)] is replaced by rand(rng, r, g), so the indexing logic is handled in this rand method, and the equivalent of endof_0 is done in RangeGenerator constructors.

This is another approach to the (non-merged) refactoring part
of #8309 and #8649.
The goal is to reduce to the minimum the different code paths
taken by UnitRange and AbstractArray (UnitRange ranges are handled
differently so that those with overflowing length still work).
In particular two rand! method are merged into one.

Previously, RangeGenerator objects could create
(scalar of array of) random values in a range a:b, taking care of
creating first a random value in 0:b-a and then adding a. Choosing a
random value in an AbstractArray A was then using A[rand(1:length(A))].
Here, RangeGenerator is changed to only handle the creation of random
values in a range 0:k, and determining the right value of k
(length(A)-1 or b-a) and picking the right element using the random
value v (A[1+v] or a+v) is left to separate (and minimal) methods.
Hence Range and AbstractArray are handled as uniformly as possible,
given that we still want to support ranges like
typemin(Int):typemax(Int) for which length overflows.
@rfourquet
Copy link
Member Author

I updated the commit message, tell me if that helps or if I should try again.

@rfourquet
Copy link
Member Author

hmm, a bit late for a bump here 😉
Closing in favor of new PRs.

@rfourquet rfourquet closed this Dec 8, 2017
@rfourquet rfourquet deleted the rf/rand-ranges branch December 8, 2017 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants