What's the fastest to convert a String to an `Vector{T}` where `isbits(T) == true` and back again?

I want to see what’s the fastest way to convert a String to an invertible its binary representation (which I assume will be an array of type T for some T). The codeunit should do the trick but it used to be slow.

Also how do I go back from a bunch of codeunit back to a string?

N = 1_000_000;
K = 100;

# faster string sort
svec = rand("id".*string.(1:N÷K, pad=10), N);

@time meh = reduce(vcat, codeunit.(svec, 1:length(svec)) for svec in svec) # takes too long

Is it codeunits? Also unsafe_string(pointer(codeunits("string")))

codeunits will give you an iterator that iterates the bytes of the string. String goes from an array of bytes to a String.

julia> str = "fαβ"
"fαβ"

julia> cu_iterator = codeunits(str)
5-element Base.CodeUnits{UInt8,String}:
 0x66
 0xce
 0xb1
 0xce
 0xb2

julia> cu_array = collect(cu_iterator)
5-element Array{UInt8,1}:
 0x66
 0xce
 0xb1
 0xce
 0xb2

julia> String(cu_array)
"fαβ"

There are also unsafer versions like

julia> unsafe_wrap(Vector{UInt8}, str)
5-element Array{UInt8,1}:
 0x66
 0xce
 0xb1
 0xce
 0xb2
1 Like