Add benchmark
This commit is contained in:
+29
-11
@@ -1,5 +1,6 @@
|
||||
pub mod generator;
|
||||
pub mod google;
|
||||
pub mod hackers;
|
||||
// Uncomment this to check if the code compiles
|
||||
// #[path = "../proto/google/protobuf/descriptor.rs"]
|
||||
// pub mod descriptor;
|
||||
@@ -220,11 +221,19 @@ impl<'a> ProtoAccessor<'a> {
|
||||
}
|
||||
_ => (cursor_after_tag, value_len),
|
||||
};
|
||||
Ok((&self.data[value_offset..value_offset + actual_value_len], tag.wire_type))
|
||||
Ok((
|
||||
&self.data[value_offset..value_offset + actual_value_len],
|
||||
tag.wire_type,
|
||||
))
|
||||
}
|
||||
|
||||
/// Returns an iterator that scans a specific range of the buffer for all occurrences of the specified field.
|
||||
pub fn iter_repeated_range(&self, field_number: u32, start: usize, end: usize) -> RepeatedFieldIterator<'a> {
|
||||
pub fn iter_repeated_range(
|
||||
&self,
|
||||
field_number: u32,
|
||||
start: usize,
|
||||
end: usize,
|
||||
) -> RepeatedFieldIterator<'a> {
|
||||
RepeatedFieldIterator::new_range(self.data, field_number, start, end)
|
||||
}
|
||||
}
|
||||
@@ -280,7 +289,11 @@ impl<'a> Iterator for FieldIterator<'a> {
|
||||
|
||||
self.cursor = cursor_after_tag + value_len;
|
||||
|
||||
Some(Ok((self.cursor - tag_len - value_len, tag, &self.data[value_offset..value_offset + actual_value_len])))
|
||||
Some(Ok((
|
||||
self.cursor - tag_len - value_len,
|
||||
tag,
|
||||
&self.data[value_offset..value_offset + actual_value_len],
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,10 +306,7 @@ pub struct RepeatedFieldIterator<'a> {
|
||||
impl<'a> RepeatedFieldIterator<'a> {
|
||||
pub fn new(data: &'a [u8], field_number: u32) -> Self {
|
||||
Self {
|
||||
iterator: FieldIterator {
|
||||
data,
|
||||
cursor: 0,
|
||||
},
|
||||
iterator: FieldIterator { data, cursor: 0 },
|
||||
field_number,
|
||||
end_offset: None,
|
||||
}
|
||||
@@ -491,7 +501,8 @@ mod tests {
|
||||
}
|
||||
assert_eq!(i32_vals, vec![1, 2, 3, 4, 5]);
|
||||
|
||||
let repeated_strings: Vec<_> = acc.iter_repeated(18)
|
||||
let repeated_strings: Vec<_> = acc
|
||||
.iter_repeated(18)
|
||||
.map(|r| {
|
||||
let (val, _) = r.expect("Failed to decode repeated string");
|
||||
std::str::from_utf8(val).expect("Invalid utf8")
|
||||
@@ -499,7 +510,8 @@ mod tests {
|
||||
.collect();
|
||||
assert_eq!(repeated_strings, vec!["one", "two", "three"]);
|
||||
|
||||
let repeated_nested: Vec<_> = acc.iter_repeated(19)
|
||||
let repeated_nested: Vec<_> = acc
|
||||
.iter_repeated(19)
|
||||
.map(|r| {
|
||||
let (val, _) = r.expect("Failed to decode repeated nested");
|
||||
let nested_acc = ProtoAccessor::new(val).unwrap();
|
||||
@@ -519,7 +531,8 @@ mod tests {
|
||||
assert_eq!(id, 200);
|
||||
|
||||
// Validate that fields appear in the expected relative order
|
||||
let field_numbers: Vec<u32> = acc.fields()
|
||||
let field_numbers: Vec<u32> = acc
|
||||
.fields()
|
||||
.map(|r| r.expect("Failed to decode field").1.field_number)
|
||||
.collect();
|
||||
|
||||
@@ -528,7 +541,12 @@ mod tests {
|
||||
let mut found_count = 0;
|
||||
for &f in &field_numbers {
|
||||
if essential_fields.contains(&f) {
|
||||
assert!(f >= last_field, "Fields appeared out of order: {} came after {}", f, last_field);
|
||||
assert!(
|
||||
f >= last_field,
|
||||
"Fields appeared out of order: {} came after {}",
|
||||
f,
|
||||
last_field
|
||||
);
|
||||
last_field = f;
|
||||
found_count += 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user