Search code examples
stringperformancerustvectortype-conversion

Is converting between String & Vec<u8> a no-op in --release binary?


String & Vec<u8> are almost the same to me, though String guarantees to have valid UTF-8 content, which is often useful.

However, being in unsafe context, does it really take any machine operation to cast between two of them if no check is performed?

Consider these two functions:

  • pub unsafe fn from_utf8_unchecked(bytes: Vec<u8, Global>) -> String
  • pub fn into_bytes(self) -> Vec<u8, Global>

They're both consuming input, so a compiler has theoretically no need to render a new object in memory.


Solution

  • With the unsafe version of the function it is a no-op. As you can see here, the assembly for converting a string into/from a vec without checks is the same as the identity function on a vec. This does not mean you should just use the unsafe function for performance, you should use the unsafe function if by profiling you determine the performance is necessary and you can guarantee that the vector you give to the function will always contain vaid UTF-8.