checkpoint vec without Sep, 184ns vs 182ns

This commit is contained in:
Erick Tryzelaar 2014-05-23 20:58:09 -07:00
parent 79f35c4d48
commit 2d1c7b43d7
3 changed files with 42 additions and 27 deletions

View File

@ -130,15 +130,15 @@ mod deserializer {
use de::{Token, Int, SeqStart, Sep, End};
#[deriving(Eq, Show)]
enum IntsDeserializerState {
IntsDeserializserStartState,
IntsDeserializserSepOrEndState,
IntsDeserializserValueState,
IntsDeserializserEndState,
enum State {
StartState,
SepOrEndState,
//ValueState,
EndState,
}
pub struct IntsDeserializer {
state: IntsDeserializerState,
state: State,
len: uint,
iter: vec::MoveItems<int>,
value: Option<int>
@ -148,7 +148,7 @@ mod deserializer {
#[inline]
pub fn new(values: Vec<int>) -> IntsDeserializer {
IntsDeserializer {
state: IntsDeserializserStartState,
state: StartState,
len: values.len(),
iter: values.move_iter(),
value: None,
@ -160,31 +160,34 @@ mod deserializer {
#[inline]
fn next(&mut self) -> Option<Result<Token, Error>> {
match self.state {
IntsDeserializserStartState => {
self.state = IntsDeserializserSepOrEndState;
StartState => {
self.state = SepOrEndState;
Some(Ok(SeqStart(self.len)))
}
IntsDeserializserSepOrEndState => {
SepOrEndState => {
match self.iter.next() {
Some(value) => {
self.state = IntsDeserializserValueState;
self.value = Some(value);
Some(Ok(Sep))
//self.state = ValueState;
//self.value = Some(value);
//Some(Ok(Sep))
Some(Ok(Int(value)))
}
None => {
self.state = IntsDeserializserEndState;
self.state = EndState;
Some(Ok(End))
}
}
}
IntsDeserializserValueState => {
self.state = IntsDeserializserSepOrEndState;
/*
ValueState => {
self.state = SepOrEndState;
match self.value.take() {
Some(value) => Some(Ok(Int(value))),
None => Some(Err(self.end_of_stream_error())),
}
}
IntsDeserializserEndState => {
*/
EndState => {
None
}
}
@ -204,11 +207,9 @@ mod deserializer {
#[inline]
fn expect_num<T: NumCast>(&mut self) -> Result<T, Error> {
assert_eq!(self.state, IntsDeserializserValueState);
assert_eq!(self.state, SepOrEndState);
self.state = IntsDeserializserSepOrEndState;
match self.value.take() {
match self.iter.next() {
Some(value) => {
match num::cast(value) {
Some(value) => Ok(value),

22
de.rs
View File

@ -272,14 +272,25 @@ fn expect_rest_of_collection<
T: Deserializable<E, D>,
C: FromIterator<T>
>(d: &mut D, len: uint) -> Result<C, E> {
let mut idx = 0;
let iter = d.by_ref().batch(|d| {
let d = d.iter();
let token = match d.next() {
Some(token) => token,
None => { return None; }
};
if idx < len {
idx += 1;
let value: Result<T, E> = Deserializable::deserialize(d);
Some(value)
} else {
match d.next() {
Some(Ok(End)) => None,
Some(Ok(_)) => Some(Err(d.syntax_error())),
Some(Err(e)) => Some(Err(e)),
None => Some(Err(d.end_of_stream_error())),
}
}
/*
match token {
Ok(Sep) => {
let value: Result<T, E> = Deserializable::deserialize(d);
@ -289,6 +300,7 @@ fn expect_rest_of_collection<
Ok(_) => Some(Err(d.syntax_error())),
Err(e) => Some(Err(e)),
}
*/
});
result::collect_with_capacity(iter, len)
@ -422,6 +434,7 @@ deserialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
//////////////////////////////////////////////////////////////////////////////
/*
#[cfg(test)]
mod tests {
use collections::HashMap;
@ -901,3 +914,4 @@ mod tests {
})
}
}
*/

View File

@ -15,10 +15,10 @@ pub mod de;
//pub mod json;
#[cfg(test)]
pub mod bench_enum;
//pub mod bench_enum;
#[cfg(test)]
pub mod bench_struct;
//pub mod bench_struct;
#[cfg(test)]
pub mod bench_vec;