aboutsummaryrefslogtreecommitdiff
path: root/pw_protobuf/size_report/oneof_codegen_comparison.cc
blob: 27317db0d2f50f5805a5e6c92ae7ed475a8db861 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "proto_bloat.h"
#include "pw_bloat/bloat_this_binary.h"
#include "pw_protobuf/decoder.h"
#include "pw_protobuf/encoder.h"
#include "pw_protobuf/stream_decoder.h"
#include "pw_protobuf_test_protos/size_report.pwpb.h"
#include "pw_result/result.h"
#include "pw_status/status.h"

#ifndef _PW_PROTOBUF_SIZE_REPORT_NO_CODEGEN
#define _PW_PROTOBUF_SIZE_REPORT_NO_CODEGEN 0
#endif  // _PW_PROTOBUF_SIZE_REPORT_NO_CODEGEN

#ifndef _PW_PROTOBUF_SIZE_REPORT_WIRE_FORMAT
#define _PW_PROTOBUF_SIZE_REPORT_WIRE_FORMAT 0
#endif  // _PW_PROTOBUF_SIZE_REPORT_WIRE_FORMAT

#ifndef _PW_PROTOBUF_SIZE_REPORT_MESSAGE
#define _PW_PROTOBUF_SIZE_REPORT_MESSAGE 0
#endif  // _PW_PROTOBUF_SIZE_REPORT_MESSAGE

