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

Allow types other than Int in randstring #54402

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Seelengrab
Copy link
Contributor

Fixes #54397

What remains is that the effectively allowed input still needs to be convertible to Int and non-negative. So things like typemax(Int128) won't work.

Fixes JuliaLang#54397

What remains is that the effectively allowed input still needs
to be convertible to `Int`. So things like `typemax(Int128)` won't
work, and neither will negative values.
@Seelengrab Seelengrab added the domain:randomness Random number generation and the Random stdlib label May 8, 2024
@@ -71,13 +71,14 @@ let b = UInt8['0':'9';'A':'Z';'a':'z']
global randstring

function randstring(r::AbstractRNG, chars=b, n::Integer=8)
_n = convert(Int, n)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure it is worth doing in this specific instance but presumably structuring it like:

f(x::Integer) = f(convert(Int, n))
f(x::Int) = ...

would reduce compilation time since you would not compile the main function body for multiple integer types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For cases where lots of different types are passed to randstring, that's certainly the case, yeah. I'm not sure how common that would be though, there's lots of better ways to control the length of a generated string other than through the type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @KristofferC is just referring to the amount of code that is generated by using function barriers or:
https://docs.julialang.org/en/v1/manual/performance-tips/#kernel-functions

Copy link
Contributor Author

@Seelengrab Seelengrab May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm referring to the likelihood of hitting that & the additional compilation overhead being a significant slowdown. If this is a bottleneck, I'd first recommend switching to a different scheme for generating the length (with just Int) than optimize the compilation overhead.

Copy link
Contributor

@sjkelly sjkelly May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and the problem is that this PR would allow someone to have a type unstable call site with various Integer types for length, rather than error as it currently does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would that inherently be a problem? We don't prevent type instabilities in user code in other places either.

@sjkelly
Copy link
Contributor

sjkelly commented May 9, 2024 via email

@Seelengrab
Copy link
Contributor Author

Seelengrab commented May 9, 2024

Since Kristoffer mentioned that it's probably not worth it in this PR, I've chosen to keep the code simpler to follow without having to disentangle niche dispatch/compilation optimizations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Random stdlib: randstring(rand(UInt8)) errors
3 participants