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

faster rand(::UnitRange{Bool}) #25190

Closed
wants to merge 1 commit into from
Closed

faster rand(::UnitRange{Bool}) #25190

wants to merge 1 commit into from

Conversation

rfourquet
Copy link
Member

This becomes about twice as fast on my machine. The motivation is to work-around the regression I created in #25058.

@rfourquet rfourquet added performance Must go faster randomness Random number generation and the Random stdlib labels Dec 19, 2017
@rfourquet
Copy link
Member Author

@nanosoldier runbenchmarks("random", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - no performance regressions were detected. A full report can be found here. cc @ararslan

@stevengj
Copy link
Member

Is rand(false:true) really important to optimize? Shouldn't we just encourage people to use rand(Bool)?

I ask because this PR seems to add a lot of code to optimize a weird use-case.

@StefanKarpinski
Copy link
Sponsor Member

Agree, this seems like a very odd corner case.

@rfourquet
Copy link
Member Author

I never used this rand(::UnitRange{Bool}) thing, but clearly it should not be used in place of rand(Bool). Rather, it's for the case where the range is a dynamic value with unknown length at compile time. As I said, my motivation was to compensate for the performance regression I introduce somewhere else (because it was annoying to maintain where it was), and I don't want to be the one to make things slower if I can. Feel free to close if you think it should be; just know that, as this PR is improving on master, this implies a combined regression of at least about 2.70 (i.e. almost 3 times as slow) 😉 Otherwise I will merge eventually.

@stevengj
Copy link
Member

stevengj commented Dec 22, 2017

Yes, but what is the use case for a dynamic value on UnitRange{Bool}? It seems like in any case where you'd need this, you'd be better off passing one of () -> rand(Bool), () -> true, or () -> false.

I don't doubt that this PR makes rand(::UnitRange{Bool}) significantly faster—you can always speed things up by writing specialized code for weird cases. But speed improvements for (apparently) unimportant use cases are not worth significant increases in code complexity.

@rfourquet
Copy link
Member Author

Yes, but what is the use case for a dynamic value on UnitRange{Bool}

I don't know, I never needed that.

It seems like in any case where you'd need this, you'd be better off passing one of () -> rand(Bool), () -> true, or () -> false.

I don't think so, at least in a case where e.g. you get to Bool values aand b as a function parameter, and you need rand(a:b). What you propose would look like a == b ? a : rand(Bool), but rand!(X, a:b) would have to be something like X .= (() -> a == b ? a : rand(Bool)).() or something like this. But hard to say without a real use-case.

@stevengj
Copy link
Member

stevengj commented Dec 23, 2017

The fact that it is hard to come up with a real use-case for rand(::UnitRange{Bool}) suggests that optimization may be premature.

(I'm not opposed to a bit of defensive pre-optimization in the standard library as long as it doesn't sacrifice much in terms of code size or complexity. But that doesn't seem to be the case here.)

@rfourquet
Copy link
Member Author

Ok, let's re-open this if a convincing use-case pops-up.

@rfourquet rfourquet closed this Dec 24, 2017
@DilumAluthge DilumAluthge deleted the rf/rand/range-bool branch March 25, 2021 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants