diff --git a/bench_vec.rs b/bench_vec.rs index c94ca505..0225f58f 100644 --- a/bench_vec.rs +++ b/bench_vec.rs @@ -1,8 +1,9 @@ +use std::fmt::Show; use test::Bencher; -use serialize::Decodable; +use serialize::{Decoder, Decodable}; -use de::{Deserializable}; +use de::{Deserializer, Deserializable}; ////////////////////////////////////////////////////////////////////////////// @@ -21,6 +22,7 @@ mod decoder { use super::{Error, EndOfStream, SyntaxError}; pub struct IntsDecoder { + len: uint, iter: vec::MoveItems, } @@ -28,7 +30,8 @@ mod decoder { #[inline] pub fn new(values: Vec) -> IntsDecoder { IntsDecoder { - iter: values.move_iter() + len: values.len(), + iter: values.move_iter(), } } } @@ -105,7 +108,7 @@ mod decoder { #[inline] fn read_seq(&mut self, f: |&mut IntsDecoder, uint| -> Result) -> Result { - f(self, 3) + f(self, self.len) } #[inline] fn read_seq_elt(&mut self, _idx: uint, f: |&mut IntsDecoder| -> Result) -> Result { @@ -194,26 +197,70 @@ mod deserializer { ////////////////////////////////////////////////////////////////////////////// +fn run_decoder< + E: Show, + D: Decoder, + T: Clone + Eq + Show + Decodable +>(mut d: D, value: T) { + let v: T = Decodable::decode(&mut d).unwrap(); + + assert_eq!(value, v); +} + +fn run_deserializer< + E: Show, + D: Deserializer, + T: Clone + Eq + Show + Deserializable +>(mut d: D, value: T) { + let v: T = Deserializable::deserialize(&mut d).unwrap(); + + assert_eq!(value, v); +} + #[bench] -fn bench_ints_decoder(b: &mut Bencher) { +fn bench_decoder_vec_int_000(b: &mut Bencher) { b.iter(|| { - let ints = vec!(5, 6, 7); - - let mut d = decoder::IntsDecoder::new(ints); - let value: Vec = Decodable::decode(&mut d).unwrap(); - - assert_eq!(value, vec!(5, 6, 7)); + let v: Vec = vec!(); + run_decoder(decoder::IntsDecoder::new(v.clone()), v) }) } #[bench] -fn bench_ints_deserializer(b: &mut Bencher) { +fn bench_deserializer_vec_int_000(b: &mut Bencher) { b.iter(|| { - let ints = vec!(5, 6, 7); - - let mut d = deserializer::IntsDeserializer::new(ints); - let value: Vec = Deserializable::deserialize(&mut d).unwrap(); - - assert_eq!(value, vec!(5, 6, 7)); + let v: Vec = vec!(); + run_deserializer(deserializer::IntsDeserializer::new(v.clone()), v) + }) +} + +#[bench] +fn bench_decoder_vec_int_003(b: &mut Bencher) { + b.iter(|| { + let v: Vec = vec!(1, 2, 3); + run_decoder(decoder::IntsDecoder::new(v.clone()), v) + }) +} + +#[bench] +fn bench_deserializer_vec_int_003(b: &mut Bencher) { + b.iter(|| { + let v: Vec = vec!(1, 2, 3); + run_deserializer(deserializer::IntsDeserializer::new(v.clone()), v) + }) +} + +#[bench] +fn bench_decoder_vec_int_100(b: &mut Bencher) { + b.iter(|| { + let v: Vec = range(0, 100).collect(); + run_decoder(decoder::IntsDecoder::new(v.clone()), v) + }) +} + +#[bench] +fn bench_deserializer_vec_int_100(b: &mut Bencher) { + b.iter(|| { + let v: Vec = range(0, 100).collect(); + run_deserializer(deserializer::IntsDeserializer::new(v.clone()), v) }) } diff --git a/de.rs b/de.rs index 2c0c6385..e7de6cee 100644 --- a/de.rs +++ b/de.rs @@ -291,15 +291,16 @@ fn expect_rest_of_collection< >(d: &mut D, len: uint) -> Result { let iter = d.by_ref().batch(|d| { let d = d.iter(); - match d.next() { - Some(Ok(End)) => None, - Some(Ok(token)) => { - let value: Result = Deserializable::deserialize_token(d, token); - Some(value) - } - Some(Err(e)) => Some(Err(e)), - None => Some(Err(d.end_of_stream_error())), + + match d.next() { + Some(Ok(End)) => None, + Some(Ok(token)) => { + let value: Result = Deserializable::deserialize_token(d, token); + Some(value) } + Some(Err(e)) => Some(Err(e)), + None => Some(Err(d.end_of_stream_error())), + } }); result::collect_with_capacity(iter, len) diff --git a/serde.rs b/serde.rs index d3e2ae4e..09864cf7 100644 --- a/serde.rs +++ b/serde.rs @@ -14,8 +14,11 @@ extern crate serialize; pub mod de; //pub mod json; +//#[cfg(test)] +//pub mod bench_bytes; + #[cfg(test)] -pub mod bench_enum; +//pub mod bench_enum; #[cfg(test)] //pub mod bench_struct;