about summary refs log tree commit diff
path: root/src/libextra/hex.rs
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2013-08-04 23:51:26 -0400
committerSteven Fackler <sfackler@gmail.com>2013-08-06 09:58:36 -0700
commitff5fdffc135f55e751dfcff0df1f52b9f4faad17 (patch)
tree1bf01497186c19249e1a8b2d4a8b7ca2854cae54 /src/libextra/hex.rs
parent463e2416e98238e294d332397048b106d85fd474 (diff)
ToBase64 and ToHex perf improvements
The overhead of str::push_char is high enough to cripple the performance
of these two functions. I've switched them to build the output in a
~[u8] and then convert to a string later. Since we know exactly the
bytes going into the vector, we can use the unsafe version to avoid the
is_utf8 check.

I could have riced it further with vec::raw::get, but it only added
~10MB/s so I didn't think it was worth it. ToHex is still ~30% slower
than FromHex, which is puzzling.

Before:

```
test base64::test::from_base64 ... bench: 1000 ns/iter (+/- 349) = 204 MB/s
test base64::test::to_base64 ... bench: 2390 ns/iter (+/- 1130) = 63 MB/s
...
test hex::tests::bench_from_hex ... bench: 884 ns/iter (+/- 220) = 341 MB/s
test hex::tests::bench_to_hex ... bench: 2453 ns/iter (+/- 919) = 61 MB/s
```

After:

```
test base64::test::from_base64 ... bench: 1271 ns/iter (+/- 600) = 160 MB/s
test base64::test::to_base64 ... bench: 759 ns/iter (+/- 286) = 198 MB/s
...
test hex::tests::bench_from_hex ... bench: 875 ns/iter (+/- 377) = 345 MB/s
test hex::tests::bench_to_hex ... bench: 593 ns/iter (+/- 240) = 254 MB/s
```
Diffstat (limited to 'src/libextra/hex.rs')
-rw-r--r--src/libextra/hex.rs14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/libextra/hex.rs b/src/libextra/hex.rs
index 5609c566d92..748d0391588 100644
--- a/src/libextra/hex.rs
+++ b/src/libextra/hex.rs
@@ -19,8 +19,7 @@ pub trait ToHex {
     fn to_hex(&self) -> ~str;
 }
 
-static CHARS: [char, ..16] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
-                              'a', 'b', 'c', 'd', 'e', 'f'];
+static CHARS: &'static[u8] = bytes!("0123456789abcdef");
 
 impl<'self> ToHex for &'self [u8] {
     /**
@@ -39,13 +38,16 @@ impl<'self> ToHex for &'self [u8] {
      * ~~~
      */
     fn to_hex(&self) -> ~str {
-        let mut s = str::with_capacity(self.len() * 2);
+        // +1 for NULL terminator
+        let mut v = vec::with_capacity(self.len() * 2 + 1);
         for &byte in self.iter() {
-            s.push_char(CHARS[byte >> 4]);
-            s.push_char(CHARS[byte & 0xf]);
+            v.push(CHARS[byte >> 4]);
+            v.push(CHARS[byte & 0xf]);
         }
 
-        s
+        unsafe {
+            str::raw::from_bytes_owned(v)
+        }
     }
 }