I am writing a fast serializer of strings onto disk. What I found was that a String’s memory structure is very simple. It’s basically a UInt64
followed by bytes. The first UInt64
indicates the length of the string, and the subsequent bytes are the content of a string.
E.g…
a = reinterpret(UInt8, [2])
a = vcat(a, UInt8[63, 64])
b = String(UInt8[63, 64])
b_structure = unsafe_load.(pointer(b)-8, 1:10)
b_structure == a
a = reinterpret(UInt8, [2])
a = vcat(a, UInt8[63, 64])
b = String(UInt8[63, 64])
b_structure = unsafe_load.(pointer(b)-8, 1:10)
b_structure == a
Notice in the above b
is a string. But a
is just some array of UInt
. If I convert the string b
to its binary representation, you will see that they have the same structure.
Now if I want to make the content start from pointer(a)
into a String. How do I do that? I need a no-copy solution! So unsafe_string
doesn’t fit the bill here.
Thanks!!