2 #ifndef UPBC_MESSAGE_LAYOUT_H
3 #define UPBC_MESSAGE_LAYOUT_H
5 #include "absl/base/macros.h"
6 #include "absl/container/flat_hash_map.h"
7 #include "google/protobuf/descriptor.h"
14 void Add(const Size& other) {
15 size32 += other.size32;
16 size64 += other.size64;
19 void MaxFrom(const Size& other) {
20 size32 = std::max(size32, other.size32);
21 size64 = std::max(size64, other.size64);
24 void AlignUp(const Size& align) {
25 size32 = Align(size32, align.size32);
26 size64 = Align(size64, align.size64);
37 void MaxFrom(const SizeAndAlign& other) {
38 size.MaxFrom(other.size);
39 align.MaxFrom(other.align);
43 MessageLayout(const google::protobuf::Descriptor* descriptor) {
44 ComputeLayout(descriptor);
47 Size GetFieldOffset(const google::protobuf::FieldDescriptor* field) const {
48 return GetMapValue(field_offsets_, field);
51 Size GetOneofCaseOffset(
52 const google::protobuf::OneofDescriptor* oneof) const {
53 return GetMapValue(oneof_case_offsets_, oneof);
56 int GetHasbitIndex(const google::protobuf::FieldDescriptor* field) const {
57 return GetMapValue(hasbit_indexes_, field);
60 Size message_size() const { return size_; }
62 static bool HasHasbit(const google::protobuf::FieldDescriptor* field);
63 static SizeAndAlign SizeOfUnwrapped(
64 const google::protobuf::FieldDescriptor* field);
67 void ComputeLayout(const google::protobuf::Descriptor* descriptor);
68 void PlaceNonOneofFields(const google::protobuf::Descriptor* descriptor);
69 void PlaceOneofFields(const google::protobuf::Descriptor* descriptor);
70 Size Place(SizeAndAlign size_and_align);
72 template <class K, class V>
73 static V GetMapValue(const absl::flat_hash_map<K, V>& map, K key) {
74 auto iter = map.find(key);
75 if (iter == map.end()) {
76 fprintf(stderr, "No value for field.\n");
82 static bool IsPowerOfTwo(size_t val) {
83 return (val & (val - 1)) == 0;
86 static size_t Align(size_t val, size_t align) {
87 ABSL_ASSERT(IsPowerOfTwo(align));
88 return (val + align - 1) & ~(align - 1);
91 static SizeAndAlign SizeOf(const google::protobuf::FieldDescriptor* field);
92 static int64_t FieldLayoutRank(
93 const google::protobuf::FieldDescriptor* field);
95 absl::flat_hash_map<const google::protobuf::FieldDescriptor*, Size>
97 absl::flat_hash_map<const google::protobuf::FieldDescriptor*, int>
99 absl::flat_hash_map<const google::protobuf::OneofDescriptor*, Size>
105 // Returns fields in order of "hotness", eg. how frequently they appear in
106 // serialized payloads. Ideally this will use a profile. When we don't have
107 // that, we assume that fields with smaller numbers are used more frequently.
108 inline std::vector<const google::protobuf::FieldDescriptor*> FieldHotnessOrder(
109 const google::protobuf::Descriptor* message) {
110 std::vector<const google::protobuf::FieldDescriptor*> fields;
111 for (int i = 0; i < message->field_count(); i++) {
112 fields.push_back(message->field(i));
114 std::sort(fields.begin(), fields.end(),
115 [](const google::protobuf::FieldDescriptor* a,
116 const google::protobuf::FieldDescriptor* b) {
117 return a->number() < b->number();
124 #endif // UPBC_MESSAGE_LAYOUT_H