1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170


#![allow(unused_imports)]
use {
    fidl::{
        encoding::{Context, Decodable, Decoder, Encoder, with_tls_encode_buf, WireFormatVersion},
        handle::Handle, handle::HandleDisposition, handle::HandleInfo, handle::HandleOp,
        ObjectType, Rights,
    },
    fidl_test_benchmarkfidltable as test_benchmarkfidltable,
    fuchsia_async as fasync,
    fuchsia_criterion::criterion::{BatchSize, Bencher, black_box},
    futures::{future, stream::StreamExt},
    fuchsia_zircon as zx,
    gidl_util::{HandleDef, HandleSubtype, copy_handle, create_handles, select_handle_infos},
    std::mem::ManuallyDrop
};

// BENCHMARKS is aggregated by a generated benchmark_suite.rs file, which is ultimately
// used in benchmarks in src/tests/benchmarks/fidl/rust/src/main.rs.
pub const BENCHMARKS: [(&'static str, fn(&mut Bencher)); 9] = [
    ("Builder/Table/SingleSet/1_of_63", benchmark_table_single_set_1_of_63_builder),
    ("Encode/Table/SingleSet/1_of_63", benchmark_table_single_set_1_of_63_encode),
    ("Decode/Table/SingleSet/1_of_63", benchmark_table_single_set_1_of_63_decode),
    
    ("Builder/Table/SingleSet/16_of_63", benchmark_table_single_set_16_of_63_builder),
    ("Encode/Table/SingleSet/16_of_63", benchmark_table_single_set_16_of_63_encode),
    ("Decode/Table/SingleSet/16_of_63", benchmark_table_single_set_16_of_63_decode),
    
    ("Builder/Table/SingleSet/63_of_63", benchmark_table_single_set_63_of_63_builder),
    ("Encode/Table/SingleSet/63_of_63", benchmark_table_single_set_63_of_63_encode),
    ("Decode/Table/SingleSet/63_of_63", benchmark_table_single_set_63_of_63_decode),
    
];

const _V2_CONTEXT: &Context = &Context { wire_format_version: WireFormatVersion::V2 };


fn benchmark_table_single_set_1_of_63_builder(b: &mut Bencher) {
    b.iter(
        || {
            black_box(test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field1: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } });
        },
    );
}

fn benchmark_table_single_set_1_of_63_encode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field1: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } }
        },
        |value| {
            with_tls_encode_buf(|bytes, handles| {
                Encoder::encode_with_context(_V2_CONTEXT, bytes, handles, value).unwrap();
            })
        },
        BatchSize::SmallInput,
    );
}

fn benchmark_table_single_set_1_of_63_decode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            let mut bytes = Vec::<u8>::new();
            let mut handles = Vec::<HandleDisposition<'static>>::new();
            let original_value = &mut test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field1: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } };
            Encoder::encode_with_context(_V2_CONTEXT, &mut bytes, &mut handles, original_value).unwrap();
            let handle_infos = fidl::encoding::convert_handle_dispositions_to_infos(handles).unwrap();
            (bytes, handle_infos, ManuallyDrop::new(test_benchmarkfidltable::Table63Struct::new_empty()))
        },
        |(bytes, handle_infos, manually_drop_value)| {
            // Cast &mut ManuallyDrop<T> to &mut T.
            let value : &mut test_benchmarkfidltable::Table63Struct = unsafe { std::mem::transmute(manually_drop_value as *mut _) };
            Decoder::decode_with_context(_V2_CONTEXT, bytes, handle_infos, value).unwrap();
            // Count the drop time in the benchmark.
            unsafe { ManuallyDrop::drop(manually_drop_value) };
        },
        BatchSize::SmallInput,
    );
}


fn benchmark_table_single_set_16_of_63_builder(b: &mut Bencher) {
    b.iter(
        || {
            black_box(test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field16: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } });
        },
    );
}

fn benchmark_table_single_set_16_of_63_encode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field16: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } }
        },
        |value| {
            with_tls_encode_buf(|bytes, handles| {
                Encoder::encode_with_context(_V2_CONTEXT, bytes, handles, value).unwrap();
            })
        },
        BatchSize::SmallInput,
    );
}

fn benchmark_table_single_set_16_of_63_decode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            let mut bytes = Vec::<u8>::new();
            let mut handles = Vec::<HandleDisposition<'static>>::new();
            let original_value = &mut test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field16: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } };
            Encoder::encode_with_context(_V2_CONTEXT, &mut bytes, &mut handles, original_value).unwrap();
            let handle_infos = fidl::encoding::convert_handle_dispositions_to_infos(handles).unwrap();
            (bytes, handle_infos, ManuallyDrop::new(test_benchmarkfidltable::Table63Struct::new_empty()))
        },
        |(bytes, handle_infos, manually_drop_value)| {
            // Cast &mut ManuallyDrop<T> to &mut T.
            let value : &mut test_benchmarkfidltable::Table63Struct = unsafe { std::mem::transmute(manually_drop_value as *mut _) };
            Decoder::decode_with_context(_V2_CONTEXT, bytes, handle_infos, value).unwrap();
            // Count the drop time in the benchmark.
            unsafe { ManuallyDrop::drop(manually_drop_value) };
        },
        BatchSize::SmallInput,
    );
}


fn benchmark_table_single_set_63_of_63_builder(b: &mut Bencher) {
    b.iter(
        || {
            black_box(test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field63: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } });
        },
    );
}

fn benchmark_table_single_set_63_of_63_encode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field63: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } }
        },
        |value| {
            with_tls_encode_buf(|bytes, handles| {
                Encoder::encode_with_context(_V2_CONTEXT, bytes, handles, value).unwrap();
            })
        },
        BatchSize::SmallInput,
    );
}

fn benchmark_table_single_set_63_of_63_decode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            let mut bytes = Vec::<u8>::new();
            let mut handles = Vec::<HandleDisposition<'static>>::new();
            let original_value = &mut test_benchmarkfidltable::Table63Struct { value: test_benchmarkfidltable::Table63 { field63: Some(1u8), ..test_benchmarkfidltable::Table63::EMPTY } };
            Encoder::encode_with_context(_V2_CONTEXT, &mut bytes, &mut handles, original_value).unwrap();
            let handle_infos = fidl::encoding::convert_handle_dispositions_to_infos(handles).unwrap();
            (bytes, handle_infos, ManuallyDrop::new(test_benchmarkfidltable::Table63Struct::new_empty()))
        },
        |(bytes, handle_infos, manually_drop_value)| {
            // Cast &mut ManuallyDrop<T> to &mut T.
            let value : &mut test_benchmarkfidltable::Table63Struct = unsafe { std::mem::transmute(manually_drop_value as *mut _) };
            Decoder::decode_with_context(_V2_CONTEXT, bytes, handle_infos, value).unwrap();
            // Count the drop time in the benchmark.
            unsafe { ManuallyDrop::drop(manually_drop_value) };
        },
        BatchSize::SmallInput,
    );
}