Skip to content

Commit 041c9a0

Browse files
committed
core: Optimize str::bytes
This compiles down to a memmove. Takes about 1/4 of the time of the old version.
1 parent 771177a commit 041c9a0

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/libcore/str.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ Converts a string to a vector of bytes
311311
The result vector is not null-terminated.
312312
"]
313313
fn bytes(s: str) -> [u8] unsafe {
314-
as_bytes(s) { |v| vec::slice(v, 0u, vec::len(v) - 1u) }
314+
let mut s_copy = s;
315+
let mut v: [u8] = ::unsafe::reinterpret_cast(s_copy);
316+
::unsafe::leak(s_copy);
317+
vec::unsafe::set_len(v, len(s));
318+
ret v;
315319
}
316320

317321
#[doc = "Convert a string to a vector of characters"]

0 commit comments

Comments
 (0)