From 57091bf44184742048468bef5a1b8daf6099196b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 21 May 2014 22:08:08 -0700 Subject: [PATCH] Add struct bench ints: 169ns vs 191ns enum: 376ns vs 347ns struct: 2432ns vs 3465ns --- de.rs | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) diff --git a/de.rs b/de.rs index 2c24b411..5a19578f 100644 --- a/de.rs +++ b/de.rs @@ -1164,6 +1164,141 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// + enum OuterDeserializerState { + OuterDeserializerOuterState(Outer), + OuterDeserializerInnerState(Inner), + OuterDeserializerFieldState(&'static str), + OuterDeserializerNullState, + OuterDeserializerUintState(uint), + OuterDeserializerCharState(char), + OuterDeserializerStrState(StrBuf), + OuterDeserializerOptionState(bool), + OuterDeserializerTupleState(uint), + OuterDeserializerVecState(uint), + OuterDeserializerMapState(uint), + OuterDeserializerSepState, + OuterDeserializerEndState, + + } + + struct OuterDeserializer { + stack: Vec, + } + + impl OuterDeserializer { + #[inline] + fn new(outer: Outer) -> OuterDeserializer { + OuterDeserializer { + stack: vec!(OuterDeserializerOuterState(outer)), + } + } + } + + impl Iterator> for OuterDeserializer { + #[inline] + fn next(&mut self) -> Option> { + match self.stack.pop() { + Some(OuterDeserializerOuterState(Outer { inner })) => { + self.stack.push(OuterDeserializerEndState); + + self.stack.push(OuterDeserializerEndState); + let len = inner.len(); + for v in inner.move_iter().rev() { + self.stack.push(OuterDeserializerInnerState(v)); + self.stack.push(OuterDeserializerSepState); + } + self.stack.push(OuterDeserializerVecState(len)); + + self.stack.push(OuterDeserializerFieldState("inner")); + Some(Ok(StructStart("Outer"))) + } + Some(OuterDeserializerInnerState(Inner { a: (), b: b, c: c })) => { + self.stack.push(OuterDeserializerEndState); + + self.stack.push(OuterDeserializerEndState); + let len = c.len(); + for (k, v) in c.move_iter() { + self.stack.push(OuterDeserializerEndState); + match v { + Some(c) => { + self.stack.push(OuterDeserializerCharState(c)); + self.stack.push(OuterDeserializerOptionState(true)); + } + None => { + self.stack.push(OuterDeserializerOptionState(false)); + } + } + self.stack.push(OuterDeserializerSepState); + + self.stack.push(OuterDeserializerStrState(k)); + self.stack.push(OuterDeserializerSepState); + self.stack.push(OuterDeserializerTupleState(2)); + + self.stack.push(OuterDeserializerSepState); + } + self.stack.push(OuterDeserializerMapState(len)); + + self.stack.push(OuterDeserializerFieldState("c")); + + self.stack.push(OuterDeserializerUintState(b)); + self.stack.push(OuterDeserializerFieldState("b")); + + self.stack.push(OuterDeserializerNullState); + self.stack.push(OuterDeserializerFieldState("a")); + Some(Ok(StructStart("Inner"))) + } + Some(OuterDeserializerFieldState(name)) => { + Some(Ok(StructField(name))) + } + Some(OuterDeserializerVecState(len)) => { + Some(Ok(SeqStart(len))) + } + Some(OuterDeserializerMapState(len)) => { + Some(Ok(MapStart(len))) + } + Some(OuterDeserializerTupleState(len)) => { + Some(Ok(TupleStart(len))) + } + Some(OuterDeserializerSepState) => { + Some(Ok(Sep)) + } + Some(OuterDeserializerNullState) => { + Some(Ok(Null)) + } + Some(OuterDeserializerUintState(x)) => { + Some(Ok(Uint(x))) + } + Some(OuterDeserializerCharState(x)) => { + Some(Ok(Char(x))) + } + Some(OuterDeserializerStrState(x)) => { + Some(Ok(StrBuf(x))) + } + Some(OuterDeserializerOptionState(x)) => { + Some(Ok(Option(x))) + } + Some(OuterDeserializerEndState) => { + Some(Ok(End)) + } + None => None, + } + } + } + + impl Deserializer for OuterDeserializer { + #[inline] + fn end_of_stream_error(&self) -> Error { + EndOfStream + } + + #[inline] + fn syntax_error(&self) -> Error { + SyntaxError + } + } + + ////////////////////////////////////////////////////////////////////////////// + #[test] fn test_tokens_int() { let tokens = vec!( @@ -1589,4 +1724,27 @@ mod tests { assert_eq!(value, outer); }) } + + #[bench] + fn bench_struct_deserializer(b: &mut Bencher) { + b.iter(|| { + let mut map = HashMap::new(); + map.insert("abc".to_strbuf(), Some('c')); + + let outer = Outer { + inner: vec!( + Inner { + a: (), + b: 5, + c: map, + }, + ) + }; + + let mut d = OuterDeserializer::new(outer.clone()); + let value: Outer = Deserializable::deserialize(&mut d).unwrap(); + + assert_eq!(value, outer); + }) + } }