forked from protocolbuffers/upb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_layout.h
150 lines (126 loc) · 5.06 KB
/
message_layout.h
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
/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google LLC nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UPBC_MESSAGE_LAYOUT_H
#define UPBC_MESSAGE_LAYOUT_H
#include "absl/base/macros.h"
#include "absl/container/flat_hash_map.h"
#include "google/protobuf/descriptor.h"
namespace upbc {
class MessageLayout {
public:
struct Size {
void Add(const Size& other) {
size32 += other.size32;
size64 += other.size64;
}
void MaxFrom(const Size& other) {
size32 = std::max(size32, other.size32);
size64 = std::max(size64, other.size64);
}
void AlignUp(const Size& align) {
size32 = Align(size32, align.size32);
size64 = Align(size64, align.size64);
}
int64_t size32;
int64_t size64;
};
struct SizeAndAlign {
Size size;
Size align;
void MaxFrom(const SizeAndAlign& other) {
size.MaxFrom(other.size);
align.MaxFrom(other.align);
}
};
MessageLayout(const google::protobuf::Descriptor* descriptor) {
ComputeLayout(descriptor);
}
Size GetFieldOffset(const google::protobuf::FieldDescriptor* field) const {
return GetMapValue(field_offsets_, field);
}
Size GetOneofCaseOffset(
const google::protobuf::OneofDescriptor* oneof) const {
return GetMapValue(oneof_case_offsets_, oneof);
}
int GetHasbitIndex(const google::protobuf::FieldDescriptor* field) const {
return GetMapValue(hasbit_indexes_, field);
}
Size message_size() const { return size_; }
static bool HasHasbit(const google::protobuf::FieldDescriptor* field);
static SizeAndAlign SizeOfUnwrapped(
const google::protobuf::FieldDescriptor* field);
private:
void ComputeLayout(const google::protobuf::Descriptor* descriptor);
void PlaceNonOneofFields(const google::protobuf::Descriptor* descriptor);
void PlaceOneofFields(const google::protobuf::Descriptor* descriptor);
Size Place(SizeAndAlign size_and_align);
template <class K, class V>
static V GetMapValue(const absl::flat_hash_map<K, V>& map, K key) {
auto iter = map.find(key);
if (iter == map.end()) {
fprintf(stderr, "No value for field.\n");
abort();
}
return iter->second;
}
static bool IsPowerOfTwo(size_t val) {
return (val & (val - 1)) == 0;
}
static size_t Align(size_t val, size_t align) {
ABSL_ASSERT(IsPowerOfTwo(align));
return (val + align - 1) & ~(align - 1);
}
static SizeAndAlign SizeOf(const google::protobuf::FieldDescriptor* field);
static int64_t FieldLayoutRank(
const google::protobuf::FieldDescriptor* field);
absl::flat_hash_map<const google::protobuf::FieldDescriptor*, Size>
field_offsets_;
absl::flat_hash_map<const google::protobuf::FieldDescriptor*, int>
hasbit_indexes_;
absl::flat_hash_map<const google::protobuf::OneofDescriptor*, Size>
oneof_case_offsets_;
Size maxalign_;
Size size_;
};
// Returns fields in order of "hotness", eg. how frequently they appear in
// serialized payloads. Ideally this will use a profile. When we don't have
// that, we assume that fields with smaller numbers are used more frequently.
inline std::vector<const google::protobuf::FieldDescriptor*> FieldHotnessOrder(
const google::protobuf::Descriptor* message) {
std::vector<const google::protobuf::FieldDescriptor*> fields;
for (int i = 0; i < message->field_count(); i++) {
fields.push_back(message->field(i));
}
std::sort(fields.begin(), fields.end(),
[](const google::protobuf::FieldDescriptor* a,
const google::protobuf::FieldDescriptor* b) {
return a->number() < b->number();
});
return fields;
}
} // namespace upbc
#endif // UPBC_MESSAGE_LAYOUT_H