Skip to content

Commit 28eaca3

Browse files
author
Fahad Zubair
committed
Move blob benches to a separate file.
1 parent 79ebd87 commit 28eaca3

File tree

3 files changed

+84
-41
lines changed

3 files changed

+84
-41
lines changed

rust-runtime/aws-smithy-cbor/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ criterion = "0.5.1"
3434
[[bench]]
3535
name = "string"
3636
harness = false
37+
38+
[[bench]]
39+
name = "blob"
40+
harness = false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use aws_smithy_cbor::decode::Decoder;
2+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
3+
4+
pub fn blob_benchmark(c: &mut Criterion) {
5+
// Indefinite length blob containing bytes corresponding to `indefinite-byte, chunked, on each comma`.
6+
let blob_indefinite_bytes = [
7+
0x5f, 0x50, 0x69, 0x6e, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x65, 0x2d, 0x62, 0x79,
8+
0x74, 0x65, 0x2c, 0x49, 0x20, 0x63, 0x68, 0x75, 0x6e, 0x6b, 0x65, 0x64, 0x2c, 0x4e, 0x20,
9+
0x6f, 0x6e, 0x20, 0x65, 0x61, 0x63, 0x68, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0xff,
10+
];
11+
12+
c.bench_function("blob", |b| {
13+
b.iter(|| {
14+
let mut decoder = Decoder::new(&blob_indefinite_bytes);
15+
let _ = black_box(decoder.blob());
16+
})
17+
});
18+
}
19+
20+
criterion_group!(benches, blob_benchmark);
21+
criterion_main!(benches);
Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use aws_smithy_cbor::decode::Decoder;
24
use criterion::{black_box, criterion_group, criterion_main, Criterion};
35

@@ -13,37 +15,75 @@ pub fn str_benchmark(c: &mut Criterion) {
1315
0xff,
1416
];
1517

