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_benchmarkfidloptionaluniontree as test_benchmarkfidloptionaluniontree,
    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/OptionalUnionTree/Depth8", benchmark_optional_union_tree_depth8_builder),
    ("Encode/OptionalUnionTree/Depth8", benchmark_optional_union_tree_depth8_encode),
    ("Decode/OptionalUnionTree/Depth8", benchmark_optional_union_tree_depth8_decode),
    
    ("Builder/OptionalUnionTree/Depth6", benchmark_optional_union_tree_depth6_builder),
    ("Encode/OptionalUnionTree/Depth6", benchmark_optional_union_tree_depth6_encode),
    ("Decode/OptionalUnionTree/Depth6", benchmark_optional_union_tree_depth6_decode),
    
    ("Builder/OptionalUnionTree/Depth4", benchmark_optional_union_tree_depth4_builder),
    ("Encode/OptionalUnionTree/Depth4", benchmark_optional_union_tree_depth4_encode),
    ("Decode/OptionalUnionTree/Depth4", benchmark_optional_union_tree_depth4_decode),
    
];

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


fn benchmark_optional_union_tree_depth8_builder(b: &mut Bencher) {
    b.iter(
        || {
            black_box(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) }))) }))) }))) }))) });
        },
    );
}

fn benchmark_optional_union_tree_depth8_encode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) }))) }))) }))) }))) }
        },
        |value| {
            with_tls_encode_buf(|bytes, handles| {
                Encoder::encode_with_context(_V2_CONTEXT, bytes, handles, value).unwrap();
            })
        },
        BatchSize::SmallInput,
    );
}

fn benchmark_optional_union_tree_depth8_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_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) }))) }))) }))) }))) };
            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_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper::new_empty()))
        },
        |(bytes, handle_infos, manually_drop_value)| {
            // Cast &mut ManuallyDrop<T> to &mut T.
            let value : &mut test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper = 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_optional_union_tree_depth6_builder(b: &mut Bencher) {
    b.iter(
        || {
            black_box(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) }))) }))) });
        },
    );
}

fn benchmark_optional_union_tree_depth6_encode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) }))) }))) }
        },
        |value| {
            with_tls_encode_buf(|bytes, handles| {
                Encoder::encode_with_context(_V2_CONTEXT, bytes, handles, value).unwrap();
            })
        },
        BatchSize::SmallInput,
    );
}

fn benchmark_optional_union_tree_depth6_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_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) }))) }))) };
            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_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper::new_empty()))
        },
        |(bytes, handle_infos, manually_drop_value)| {
            // Cast &mut ManuallyDrop<T> to &mut T.
            let value : &mut test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper = 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_optional_union_tree_depth4_builder(b: &mut Bencher) {
    b.iter(
        || {
            black_box(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) });
        },
    );
}

fn benchmark_optional_union_tree_depth4_encode(b: &mut Bencher) {
    b.iter_batched_ref(
        || {
            test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) }
        },
        |value| {
            with_tls_encode_buf(|bytes, handles| {
                Encoder::encode_with_context(_V2_CONTEXT, bytes, handles, value).unwrap();
            })
        },
        BatchSize::SmallInput,
    );
}

fn benchmark_optional_union_tree_depth4_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_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: Some(Box::new(test_benchmarkfidloptionaluniontree::OptionalUnionTree::S(test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper { d: None }))) }))) }))) }))) };
            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_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper::new_empty()))
        },
        |(bytes, handle_infos, manually_drop_value)| {
            // Cast &mut ManuallyDrop<T> to &mut T.
            let value : &mut test_benchmarkfidloptionaluniontree::OptionalUnionTreeWrapper = 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,
    );
}