From 041c9a0863e01f2cfa92816eaaf8ea295f851157 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 15 Mar 2012 15:41:59 -0700 Subject: [PATCH] core: Optimize str::bytes This compiles down to a memmove. Takes about 1/4 of the time of the old version. --- src/libcore/str.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index aba5b22d85a..d2fceb2d85d 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -311,7 +311,11 @@ Converts a string to a vector of bytes The result vector is not null-terminated. "] fn bytes(s: str) -> [u8] unsafe { - as_bytes(s) { |v| vec::slice(v, 0u, vec::len(v) - 1u) } + let mut s_copy = s; + let mut v: [u8] = ::unsafe::reinterpret_cast(s_copy); + ::unsafe::leak(s_copy); + vec::unsafe::set_len(v, len(s)); + ret v; } #[doc = "Convert a string to a vector of characters"]