16-
c.bench_function("definite str_alt", |b| {
18+
c.bench_function("definite str()", |b| {
1719
b.iter(|| {
1820
let mut decoder = Decoder::new(&definite_bytes);
19-
let x = black_box(decoder.str_alt());
21+
let x = black_box(decoder.str());
2022
assert!(matches!(x.unwrap().as_ref(), "thisIsAKey"));
2123
})
2224
});
23-
c.bench_function("indefinite str_alt", |b| {
25+
26+
c.bench_function("definite str_alt", |b| {
2427
b.iter(|| {
25-
let mut decoder = Decoder::new(&indefinite_bytes);
26-
let x = black_box(decoder.str_alt());
28+
let mut decoder = minicbor::decode::Decoder::new(&indefinite_bytes);
29+
let x = black_box(str_alt(&mut decoder));
2730
assert!(matches!(x.unwrap().as_ref(), "thisIsAKey"));
2831
})
2932
});
3033

31-
c.bench_function("definite str()", |b| {
34+
c.bench_function("indefinite str()", |b| {
3235
b.iter(|| {
33-
let mut decoder = Decoder::new(&definite_bytes);
36+
let mut decoder = Decoder::new(&indefinite_bytes);
3437
let x = black_box(decoder.str());
3538
assert!(matches!(x.unwrap().as_ref(), "thisIsAKey"));
3639
})
3740
});
38-
c.bench_function("indefinite str()", |b| {
41+
42+
c.bench_function("indefinite str_alt", |b| {
3943
b.iter(|| {
40-
let mut decoder = Decoder::new(&indefinite_bytes);
41-
let x = black_box(decoder.str());
44+
let mut decoder = minicbor::decode::Decoder::new(&indefinite_bytes);
45+
let x = black_box(str_alt(&mut decoder));
4246
assert!(matches!(x.unwrap().as_ref(), "thisIsAKey"));
4347
})
4448
});
4549
}
4650

51+
// The following seems to be a bit slower than the implementation that we have
52+
// kept in the `aws_smithy_cbor::Decoder`.
53+
pub fn string_alt<'b>(
54+
decoder: &'b mut minicbor::Decoder<'b>,
55+
) -> Result<String, minicbor::decode::Error> {
56+
decoder.str_iter()?.collect()
57+
}
58+
59+
// The following seems to be a bit slower than the implementation that we have
60+
// kept in the `aws_smithy_cbor::Decoder`.
61+
fn str_alt<'b>(
62+
decoder: &'b mut minicbor::Decoder<'b>,
63+
) -> Result<Cow<'b, str>, minicbor::decode::Error> {
64+
// This implementation uses `next` twice to see if there is
65+
// another str chunk. If there is, it returns a owned `String`.
66+
let mut chunks_iter = decoder.str_iter()?;
67+
let head = match chunks_iter.next() {
68+
Some(Ok(head)) => head,
69+
None => return Ok(Cow::Borrowed("")),
70+
Some(Err(e)) => return Err(e),
71+
};
72+
73+
match chunks_iter.next() {
74+
None => Ok(Cow::Borrowed(head)),
75+
Some(Err(e)) => Err(e),
76+
Some(Ok(next)) => {
77+
let mut concatenated_string = String::from(head);
78+
concatenated_string.push_str(next);
79+
for chunk in chunks_iter {
80+
concatenated_string.push_str(chunk?);
81+
}
82+
Ok(Cow::Owned(concatenated_string))
83+
}
84+
}
85+
}
86+
4787
// We have two `string` implementations. One uses `collect` the other
4888
// uses `String::new` followed by `string::push`.
4989
pub fn string_benchmark(c: &mut Criterion) {
@@ -61,53 +101,31 @@ pub fn string_benchmark(c: &mut Criterion) {
61101
c.bench_function("definite string()", |b| {
62102
b.iter(|| {
63103
let mut decoder = Decoder::new(&definite_bytes);
64-
let _ = black_box(decoder.str());
65-
})
66-
});
67-
68-
c.bench_function("indefinite string()", |b| {
69-
b.iter(|| {
70-
let mut decoder = Decoder::new(&indefinite_bytes);
71-
let _ = black_box(decoder.str());
104+
let _ = black_box(decoder.string());
72105
})
73106
});
74107

75108
c.bench_function("definite string_alt()", |b| {
76109
b.iter(|| {
77-
let mut decoder = Decoder::new(&definite_bytes);
78-
let _ = black_box(decoder.str_alt());
110+
let mut decoder = minicbor::decode::Decoder::new(&indefinite_bytes);
111+
let _ = black_box(string_alt(&mut decoder));
79112
})
80113
});
81114

82-
c.bench_function("indefinite string_alt()", |b| {
115+
c.bench_function("indefinite string()", |b| {
83116
b.iter(|| {
84117
let mut decoder = Decoder::new(&indefinite_bytes);
85-
let _ = black_box(decoder.str_alt());
118+
let _ = black_box(decoder.string());
86119
})
87120
});
88-
}
89121

90-
pub fn blob_benchmark(c: &mut Criterion) {
91-
// Indefinite length blob containing bytes corresponding to `indefinite-byte, chunked, on each comma`.
92-
let blob_indefinite_bytes = [
93-
0x5f, 0x50, 0x69, 0x6e, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x65, 0x2d, 0x62, 0x79,
94-
0x74, 0x65, 0x2c, 0x49, 0x20, 0x63, 0x68, 0x75, 0x6e, 0x6b, 0x65, 0x64, 0x2c, 0x4e, 0x20,
95-
0x6f, 0x6e, 0x20, 0x65, 0x61, 0x63, 0x68, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0xff,
96-
];
97-
98-
c.bench_function("blob", |b| {
122+
c.bench_function("indefinite string_alt()", |b| {
99123
b.iter(|| {
100-
let mut decoder = Decoder::new(&blob_indefinite_bytes);
101-
let _ = black_box(decoder.blob());
124+
let mut decoder = minicbor::decode::Decoder::new(&indefinite_bytes);
125+
let _ = black_box(string_alt(&mut decoder));
102126
})
103127
});
104128
}
105129

106-
criterion_group!(
107-
benches,
108-
string_benchmark,
109-
// str_benchmark blob_benchmark,
110-
// str_benchmark,
111-
);
112-
130+
criterion_group!(benches, string_benchmark, str_benchmark,);
113131
criterion_main!(benches);

0 commit comments

Comments
 (0)