namespace pw::protobuf_size_report {
namespace {

namespace ItemInfo = pwpb::ItemInfo;
namespace ResponseInfo = pwpb::ResponseInfo;

template <typename T>
PW_NO_INLINE void ConsumeValue(T val) {
  [[maybe_unused]] volatile T no_optimize = val;
}

#if _PW_PROTOBUF_SIZE_REPORT_NO_CODEGEN

std::array<std::byte, ItemInfo::kMaxEncodedSizeBytes> encode_buffer;
pw::protobuf::MemoryEncoder encoder(encode_buffer);

PW_NO_INLINE void BasicEncode() {
  pw::Status status;
  volatile enum KeyType : uint32_t {
    NONE = 0,
    KEY_STRING = 1,
    KEY_TOKEN = 2,
  } which_key = KeyType::KEY_STRING;
  volatile bool has_timestamp = true;
  volatile bool has_has_value = false;
  if (which_key == KeyType::KEY_STRING) {
    encoder.WriteString(1, "test");
  } else if (which_key == KeyType::KEY_TOKEN) {
    encoder.WriteFixed32(2, 99999);
  }

  if (has_timestamp) {
    encoder.WriteInt64(3, 1663003467);
  }

  if (has_has_value) {
    encoder.WriteBool(4, true);
  }

  {
    pw::protobuf::StreamEncoder submessage_encoder =
        encoder.GetNestedEncoder(5);
    status.Update(submessage_encoder.WriteInt64(1, 0x5001DBADFEEDBEE5));
    status.Update(submessage_encoder.WriteInt32(2, 128));
    status.Update(submessage_encoder.WriteInt32(3, 2));
  }
  ConsumeValue(status);
}

std::array<std::byte, ItemInfo::kMaxEncodedSizeBytes> decode_buffer;
pw::protobuf::Decoder decoder(decode_buffer);

PW_NO_INLINE void DecodeItemInfo(pw::ConstByteSpan data) {
  pw::protobuf::Decoder submessage_decoder(data);
  while (submessage_decoder.Next().ok()) {
    switch (submessage_decoder.FieldNumber()) {
      case static_cast<uint32_t>(ItemInfo::Fields::kOffset): {
        uint64_t value;
        if (submessage_decoder.ReadUint64(&value).ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case static_cast<uint32_t>(ItemInfo::Fields::kSize): {
        uint32_t value;
        if (submessage_decoder.ReadUint32(&value).ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case static_cast<uint32_t>(ItemInfo::Fields::kAccessLevel): {
        uint32_t value;

        if (submessage_decoder.ReadUint32(&value).ok()) {
          ConsumeValue(value);
        }
        break;
      }
    }
  }
}

PW_NO_INLINE void BasicDecode() {
  volatile enum KeyType : uint32_t {
    NONE = 0,
    KEY_STRING = 1,
    KEY_TOKEN = 2,
  } which_key = KeyType::NONE;
  volatile bool has_timestamp = false;
  volatile bool has_has_value = false;

  while (decoder.Next().ok()) {
    switch (decoder.FieldNumber()) {
      case static_cast<uint32_t>(ResponseInfo::Fields::kKeyString): {
        which_key = KeyType::KEY_STRING;
        std::string_view value;
        if (decoder.ReadString(&value).ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case static_cast<uint32_t>(ResponseInfo::Fields::kKeyToken): {
        which_key = KeyType::KEY_TOKEN;
        uint32_t value;
        if (decoder.ReadUint32(&value).ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case static_cast<uint32_t>(ResponseInfo::Fields::kTimestamp): {
        uint64_t value;
        has_timestamp = true;
        if (decoder.ReadUint64(&value).ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case static_cast<uint32_t>(ResponseInfo::Fields::kHasValue): {
        bool value;
        has_has_value = true;
        if (decoder.ReadBool(&value).ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case static_cast<uint32_t>(ResponseInfo::Fields::kItemInfo): {
        pw::ConstByteSpan value;
        if (decoder.ReadBytes(&value).ok()) {
          DecodeItemInfo(value);
        }
        break;
      }
    }
  }
  ConsumeValue(which_key);
  ConsumeValue(has_timestamp);
  ConsumeValue(has_has_value);
}

#endif  // _PW_PROTOBUF_SIZE_REPORT_NO_CODEGEN

#if _PW_PROTOBUF_SIZE_REPORT_WIRE_FORMAT

std::array<std::byte, ResponseInfo::kMaxEncodedSizeBytes> encode_buffer;
ResponseInfo::MemoryEncoder encoder(encode_buffer);

PW_NO_INLINE void BasicEncode() {
  pw::Status status;
  volatile enum KeyType : uint32_t {
    NONE = 0,
    KEY_STRING = 1,
    KEY_TOKEN = 2,
  } which_key = KeyType::KEY_STRING;
  volatile bool has_timestamp = true;
  volatile bool has_has_value = false;
  if (which_key == KeyType::KEY_STRING) {
    encoder.WriteKeyString("test");
  } else if (which_key == KeyType::KEY_TOKEN) {
    encoder.WriteKeyToken(99999);
  }

  if (has_timestamp) {
    encoder.WriteTimestamp(1663003467);
  }

  if (has_has_value) {
    encoder.WriteHasValue(true);
  }

  {
    ItemInfo::StreamEncoder submessage_encoder = encoder.GetItemInfoEncoder();
    status.Update(submessage_encoder.WriteOffset(0x5001DBADFEEDBEE5));
    status.Update(submessage_encoder.WriteSize(128));
    status.Update(submessage_encoder.WriteAccessLevel(ItemInfo::Access::WRITE));
  }
  ConsumeValue(status);
}

std::array<std::byte, ResponseInfo::kMaxEncodedSizeBytes> decode_buffer;
pw::stream::MemoryReader reader(decode_buffer);
ResponseInfo::StreamDecoder decoder(reader);

PW_NO_INLINE void DecodeItemInfo(ItemInfo::StreamDecoder& submessage_decoder) {
  while (submessage_decoder.Next().ok()) {
    pw::Result<ItemInfo::Fields> field = submessage_decoder.Field();
    if (!field.ok()) {
      ConsumeValue(field.status());
      return;
    }

    switch (field.value()) {
      case ItemInfo::Fields::kOffset: {
        pw::Result<uint64_t> value = submessage_decoder.ReadOffset();
        if (value.ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case ItemInfo::Fields::kSize: {
        pw::Result<uint32_t> value = submessage_decoder.ReadSize();
        if (value.ok()) {
          ConsumeValue(value);
        }
        break;
      }
      case ItemInfo::Fields::kAccessLevel: {
        pw::Result<ItemInfo::Access> value =
            submessage_decoder.ReadAccessLevel();
        if (value.ok()) {
          ConsumeValue(value);
        }
        break;
      }
    }
  }
}

PW_NO_INLINE void BasicDecode() {
  volatile enum KeyType : uint32_t {
    NONE = 0,
    KEY_STRING = 1,
    KEY_TOKEN = 2,
  } which_key = KeyType::NONE;
  volatile bool has_timestamp = false;
  volatile bool has_has_value = false;

  while (decoder.Next().ok()) {
    while (decoder.Next().ok()) {
      pw::Result<ResponseInfo::Fields> field = decoder.Field();
      if (!field.ok()) {
        ConsumeValue(field.status());
        return;
      }

      switch (field.value()) {
        case ResponseInfo::Fields::kKeyString: {
          which_key = KeyType::KEY_STRING;
          std::array<char, 8> value;
          pw::StatusWithSize status = decoder.ReadKeyString(value);
          if (status.ok()) {
            ConsumeValue(pw::span(value));
          }
          break;
        }
        case ResponseInfo::Fields::kKeyToken: {
          which_key = KeyType::KEY_TOKEN;
          pw::Result<uint32_t> value = decoder.ReadKeyToken();
          if (value.ok()) {
            ConsumeValue(value);
          }
          break;
        }
        case ResponseInfo::Fields::kTimestamp: {
          has_timestamp = true;
          pw::Result<int64_t> value = decoder.ReadTimestamp();
          if (value.ok()) {
            ConsumeValue(value);
          }
          break;
        }
        case ResponseInfo::Fields::kHasValue: {
          has_has_value = true;
          pw::Result<bool> value = decoder.ReadHasValue();
          if (value.ok()) {
            ConsumeValue(value);
          }
          break;
        }
        case ResponseInfo::Fields::kItemInfo: {
          ItemInfo::StreamDecoder submessage_decoder =
              decoder.GetItemInfoDecoder();
          DecodeItemInfo(submessage_decoder);
          break;
        }
      }
    }
  }
  ConsumeValue(which_key);
  ConsumeValue(has_timestamp);
  ConsumeValue(has_has_value);
}
#endif  // _PW_PROTOBUF_SIZE_REPORT_WIRE_FORMAT

#if _PW_PROTOBUF_SIZE_REPORT_MESSAGE

ResponseInfo::Message message;

std::array<std::byte, ResponseInfo::kMaxEncodedSizeBytes> encode_buffer;
ResponseInfo::MemoryEncoder encoder(encode_buffer);

PW_NO_INLINE void BasicEncode() {
  volatile enum KeyType : uint32_t {
    NONE = 0,
    KEY_STRING = 1,
    KEY_TOKEN = 2,
  } which_key = KeyType::KEY_STRING;
  volatile bool has_timestamp = true;
  volatile bool has_has_value = false;
  if (which_key == KeyType::KEY_STRING) {
    message.key_string.SetEncoder(
        [](ResponseInfo::StreamEncoder& key_string_encoder) -> pw::Status {
          key_string_encoder.WriteKeyString("test");
          return pw::OkStatus();
        });
  } else if (which_key == KeyType::KEY_TOKEN) {
    message.key_token = 99999;
  }
  message.timestamp =
      has_timestamp ? std::optional<uint32_t>(1663003467) : std::nullopt;
  message.has_value = has_has_value ? std::optional<bool>(false) : std::nullopt;

  message.item_info.offset = 0x5001DBADFEEDBEE5;
  message.item_info.size = 128;
  message.item_info.access_level = ItemInfo::Access::WRITE;
  ConsumeValue(encoder.Write(message));
}

std::array<std::byte, ResponseInfo::kMaxEncodedSizeBytes> decode_buffer;
pw::stream::MemoryReader reader(decode_buffer);
ResponseInfo::StreamDecoder decoder(reader);

PW_NO_INLINE void BasicDecode() {
  volatile enum KeyType : uint32_t {
    NONE = 0,
    KEY_STRING = 1,
    KEY_TOKEN = 2,
  } which_key = KeyType::NONE;
  volatile bool has_timestamp = false;
  volatile bool has_has_value = false;
  if (pw::Status status = decoder.Read(message); status.ok()) {
    ConsumeValue(status);
    has_timestamp = message.timestamp.has_value();
    has_has_value = message.has_value.has_value();
  }
  ConsumeValue(which_key);
  ConsumeValue(has_timestamp);
  ConsumeValue(has_has_value);
}
#endif  // _PW_PROTOBUF_SIZE_REPORT_MESSAGE

}  // namespace
}  // namespace pw::protobuf_size_report

int main() {
  pw::bloat::BloatThisBinary();
  pw::protobuf_size_report::BloatWithBase();
  pw::protobuf_size_report::BloatWithEncoder();
  pw::protobuf_size_report::BloatWithStreamDecoder();
  pw::protobuf_size_report::BloatWithDecoder();
  pw::protobuf_size_report::BloatWithTableEncoder();
  pw::protobuf_size_report::BloatWithTableDecoder();
  pw::protobuf_size_report::BasicEncode();
  pw::protobuf_size_report::BasicDecode();
  return 0;
}