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
|
#include "upbc/message_layout.h"
#include "google/protobuf/descriptor.pb.h"
namespace upbc {
namespace protobuf = ::google::protobuf;
static int64_t DivRoundUp(int64_t a, int64_t b) {
ABSL_ASSERT(a >= 0);
ABSL_ASSERT(b > 0);
return (a + b - 1) / b;
}
MessageLayout::Size MessageLayout::Place(
MessageLayout::SizeAndAlign size_and_align) {
Size offset = size_;
offset.AlignUp(size_and_align.align);
size_ = offset;
size_.Add(size_and_align.size);
//maxalign_.MaxFrom(size_and_align.align);
maxalign_.MaxFrom(size_and_align.size);
return offset;
}
bool MessageLayout::HasHasbit(const protobuf::FieldDescriptor* field) {
return field->file()->syntax() == protobuf::FileDescriptor::SYNTAX_PROTO2 &&
field->label() != protobuf::FieldDescriptor::LABEL_REPEATED &&
!field->containing_oneof() &&
!field->containing_type()->options().map_entry();
}
MessageLayout::SizeAndAlign MessageLayout::SizeOf(
const protobuf::FieldDescriptor* field) {
if (field->is_repeated()) {
return {{4, 8}, {4, 8}}; // Pointer to array object.
} else {
return SizeOfUnwrapped(field);
}
}
MessageLayout::SizeAndAlign MessageLayout::SizeOfUnwrapped(
const protobuf::FieldDescriptor* field) {
switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
return {{4, 8}, {4, 8}}; // Pointer to message.
case protobuf::FieldDescriptor::CPPTYPE_STRING:
return {{8, 16}, {4, 8}}; // upb_strview
case protobuf::FieldDescriptor::CPPTYPE_BOOL:
return {{1, 1}, {1, 1}};
case protobuf::FieldDescriptor::CPPTYPE_FLOAT:
case protobuf::FieldDescriptor::CPPTYPE_INT32:
case protobuf::FieldDescriptor::CPPTYPE_UINT32:
return {{4, 4}, {4, 4}};
default:
return {{8, 8}, {8, 8}};
}
}
int64_t MessageLayout::FieldLayoutRank(const protobuf::FieldDescriptor* field) {
// Order:
// 1, 2, 3. primitive fields (8, 4, 1 byte)
// 4. string fields
// 5. submessage fields
// 6. repeated fields
//
// This has the following nice properties:
//
// 1. padding alignment is (nearly) minimized.
// 2. fields that might have defaults (1-4) are segregated
// from fields that are always zero-initialized (5-7).
//
// We skip oneof fields, because they are emitted in a separate pass.
int64_t rank;
if (field->containing_oneof()) {
fprintf(stderr, "shouldn't have oneofs here.\n");
abort();
} else if (field->label() == protobuf::FieldDescriptor::LABEL_REPEATED) {
rank = 6;
} else {
switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
rank = 5;
break;
case protobuf::FieldDescriptor::CPPTYPE_STRING:
rank = 4;
break;
case protobuf::FieldDescriptor::CPPTYPE_BOOL:
rank = 3;
break;
case protobuf::FieldDescriptor::CPPTYPE_FLOAT:
case protobuf::FieldDescriptor::CPPTYPE_INT32:
case protobuf::FieldDescriptor::CPPTYPE_UINT32:
rank = 2;
break;
default:
rank = 1;
break;
}
}
// Break ties with field number.
return (rank << 29) | field->number();
}
void MessageLayout::ComputeLayout(const protobuf::Descriptor* descriptor) {
size_ = Size{0, 0};
maxalign_ = Size{0, 0};
if (descriptor->options().map_entry()) {
// Map entries aren't actually stored, they are only used during parsing.
// For parsing, it helps a lot if all map entry messages have the same
// layout.
SizeAndAlign size{{8, 16}, {4, 8}}; // upb_strview
field_offsets_[descriptor->FindFieldByNumber(1)] = Place(size);
field_offsets_[descriptor->FindFieldByNumber(2)] = Place(size);
} else {
PlaceNonOneofFields(descriptor);
PlaceOneofFields(descriptor);
}
// Align overall size up to max size.
size_.AlignUp(maxalign_);
}
void MessageLayout::PlaceNonOneofFields(
const protobuf::Descriptor* descriptor) {
std::vector<const protobuf::FieldDescriptor*> field_order;
for (int i = 0; i < descriptor->field_count(); i++) {
const protobuf::FieldDescriptor* field = descriptor->field(i);
if (!field->containing_oneof()) {
field_order.push_back(descriptor->field(i));
}
}
std::sort(field_order.begin(), field_order.end(),
[](const protobuf::FieldDescriptor* a,
const protobuf::FieldDescriptor* b) {
return FieldLayoutRank(a) < FieldLayoutRank(b);
});
// Place/count hasbits.
int hasbit_count = 0;
for (auto field : field_order) {
if (HasHasbit(field)) {
// We don't use hasbit 0, so that 0 can indicate "no presence" in the
// table. This wastes one hasbit, but we don't worry about it for now.
hasbit_indexes_[field] = ++hasbit_count;
}
}
// Place hasbits at the beginning.
int64_t hasbit_bytes = DivRoundUp(hasbit_count, 8);
Place(SizeAndAlign{{hasbit_bytes, hasbit_bytes}, {1, 1}});
// Place non-oneof fields.
for (auto field : field_order) {
field_offsets_[field] = Place(SizeOf(field));
}
}
void MessageLayout::PlaceOneofFields(const protobuf::Descriptor* descriptor) {
std::vector<const protobuf::OneofDescriptor*> oneof_order;
for (int i = 0; i < descriptor->oneof_decl_count(); i++) {
oneof_order.push_back(descriptor->oneof_decl(i));
}
std::sort(oneof_order.begin(), oneof_order.end(),
[](const protobuf::OneofDescriptor* a,
const protobuf::OneofDescriptor* b) {
return a->full_name() < b->full_name();
});
for (auto oneof : oneof_order) {
SizeAndAlign oneof_maxsize{{0, 0}, {0, 0}};
// Calculate max size.
for (int i = 0; i < oneof->field_count(); i++) {
oneof_maxsize.MaxFrom(SizeOf(oneof->field(i)));
}
// Place discriminator enum and data.
Size data = Place(oneof_maxsize);
Size discriminator = Place(SizeAndAlign{{4, 4}, {4, 4}});
oneof_case_offsets_[oneof] = discriminator;
for (int i = 0; i < oneof->field_count(); i++) {
field_offsets_[oneof->field(i)] = data;
}
}
}
} // namespace upbc
|