Skip to content

Commit c1b2fe2

Browse files
authored
Random: cut randstring allocation size in half (JuliaLang#40584)
1 parent e5da821 commit c1b2fe2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

stdlib/Random/src/misc.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ function randstring end
7171

7272
let b = UInt8['0':'9';'A':'Z';'a':'z']
7373
global randstring
74-
randstring(r::AbstractRNG, chars=b, n::Integer=8) = String(rand(r, chars, n))
74+
function randstring(r::AbstractRNG, chars=b, n::Integer=8)
75+
T = eltype(chars)
76+
v = T === UInt8 ? Base.StringVector(n) : Vector{T}(undef, n)
77+
rand!(r, v, chars)
78+
return String(v)
79+
end
7580
randstring(r::AbstractRNG, n::Integer) = randstring(r, b, n)
7681
randstring(chars=b, n::Integer=8) = randstring(default_rng(), chars, n)
7782
randstring(n::Integer) = randstring(default_rng(), b, n)

0 commit comments

Comments
 (0)