Skip to content

Commit

Permalink
Merge pull request #398 from weibocom/uuid
Browse files Browse the repository at this point in the history
减少对比次数
  • Loading branch information
viciousstar authored Nov 29, 2023
2 parents 37ec8d9 + 52db15f commit b4fec94
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion protocol/src/uuid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
Command, Commander, Error, Flag, HashedCommand, Metric, MetricItem, Protocol, RequestProcessor,
Result, Stream, Writer,
};
use ds::ByteOrder;
use sharding::hash::Hash;

#[derive(Clone, Default)]
Expand Down Expand Up @@ -36,7 +37,8 @@ impl Protocol for Uuid {
return Ok(None);
}
}
if !data.start_with(0, b"VALUE") {
//等价于if !data.start_with(0, b"VALU"),但更快,用于判断响应是否乱序
if data.u32_le(0) != u32::from_le_bytes(*b"VALU") {
return Err(crate::Error::UnexpectedData);
}
return Ok(Some(Command::from_ok(stream.take(oft))));
Expand Down
3 changes: 2 additions & 1 deletion tests/src/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ criterion_group!(
ring_slice,
ring_slice::bench_iter,
ring_slice::bench_copy,
ring_slice::bench_read_num
ring_slice::bench_read_num,
ring_slice::bench_read_num_vs_start_with
);
criterion_group!(arena, arena::bench_alloc);

Expand Down
30 changes: 30 additions & 0 deletions tests/src/benches/ring_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,33 @@ pub(super) fn bench_copy(c: &mut Criterion) {
});
group.finish();
}

pub(super) fn bench_read_num_vs_start_with(c: &mut Criterion) {
let mut group = c.benchmark_group("ring_slice_read_num_vs_start_with");
let s = b"1VALUE uuid 16784738294\r\njfdjk;afjkd;safjkds;ajfkdsa;".to_vec();
let runs = s.len() - 4;
let rs = RingSlice::from_vec(&s);
group.bench_function("read_num", |b| {
b.iter(|| {
black_box({
let mut v = 0;
for i in 0..runs {
v += (rs.u32_le(i) != u32::from_le_bytes(*b"VALU")) as u64;
}
v
});
})
});
group.bench_function("start_with", |b| {
b.iter(|| {
black_box({
let mut v = 0;
for i in 0..runs {
v += !rs.start_with(i, b"VALU") as u64;
}
v
});
})
});
group.finish();
}

0 comments on commit b4fec94

Please sign in to comment.