From de2c129362a89d7d9f027ea10a62a7fe27927400 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 17:35:15 -0800 Subject: [PATCH 1/9] First draft of mini-table building API. --- upb/mini_table.c | 937 +++++++++++++++++++++++++++++++++++++++++ upb/mini_table.h | 162 +++++++ upb/mini_table.hpp | 96 +++++ upb/mini_table_test.cc | 172 ++++++++ 4 files changed, 1367 insertions(+) create mode 100644 upb/mini_table.c create mode 100644 upb/mini_table.h create mode 100644 upb/mini_table.hpp create mode 100644 upb/mini_table_test.cc diff --git a/upb/mini_table.c b/upb/mini_table.c new file mode 100644 index 0000000000..4347b1515e --- /dev/null +++ b/upb/mini_table.c @@ -0,0 +1,937 @@ +/* + * 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. + */ + +#include "upb/mini_table.h" + +#include +#include + +#include "upb/msg_internal.h" +#include "upb/upb.h" + +// Must be last. +#include "upb/port_def.inc" + +typedef enum { + kUpb_EncodedType_Double = 0, + kUpb_EncodedType_Float = 1, + kUpb_EncodedType_Fixed32 = 2, + kUpb_EncodedType_Fixed64 = 3, + kUpb_EncodedType_SFixed32 = 4, + kUpb_EncodedType_SFixed64 = 5, + kUpb_EncodedType_Int32 = 6, + kUpb_EncodedType_UInt32 = 7, + kUpb_EncodedType_SInt32 = 8, + kUpb_EncodedType_Int64 = 9, + kUpb_EncodedType_UInt64 = 10, + kUpb_EncodedType_SInt64 = 11, + kUpb_EncodedType_Enum = 12, + kUpb_EncodedType_Bool = 13, + kUpb_EncodedType_Bytes = 14, + kUpb_EncodedType_String = 15, + kUpb_EncodedType_Group = 16, + kUpb_EncodedType_Message = 17, + + kUpb_EncodedType_RepeatedBase = 20, +} upb_EncodedType; + +typedef enum { + kUpb_EncodedFieldModifier_IsUnpacked = 1, + kUpb_EncodedFieldModifier_JspbString = 2, + // upb only. + kUpb_EncodedFieldModifier_IsProto3Singular = 4, + kUpb_EncodedFieldModifier_IsRequired = 8, +} upb_EncodedFieldModifier; + +enum { + kUpb_EncodedValue_MinField = ' ', + kUpb_EncodedValue_MaxField = 'K', + kUpb_EncodedValue_MinModifier = 'L', + kUpb_EncodedValue_MaxModifier = '[', + kUpb_EncodedValue_End = '^', + kUpb_EncodedValue_MinSkip = '_', + kUpb_EncodedValue_MaxSkip = '~', + kUpb_EncodedValue_OneofSeparator = '~', + kUpb_EncodedValue_FieldSeparator = '|', +}; + +char upb_ToBase92(int8_t ch) { + static const char kUpb_ToBase92[] = { + ' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', + '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', + 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', + 'Z', '[', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '{', '|', '}', '~', + }; + + assert(0 <= ch && ch < 92); + return kUpb_ToBase92[ch]; +} + +char upb_FromBase92(uint8_t ch) { + static const int8_t kUpb_FromBase92[] = { + 0, 1, -1, 2, 3, 4, 5, -1, 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, -1, 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, + }; + + if (' ' > ch || ch > '~') return -1; + return kUpb_FromBase92[ch - ' ']; +} + +/** upb_MtDataEncoder *********************************************************/ + +typedef struct { + char* buf_start; // Only for checking kUpb_MtDataEncoder_MinSize. + uint64_t msg_mod; + uint32_t last_field_num; + enum { + kUpb_OneofState_NotStarted, + kUpb_OneofState_StartedOneof, + kUpb_OneofState_EmittedOneofField, + } oneof_state; +} upb_MtDataEncoderInternal; + +static upb_MtDataEncoderInternal* upb_MtDataEncoder_GetInternal( + upb_MtDataEncoder* e, char* buf_start) { + upb_MtDataEncoderInternal* ret = (upb_MtDataEncoderInternal*)e->internal; + ret->buf_start = buf_start; + return ret; +} + +static char* upb_MtDataEncoder_Put(upb_MtDataEncoder* e, char* ptr, char ch) { + upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal; + UPB_ASSERT(ptr - in->buf_start < kUpb_MtDataEncoder_MinSize); + if (ptr == e->end) return NULL; + *ptr++ = upb_ToBase92(ch); + return ptr; +} + +static char* upb_MtDataEncoder_PutBase92Varint(upb_MtDataEncoder* e, char* ptr, + uint32_t val, int min, int max) { + int shift = _upb_Log2Ceiling(upb_FromBase92(max) - upb_FromBase92(min) + 1); + uint32_t mask = (1 << shift) - 1; + do { + uint32_t bits = val & mask; + ptr = upb_MtDataEncoder_Put(e, ptr, bits + upb_FromBase92(min)); + if (!ptr) return NULL; + val >>= shift; + } while (val); + return ptr; +} + +char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr, + uint64_t msg_mod) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + in->msg_mod = msg_mod; + in->last_field_num = 0; + in->oneof_state = kUpb_OneofState_NotStarted; + return ptr; +} + +char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr, + upb_FieldType type, uint32_t field_num, + uint64_t modifiers) { + static const char kUpb_TypeToEncoded[] = { + [kUpb_FieldType_Double] = kUpb_EncodedType_Double, + [kUpb_FieldType_Float] = kUpb_EncodedType_Float, + [kUpb_FieldType_Int64] = kUpb_EncodedType_Int64, + [kUpb_FieldType_UInt64] = kUpb_EncodedType_UInt64, + [kUpb_FieldType_Int32] = kUpb_EncodedType_Int32, + [kUpb_FieldType_Fixed64] = kUpb_EncodedType_Fixed64, + [kUpb_FieldType_Fixed32] = kUpb_EncodedType_Fixed32, + [kUpb_FieldType_Bool] = kUpb_EncodedType_Bool, + [kUpb_FieldType_String] = kUpb_EncodedType_String, + [kUpb_FieldType_Group] = kUpb_EncodedType_Group, + [kUpb_FieldType_Message] = kUpb_EncodedType_Message, + [kUpb_FieldType_Bytes] = kUpb_EncodedType_Bytes, + [kUpb_FieldType_UInt32] = kUpb_EncodedType_UInt32, + [kUpb_FieldType_Enum] = kUpb_EncodedType_Enum, + [kUpb_FieldType_SFixed32] = kUpb_EncodedType_SFixed32, + [kUpb_FieldType_SFixed64] = kUpb_EncodedType_SFixed64, + [kUpb_FieldType_SInt32] = kUpb_EncodedType_SInt32, + [kUpb_FieldType_SInt64] = kUpb_EncodedType_SInt64, + }; + + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + if (field_num <= in->last_field_num) return NULL; + if (in->last_field_num + 1 != field_num) { + // Put skip. + assert(field_num > in->last_field_num); + uint32_t skip = field_num - in->last_field_num; + ptr = upb_MtDataEncoder_PutBase92Varint( + e, ptr, skip, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip); + if (!ptr) return NULL; + } + in->last_field_num = field_num; + + // Put field type. + int encoded_type = kUpb_TypeToEncoded[type]; + if (modifiers & kUpb_FieldModifier_IsRepeated) { + encoded_type += kUpb_EncodedType_RepeatedBase; + } + ptr = upb_MtDataEncoder_Put(e, ptr, encoded_type); + if (!ptr) return NULL; + + uint32_t encoded_modifiers = 0; + if (modifiers & kUpb_FieldModifier_IsProto3Singular) { + encoded_modifiers |= kUpb_EncodedFieldModifier_IsProto3Singular; + } + if (modifiers & kUpb_FieldModifier_IsRequired) { + encoded_modifiers |= kUpb_EncodedFieldModifier_IsRequired; + } + if ((modifiers & kUpb_FieldModifier_IsPacked) != + (in->msg_mod & kUpb_MessageModifier_DefaultIsPacked)) { + encoded_modifiers |= kUpb_EncodedFieldModifier_IsUnpacked; + } + if (encoded_modifiers) { + ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, encoded_modifiers, + kUpb_EncodedValue_MinModifier, + kUpb_EncodedValue_MaxModifier); + } + return ptr; +} + +char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + if (in->oneof_state == kUpb_OneofState_NotStarted) { + ptr = upb_MtDataEncoder_Put(e, ptr, upb_FromBase92(kUpb_EncodedValue_End)); + } else { + ptr = upb_MtDataEncoder_Put( + e, ptr, upb_FromBase92(kUpb_EncodedValue_OneofSeparator)); + } + in->oneof_state = kUpb_OneofState_StartedOneof; + return ptr; +} + +char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr, + uint32_t field_num) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + if (in->oneof_state == kUpb_OneofState_EmittedOneofField) { + ptr = upb_MtDataEncoder_Put( + e, ptr, upb_FromBase92(kUpb_EncodedValue_FieldSeparator)); + if (!ptr) return NULL; + } + ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, field_num, upb_ToBase92(0), + upb_ToBase92(63)); + in->oneof_state = kUpb_OneofState_EmittedOneofField; + return ptr; +} + +const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber( + const upb_MiniTable* table, uint32_t number) { + int n = table->field_count; + for (int i = 0; i < n; i++) { + if (table->fields[i].number == number) { + return &table->fields[i]; + } + } + return NULL; +} + +/** Data decoder **************************************************************/ + +typedef enum { + kUpb_LayoutItemType_Field, // Non-oneof field data. + kUpb_LayoutItemType_OneofField, // Oneof field data. + kUpb_LayoutItemType_OneofCase, // Oneof case. +} upb_LayoutItemType; + +#define kUpb_LayoutItem_IndexSentinel ((uint16_t)-1) + +typedef struct { + // Index of the corresponding field. When this is a oneof field, the field's + // offset will be the index of the next field in a linked list. + uint16_t field_index; + upb_FieldRep rep; + upb_LayoutItemType type; + + // Used for temporary storage while assigning offsets (internal only). + uint16_t offset; +} upb_LayoutItem; + +typedef struct { + upb_LayoutItem* data; + size_t size; + size_t capacity; +} upb_LayoutItemVector; + +typedef struct { + const char* end; + upb_MiniTable* table; + upb_MiniTable_Field* fields; + upb_MiniTablePlatform platform; + upb_LayoutItemVector vec; + upb_Arena* arena; + upb_Status* status; + jmp_buf err; +} upb_MtDecoder; + +UPB_PRINTF(2, 3) +UPB_NORETURN static void upb_MtDecoder_ErrorFormat(upb_MtDecoder* d, + const char* fmt, ...) { + va_list argp; + upb_Status_SetErrorMessage(d->status, "Error building mini table: "); + va_start(argp, fmt); + upb_Status_VAppendErrorFormat(d->status, fmt, argp); + va_end(argp); + UPB_LONGJMP(d->err, 1); +} + +static void upb_MtDecoder_CheckOutOfMemory(upb_MtDecoder* d, const void* ptr) { + if (!ptr) upb_MtDecoder_ErrorFormat(d, "Out of memory"); +} + +// In each field's offset, we temporarily store a presence classifier: +enum PresenceClass { + kNoPresence = 0, + kHasbitPresence = 1, + kRequiredPresence = 2, + kOneofBase = 3, + // Negative values refer to a specific oneof with that number. Positive + // values >= kOneofBase indicate that this field is in a oneof, and specify + // the next field in this oneof's linked list. +}; + +static const char* upb_MiniTable_DecodeBase92Varint(upb_MtDecoder* d, + const char* ptr, + char first_ch, uint8_t min, + uint8_t max, + uint32_t* out_val) { + uint32_t val = 0; + uint32_t shift = 0; + char ch = first_ch; + while (1) { + uint32_t bits = upb_FromBase92(ch) - upb_FromBase92(min); + val |= bits << shift; + if (ptr == d->end || *ptr < min || max < *ptr) { + *out_val = val; + return ptr; + } + ch = *ptr++; + shift += _upb_Log2Ceiling(upb_FromBase92(max) - upb_FromBase92(min)); + } +} + +static bool upb_MiniTable_HasSub(char type, uint64_t msg_modifiers) { + return type == kUpb_EncodedType_Message || type == kUpb_EncodedType_Group || + (type == kUpb_EncodedType_Enum && + (msg_modifiers & kUpb_MessageModifier_HasClosedEnums)); +} + +static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch, + upb_MiniTable_Field* field, + uint64_t msg_modifiers, + uint32_t* sub_count) { + static const char kUpb_EncodedToFieldRep[] = { + [kUpb_EncodedType_Double] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_Float] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_Int64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_UInt64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_Int32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_Fixed64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_Fixed32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_Bool] = kUpb_FieldRep_1Byte, + [kUpb_EncodedType_String] = kUpb_FieldRep_StringView, + [kUpb_EncodedType_Group] = kUpb_FieldRep_Pointer, + [kUpb_EncodedType_Message] = kUpb_FieldRep_Pointer, + [kUpb_EncodedType_Bytes] = kUpb_FieldRep_StringView, + [kUpb_EncodedType_UInt32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_Enum] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_SFixed32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_SFixed64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_SInt32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_SInt64] = kUpb_FieldRep_8Byte, + }; + + static const char kUpb_EncodedToType[] = { + [kUpb_EncodedType_Double] = kUpb_FieldType_Double, + [kUpb_EncodedType_Float] = kUpb_FieldType_Float, + [kUpb_EncodedType_Int64] = kUpb_FieldType_Int64, + [kUpb_EncodedType_UInt64] = kUpb_FieldType_UInt64, + [kUpb_EncodedType_Int32] = kUpb_FieldType_Int32, + [kUpb_EncodedType_Fixed64] = kUpb_FieldType_Fixed64, + [kUpb_EncodedType_Fixed32] = kUpb_FieldType_Fixed32, + [kUpb_EncodedType_Bool] = kUpb_FieldType_Bool, + [kUpb_EncodedType_String] = kUpb_FieldType_String, + [kUpb_EncodedType_Group] = kUpb_FieldType_Group, + [kUpb_EncodedType_Message] = kUpb_FieldType_Message, + [kUpb_EncodedType_Bytes] = kUpb_FieldType_Bytes, + [kUpb_EncodedType_UInt32] = kUpb_FieldType_UInt32, + [kUpb_EncodedType_Enum] = kUpb_FieldType_Enum, + [kUpb_EncodedType_SFixed32] = kUpb_FieldType_SFixed32, + [kUpb_EncodedType_SFixed64] = kUpb_FieldType_SFixed64, + [kUpb_EncodedType_SInt32] = kUpb_FieldType_SInt32, + [kUpb_EncodedType_SInt64] = kUpb_FieldType_SInt64, + }; + + int8_t type = upb_FromBase92(ch); + if (ch >= upb_ToBase92(kUpb_EncodedType_RepeatedBase)) { + type -= kUpb_EncodedType_RepeatedBase; + field->mode = kUpb_FieldMode_Array; + field->mode |= kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift; + field->offset = kNoPresence; + } else { + field->mode = kUpb_FieldMode_Scalar; + field->mode |= kUpb_EncodedToFieldRep[type] << kUpb_FieldRep_Shift; + field->offset = kHasbitPresence; + } + if (type >= 18) { + upb_MtDecoder_ErrorFormat(d, "Invalid field type: %d", (int)type); + UPB_UNREACHABLE(); + } + field->descriptortype = kUpb_EncodedToType[type]; + if (upb_MiniTable_HasSub(type, msg_modifiers)) { + field->submsg_index = sub_count ? (*sub_count)++ : 0; + } +} + +static void upb_MtDecoder_ModifyField(upb_MtDecoder* d, uint32_t mod, + upb_MiniTable_Field* field) { + if (mod & kUpb_EncodedFieldModifier_IsUnpacked) { + field->mode &= ~upb_LabelFlags_IsPacked; + } else { + field->mode |= upb_LabelFlags_IsPacked; + } + + bool singular = mod & kUpb_EncodedFieldModifier_IsProto3Singular; + bool required = mod & kUpb_EncodedFieldModifier_IsRequired; + + // Validate. + if ((singular || required) && field->offset != kHasbitPresence) { + upb_MtDecoder_ErrorFormat( + d, "Invalid modifier(s) for repeated field %" PRIu32, field->number); + UPB_UNREACHABLE(); + } + if (singular && required) { + upb_MtDecoder_ErrorFormat( + d, "Field %" PRIu32 " cannot be both singular and required", + field->number); + UPB_UNREACHABLE(); + } + + if (singular) field->offset = kNoPresence; + if (required) field->offset = kRequiredPresence; +} + +static void upb_MtDecoder_PushItem(upb_MtDecoder* d, upb_LayoutItem item) { + if (d->vec.size == d->vec.capacity) { + size_t new_cap = UPB_MAX(8, d->vec.size * 2); + d->vec.data = realloc(d->vec.data, new_cap * sizeof(*d->vec.data)); + upb_MtDecoder_CheckOutOfMemory(d, d->vec.data); + d->vec.capacity = new_cap; + } + d->vec.data[d->vec.size++] = item; +} + +static void upb_MtDecoder_PushOneof(upb_MtDecoder* d, upb_LayoutItem item) { + if (item.field_index == kUpb_LayoutItem_IndexSentinel) { + upb_MtDecoder_ErrorFormat(d, "Empty oneof"); + UPB_UNREACHABLE(); + } + item.field_index -= kOneofBase; + + // Push oneof data. + item.type = kUpb_LayoutItemType_OneofField; + upb_MtDecoder_PushItem(d, item); + + // Push oneof case. + item.rep = kUpb_FieldRep_4Byte; // Field Number. + item.type = kUpb_LayoutItemType_OneofCase; + upb_MtDecoder_PushItem(d, item); +} + +static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d, + const char* ptr, + char first_ch, + upb_LayoutItem* item) { + uint32_t field_num; + ptr = upb_MiniTable_DecodeBase92Varint(d, ptr, first_ch, upb_ToBase92(0), + upb_ToBase92(63), &field_num); + upb_MiniTable_Field* f = + (void*)upb_MiniTable_FindFieldByNumber(d->table, field_num); + + if (!f) { + upb_MtDecoder_ErrorFormat(d, + "Couldn't add field number %" PRIu32 + " to oneof, no such field number.", + field_num); + UPB_UNREACHABLE(); + } + if (f->offset != kHasbitPresence) { + upb_MtDecoder_ErrorFormat( + d, + "Cannot add repeated, required, or singular field %" PRIu32 + " to oneof.", + field_num); + UPB_UNREACHABLE(); + } + + // Oneof storage must be large enough to accommodate the largest member. + item->rep = UPB_MAX(item->rep, f->mode >> kUpb_FieldRep_Shift); + // Prepend this field to the linked list. + f->offset = item->field_index; + item->field_index = (f - d->fields) + kOneofBase; + return ptr; +} + +static const char* upb_MtDecoder_DecodeOneofs(upb_MtDecoder* d, + const char* ptr) { + upb_LayoutItem item = {.rep = 0, + .field_index = kUpb_LayoutItem_IndexSentinel}; + while (ptr < d->end) { + char ch = *ptr++; + if (ch == kUpb_EncodedValue_FieldSeparator) { + // Field separator, no action needed. + } else if (ch == kUpb_EncodedValue_OneofSeparator) { + // End of oneof. + upb_MtDecoder_PushOneof(d, item); + item.field_index = kUpb_LayoutItem_IndexSentinel; // Move to next oneof. + } else { + ptr = upb_MtDecoder_DecodeOneofField(d, ptr, ch, &item); + } + } + + // Push final oneof. + upb_MtDecoder_PushOneof(d, item); + return ptr; +} + +static const char* upb_MtDecoder_ParseModifier(upb_MtDecoder* d, + const char* ptr, char first_ch, + uint16_t field_count, + uint64_t* msg_modifiers) { + uint32_t mod; + ptr = upb_MiniTable_DecodeBase92Varint(d, ptr, first_ch, + kUpb_EncodedValue_MinModifier, + kUpb_EncodedValue_MaxModifier, &mod); + if (field_count == 0) { + if (!d->table) { + upb_MtDecoder_ErrorFormat(d, "Extensions cannot have message modifiers"); + UPB_UNREACHABLE(); + } + *msg_modifiers = mod; + } else { + upb_MiniTable_Field* field = &d->fields[field_count - 1]; + upb_MtDecoder_ModifyField(d, mod, field); + } + + return ptr; +} + +static void upb_MtDecoder_AllocateSubs(upb_MtDecoder* d, uint32_t sub_count) { + size_t subs_bytes = sizeof(*d->table->subs) * sub_count; + d->table->subs = upb_Arena_Malloc(d->arena, subs_bytes); + upb_MtDecoder_CheckOutOfMemory(d, d->table->subs); +} + +static void upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr, size_t len, + void* fields, size_t field_size, + uint16_t* field_count, uint32_t* sub_count) { + uint64_t msg_modifiers = 0; + uint32_t last_field_number = 0; + bool need_dense_below = d->table != NULL; + + d->end = ptr + len; + + while (ptr < d->end) { + char ch = *ptr++; + if (ch <= kUpb_EncodedValue_MaxField) { + upb_MiniTable_Field* field = fields; + *field_count += 1; + fields = (char*)fields + field_size; + field->number = ++last_field_number; + upb_MiniTable_SetField(d, ch, field, msg_modifiers, sub_count); + } else if (kUpb_EncodedValue_MinModifier <= ch && + ch <= kUpb_EncodedValue_MaxModifier) { + ptr = upb_MtDecoder_ParseModifier(d, ptr, ch, *field_count, &msg_modifiers); + } else if (ch == kUpb_EncodedValue_End) { + if (!d->table) { + upb_MtDecoder_ErrorFormat(d, "Extensions cannot have oneofs."); + UPB_UNREACHABLE(); + } + ptr = upb_MtDecoder_DecodeOneofs(d, ptr); + } else if (kUpb_EncodedValue_MinSkip <= ch && + ch <= kUpb_EncodedValue_MaxSkip) { + if (need_dense_below) { + d->table->dense_below = d->table->field_count; + need_dense_below = false; + } + uint32_t skip; + ptr = upb_MiniTable_DecodeBase92Varint(d, ptr, ch, + kUpb_EncodedValue_MinSkip, + kUpb_EncodedValue_MaxSkip, &skip); + last_field_number += skip; + last_field_number--; // Next field seen will increment. + } + } + + if (need_dense_below) { + d->table->dense_below = d->table->field_count; + } +} + +static void upb_MtDecoder_ParseMessage(upb_MtDecoder* d, const char* data, + size_t len) { + // Buffer length is an upper bound on the number of fields. We will return + // what we don't use. + d->fields = upb_Arena_Malloc(d->arena, sizeof(*d->fields) * len); + upb_MtDecoder_CheckOutOfMemory(d, d->fields); + + uint32_t sub_count = 0; + d->table->field_count = 0; + d->table->fields = d->fields; + upb_MtDecoder_Parse(d, data, len, d->fields, sizeof(*d->fields), + &d->table->field_count, &sub_count); + + // Return unused memory from fields array. + assert(d->table->field_count <= len); + d->fields = upb_Arena_Realloc(d->arena, d->fields, sizeof(*d->fields) * len, + sizeof(*d->fields) * d->table->field_count); + d->table->fields = d->fields; + upb_MtDecoder_AllocateSubs(d, sub_count); +} + +#define UPB_COMPARE_INTEGERS(a, b) ((a) < (b) ? -1 : ((a) == (b) ? 0 : 1)) + +int upb_MtDecoder_CompareFields(const void* _a, const void* _b) { + const upb_LayoutItem* a = _a; + const upb_LayoutItem* b = _b; + // Currently we just sort by: + // 1. rep (descending, so largest fields are first) + // 2. is_case (descending, so oneof cases are first) + // 2. field_number (ascending, so smallest numbers are first) + // + // The main goal of this is to reduce space lost to padding. + if (a->rep != b->rep) return UPB_COMPARE_INTEGERS(a->rep, b->rep); + if (a->type != b->type) return UPB_COMPARE_INTEGERS(a->type, b->type); + return UPB_COMPARE_INTEGERS(b->field_index, a->field_index); +} + +#undef UPB_COMPARE_INTEGERS + +static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) { + // Add items for all non-oneof fields (oneofs were already added). + int n = d->table->field_count; + for (int i = 0; i < n; i++) { + upb_MiniTable_Field* f = &d->fields[i]; + if (f->offset >= kOneofBase) continue; + upb_LayoutItem item = {.field_index = i, + .rep = f->mode >> kUpb_FieldRep_Shift}; + upb_MtDecoder_PushItem(d, item); + } + + qsort(d->vec.data, d->vec.size, sizeof(*d->vec.data), + upb_MtDecoder_CompareFields); + + return true; +} + +static size_t upb_MiniTable_DivideRoundUp(size_t n, size_t d) { + return (n + d - 1) / d; +} + +static void upb_MtDecoder_AssignHasbits(upb_MiniTable* ret) { + int n = ret->field_count; + int last_hasbit = 0; // 0 cannot be used. + + // First assign required fields, which must have the lowest hasbits. + for (int i = 0; i < n; i++) { + upb_MiniTable_Field* field = (upb_MiniTable_Field*)&ret->fields[i]; + if (field->offset == kRequiredPresence) { + field->presence = ++last_hasbit; + } + } + ret->required_count = last_hasbit; + + // Next assign non-required hasbit fields. + for (int i = 0; i < n; i++) { + upb_MiniTable_Field* field = (upb_MiniTable_Field*)&ret->fields[i]; + if (field->offset == kHasbitPresence) { + field->presence = ++last_hasbit; + } + } + + ret->size = upb_MiniTable_DivideRoundUp(last_hasbit, 8); +} + +size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep, upb_MiniTablePlatform platform) { + static const uint8_t kRepToSize32[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 4, + [kUpb_FieldRep_StringView] = 8, + [kUpb_FieldRep_8Byte] = 8, + }; + static const uint8_t kRepToSize64[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 8, + [kUpb_FieldRep_StringView] = 16, + [kUpb_FieldRep_8Byte] = 8, + }; + assert(sizeof(upb_StringView) == + UPB_SIZE(kRepToSize32, kRepToSize64)[kUpb_FieldRep_StringView]); + return platform == kUpb_MiniTablePlatform_32Bit ? kRepToSize32[rep] + : kRepToSize64[rep]; +} + +size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep, upb_MiniTablePlatform platform) { + static const uint8_t kRepToAlign32[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 4, + [kUpb_FieldRep_StringView] = 4, + [kUpb_FieldRep_8Byte] = 8, + }; + static const uint8_t kRepToAlign64[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 8, + [kUpb_FieldRep_StringView] = 8, + [kUpb_FieldRep_8Byte] = 8, + }; + assert(UPB_ALIGN_OF(upb_StringView) == + UPB_SIZE(kRepToAlign32, kRepToAlign64)[kUpb_FieldRep_StringView]); + return platform == kUpb_MiniTablePlatform_32Bit ? kRepToAlign32[rep] + : kRepToAlign64[rep]; +} + +size_t upb_MtDecoder_Place(upb_MtDecoder* d, upb_FieldRep rep) { + size_t size = upb_MtDecoder_SizeOfRep(rep, d->platform); + size_t align = upb_MtDecoder_AlignOfRep(rep, d->platform); + size_t ret = UPB_ALIGN_UP(d->table->size, align); + d->table->size = ret + size; + return ret; +} + +static bool upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { + upb_LayoutItem* end = d->vec.data + d->vec.size; + + // Compute offsets. + for (upb_LayoutItem* item = d->vec.data; item < end; item++) { + item->offset = upb_MtDecoder_Place(d, item->rep); + } + + // Assign oneof case offsets. We must do these first, since assigning + // actual offsets will overwrite the links of the linked list. + for (upb_LayoutItem* item = d->vec.data; item < end; item++) { + if (item->type != kUpb_LayoutItemType_OneofCase) continue; + upb_MiniTable_Field* f = &d->fields[item->field_index]; + while (true) { + f->presence = ~item->offset; + if (f->offset == kUpb_LayoutItem_IndexSentinel) break; + f = &d->fields[f->offset - kOneofBase]; + } + } + + // Assign offsets. + for (upb_LayoutItem* item = d->vec.data; item < end; item++) { + upb_MiniTable_Field* f = &d->fields[item->field_index]; + switch (item->type) { + case kUpb_LayoutItemType_OneofField: + while (true) { + uint16_t next_offset = f->offset; + f->offset = item->offset; + if (next_offset == kUpb_LayoutItem_IndexSentinel) break; + f = &d->fields[next_offset - kOneofBase]; + } + break; + case kUpb_LayoutItemType_Field: + f->offset = item->offset; + break; + default: + break; + } + } + return true; +} + +upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, void** buf, + size_t* buf_size, upb_Status* status) { + upb_MtDecoder decoder = { + .platform = platform, + .vec = + { + .data = *buf, + .capacity = *buf_size / sizeof(*decoder.vec.data), + .size = 0, + }, + .arena = arena, + .status = status, + .table = upb_Arena_Malloc(arena, sizeof(*decoder.table)), + }; + + if (UPB_SETJMP(decoder.err)) { + decoder.table = NULL; + goto done; + } + + upb_MtDecoder_CheckOutOfMemory(&decoder, decoder.table); + + decoder.table->size = 0; + decoder.table->field_count = 0; + decoder.table->ext = upb_ExtMode_NonExtendable; + decoder.table->dense_below = 0; + decoder.table->table_mask = 0; + decoder.table->required_count = 0; + + upb_MtDecoder_ParseMessage(&decoder, data, len); + upb_MtDecoder_AssignHasbits(decoder.table); + upb_MtDecoder_SortLayoutItems(&decoder); + upb_MtDecoder_AssignOffsets(&decoder); + +done: + *buf = decoder.vec.data; + *buf_size = decoder.vec.capacity / sizeof(*decoder.vec.data); + return decoder.table; +} + +upb_MiniTable* upb_MiniTable_BuildMessageSet(upb_MiniTablePlatform platform, + upb_Arena* arena) { + upb_MiniTable* ret = upb_Arena_Malloc(arena, sizeof(*ret)); + if (!ret) return NULL; + + ret->size = 0; + ret->field_count = 0; + ret->ext = upb_ExtMode_IsMessageSet; + ret->dense_below = 0; + ret->table_mask = 0; + ret->required_count = 0; + return ret; +} + +upb_MiniTable* upb_MiniTable_BuildMapEntry(upb_FieldType key_type, + upb_FieldType value_type, + upb_MiniTablePlatform platform, + upb_Arena* arena) { + upb_MiniTable* ret = upb_Arena_Malloc(arena, sizeof(*ret)); + upb_MiniTable_Field* fields = upb_Arena_Malloc(arena, sizeof(*fields) * 2); + if (!ret || !fields) return NULL; + + upb_MiniTable_Sub* subs = NULL; + if (value_type == kUpb_FieldType_Message || value_type == kUpb_FieldType_Group) { + subs = upb_Arena_Malloc(arena, sizeof(*subs)); + if (!subs) return NULL; + } + + size_t field_size = + upb_MtDecoder_SizeOfRep(kUpb_FieldRep_StringView, platform); + + fields[0].number = 1; + fields[1].number = 2; + fields[0].mode = kUpb_FieldMode_Scalar; + fields[1].mode = kUpb_FieldMode_Scalar; + fields[0].presence = 0; + fields[1].presence = 0; + fields[0].descriptortype = key_type; + fields[1].descriptortype = value_type; + fields[0].offset = 0; + fields[1].offset = field_size; + fields[1].submsg_index = 0; + + ret->size = UPB_ALIGN_UP(2 * field_size, 8); + ret->field_count = 2; + ret->ext = upb_ExtMode_NonExtendable | upb_ExtMode_IsMapEntry; + ret->dense_below = 2; + ret->table_mask = 0; + ret->required_count = 0; + ret->subs = subs; + ret->fields = fields; + return ret; +} + +upb_MiniTable_Extension* upb_MiniTable_BuildExtensions(const char* data, + size_t len, + size_t* ext_count, + upb_Arena* arena, + upb_Status* status) { + upb_MtDecoder decoder = { + .arena = arena, + .status = status, + .table = NULL, + }; + + upb_MiniTable_Extension* exts; + + if (UPB_SETJMP(decoder.err)) { + exts = NULL; + *ext_count = 0; + goto done; + } + + uint16_t count = 0; + exts = upb_Arena_Malloc(arena, len); + upb_MtDecoder_CheckOutOfMemory(&decoder, exts); + upb_MtDecoder_Parse(&decoder, data, len, exts, sizeof(*exts), &count, NULL); + exts = upb_Arena_Realloc(arena, exts, sizeof(*exts) * len, + sizeof(*exts) * count); + +done: + *ext_count = count; + return exts; +} + +upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, upb_Status* status) { + void* buf = NULL; + size_t size = 0; + upb_MiniTable* ret = upb_MiniTable_BuildWithBuf(data, len, platform, arena, + &buf, &size, status); + free(buf); + return ret; +} + +void upb_MiniTable_SetSubMessage(upb_MiniTable* table, + upb_MiniTable_Field* field, + const upb_MiniTable* sub) { + assert((uintptr_t)table->fields <= (uintptr_t)field && + (uintptr_t)field < (uintptr_t)(table->fields + table->field_count)); + if (sub->ext & upb_ExtMode_IsMapEntry) { + field->mode = + (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift) | kUpb_FieldMode_Map; + } + upb_MiniTable_Sub* table_sub = (void*)&table->subs[field->submsg_index]; + table_sub->submsg = sub; +} + +void upb_MiniTable_SetSubEnum(upb_MiniTable* table, + upb_MiniTable_Field* field, + const upb_MiniTable_Enum* sub) { + assert((uintptr_t)table->fields <= (uintptr_t)field && + (uintptr_t)field < (uintptr_t)(table->fields + table->field_count)); + upb_MiniTable_Sub* table_sub = (void*)&table->subs[field->submsg_index]; + table_sub->subenum = sub; +} diff --git a/upb/mini_table.h b/upb/mini_table.h new file mode 100644 index 0000000000..0f82875b1a --- /dev/null +++ b/upb/mini_table.h @@ -0,0 +1,162 @@ +/* + * 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 UPB_MINI_TABLE_H_ +#define UPB_MINI_TABLE_H_ + +#include "upb/msg_internal.h" + +// Must be last. +#include "upb/port_def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber( + const upb_MiniTable* table, uint32_t number); + +typedef enum { + kUpb_MessageModifier_DefaultIsPacked = 1, + kUpb_MessageModifier_HasClosedEnums = 2, + kUpb_MessageModifier_IsExtendable = 4, + kUpb_MessageModifier_IsMapEntry = 8, +} kUpb_MessageModifier; + +typedef enum { + kUpb_FieldModifier_IsRepeated = 1, + kUpb_FieldModifier_IsPacked = 2, + kUpb_FieldModifier_IsProto3Singular = 4, + kUpb_FieldModifier_IsRequired = 8, +} kUpb_FieldModifier; + +/** upb_MtDataEncoder *********************************************************/ + +// Functions to encode a string in a format that can be loaded by +// upb_MiniTable_Build(). + +typedef struct { + char* end; // Limit of the buffer passed as a parameter. + // Aliased to internal-only members in .cc. + char internal[32]; +} upb_MtDataEncoder; + +// If the input buffer has at least this many bytes available, the encoder call +// is guaranteed to succeed (as long as field number order is maintained). +#define kUpb_MtDataEncoder_MinSize 16 + +// Encodes field/oneof information for a given message. The sequence of calls +// should look like: +// +// upb_MtDataEncoder e; +// char buf[256]; +// char* ptr = buf; +// e.end = ptr + sizeof(buf); +// ptr = upb_MtDataEncoder_StartMessage(&e, ptr); +// // Fields *must* be in field number order. +// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...); +// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...); +// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...); +// +// // If oneofs are present. Oneofs must be encoded after regular fields. +// ptr = upb_MiniTable_StartOneof(&e, ptr) +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// +// ptr = upb_MiniTable_StartOneof(&e, ptr); +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// +// Oneofs must be encoded after all regular fields. +char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* buf, + uint64_t msg_mod); +char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* buf, + upb_FieldType type, uint32_t field_num, + uint64_t field_mod); +char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* buf); +char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* buf, + uint32_t field_num); + + +/** upb_MiniTable *************************************************************/ + +typedef enum { + kUpb_MiniTablePlatform_32Bit, + kUpb_MiniTablePlatform_64Bit, + kUpb_MiniTablePlatform_Native = + UPB_SIZE(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit), +} upb_MiniTablePlatform; + +// Builds a mini table from the data encoded in the buffer [data, len]. If any +// errors occur, returns NULL and sets a status message. In the success case, +// the caller must call upb_MiniTable_SetSub*() for all message or proto2 enum +// fields to link the table to the appropriate sub-tables. +upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, upb_Status* status); +void upb_MiniTable_SetSubMessage(upb_MiniTable* table, + upb_MiniTable_Field* field, + const upb_MiniTable* sub); +void upb_MiniTable_SetSubEnum(upb_MiniTable* table, + upb_MiniTable_Field* field, + const upb_MiniTable_Enum* sub); + +upb_MiniTable_Extension* upb_MiniTable_BuildExtensions(const char* data, + size_t len, + size_t* ext_count, + upb_Arena* arena, + upb_Status* status); +void upb_MiniTable_ResolveExtension(upb_MiniTable_Extension* ext, + const upb_MiniTable* extendee, + upb_MiniTable_Sub sub); + +// Special-case functions for MessageSet layout and map entries. +upb_MiniTable* upb_MiniTable_BuildMessageSet(upb_MiniTablePlatform platform, + upb_Arena* arena); +upb_MiniTable* upb_MiniTable_BuildMapEntry(upb_FieldType key_type, + upb_FieldType value_type, + upb_MiniTablePlatform platform, + upb_Arena* arena); + +// Like upb_MiniTable_Build(), but the user provides a buffer of layout data so +// it can be reused from call to call, avoiding repeated realloc()/free(). +// +// The caller owns `*buf` both before and after the call, and must free() it +// when it is no longer in use. The function will realloc() `*buf` as +// necessary, updating `*size` accordingly. +upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, void** buf, + size_t* buf_size, upb_Status* status); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + +#endif /* UPB_MINI_TABLE_H_ */ diff --git a/upb/mini_table.hpp b/upb/mini_table.hpp new file mode 100644 index 0000000000..b0654387ba --- /dev/null +++ b/upb/mini_table.hpp @@ -0,0 +1,96 @@ +/* + * 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 UPB_MINI_TABLE_HPP_ +#define UPB_MINI_TABLE_HPP_ + +#include + +#include + +#include "upb/mini_table.h" + +namespace upb { + +class MtDataEncoder { + public: + MtDataEncoder() : appender_(&encoder_) {} + + bool StartMessage(uint64_t msg_mod) { + return appender_([=](char* buf) { + return upb_MtDataEncoder_StartMessage(&encoder_, buf, msg_mod); + }); + } + + bool PutField(upb_FieldType type, uint32_t field_num, uint64_t field_mod) { + return appender_([=](char* buf) { + return upb_MtDataEncoder_PutField(&encoder_, buf, type, field_num, + field_mod); + }); + } + + bool StartOneof() { + return appender_([=](char* buf) { + return upb_MtDataEncoder_StartOneof(&encoder_, buf); + }); + } + + bool PutOneofField(uint32_t field_num) { + return appender_([=](char* buf) { + return upb_MtDataEncoder_PutOneofField(&encoder_, buf, field_num); + }); + } + + const std::string& data() const { return appender_.data(); } + + private: + class StringAppender { + public: + StringAppender(upb_MtDataEncoder* e) { e->end = buf_ + sizeof(buf_); } + + template + bool operator()(T&& func) { + char* end = func(buf_); + if (!end) return false; + str_.append(buf_, end - buf_); + return true; + } + + const std::string& data() const { return str_; } + + private: + char buf_[kUpb_MtDataEncoder_MinSize]; + std::string str_; + }; + + upb_MtDataEncoder encoder_; + StringAppender appender_; +}; + +} // namespace upb + +#endif /* UPB_MINI_TABLE_HPP_ */ diff --git a/upb/mini_table_test.cc b/upb/mini_table_test.cc new file mode 100644 index 0000000000..94d2814eda --- /dev/null +++ b/upb/mini_table_test.cc @@ -0,0 +1,172 @@ +/* + * 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. + */ + +#include "upb/mini_table.hpp" + +#include "absl/container/flat_hash_set.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "upb/msg_internal.h" +#include "upb/upb.hpp" + +class MiniTableTest : public testing::TestWithParam {}; + +TEST_P(MiniTableTest, Empty) { + upb::Arena arena; + upb::Status status; + upb_MiniTable* table = + upb_MiniTable_Build(NULL, 0, GetParam(), arena.ptr(), status.ptr()); + ASSERT_NE(nullptr, table); + EXPECT_EQ(0, table->field_count); + EXPECT_EQ(0, table->required_count); +} + +TEST_P(MiniTableTest, AllScalarTypes) { + upb::Arena arena; + upb::MtDataEncoder e; + ASSERT_TRUE(e.StartMessage(0)); + int count = 0; + for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + ASSERT_TRUE(e.PutField(static_cast(i), i, 0)); + count++; + } + fprintf(stderr, "YO: %s\n", e.data().c_str()); + upb::Status status; + upb_MiniTable* table = upb_MiniTable_Build( + e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); + ASSERT_NE(nullptr, table); + EXPECT_EQ(count, table->field_count); + absl::flat_hash_set offsets; + for (int i = 0; i < 16; i++) { + const upb_MiniTable_Field* f = &table->fields[i]; + EXPECT_EQ(i + 1, f->number); + EXPECT_EQ(i + kUpb_FieldType_Double, f->descriptortype); + EXPECT_EQ(kUpb_FieldMode_Scalar, f->mode & kUpb_FieldMode_Mask); + EXPECT_TRUE(offsets.insert(f->offset).second); + EXPECT_TRUE(f->offset < table->size); + } + EXPECT_EQ(0, table->required_count); +} + +TEST_P(MiniTableTest, AllRepeatedTypes) { + upb::Arena arena; + upb::MtDataEncoder e; + ASSERT_TRUE(e.StartMessage(0)); + int count = 0; + for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + ASSERT_TRUE(e.PutField(static_cast(i), i, + kUpb_FieldModifier_IsRepeated)); + count++; + } + fprintf(stderr, "YO: %s\n", e.data().c_str()); + upb::Status status; + upb_MiniTable* table = upb_MiniTable_Build( + e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); + ASSERT_NE(nullptr, table); + EXPECT_EQ(count, table->field_count); + absl::flat_hash_set offsets; + for (int i = 0; i < 16; i++) { + const upb_MiniTable_Field* f = &table->fields[i]; + EXPECT_EQ(i + 1, f->number); + EXPECT_EQ(i + kUpb_FieldType_Double, f->descriptortype); + EXPECT_EQ(kUpb_FieldMode_Array, f->mode & kUpb_FieldMode_Mask); + EXPECT_TRUE(offsets.insert(f->offset).second); + EXPECT_TRUE(f->offset < table->size); + } + EXPECT_EQ(0, table->required_count); +} + +TEST_P(MiniTableTest, Skips) { + upb::Arena arena; + upb::MtDataEncoder e; + ASSERT_TRUE(e.StartMessage(0)); + int count = 0; + std::vector field_numbers; + for (int i = 0; i < 25; i++) { + int field_number = 1 << i; + field_numbers.push_back(field_number); + ASSERT_TRUE(e.PutField(kUpb_FieldType_Float, field_number, 0)); + count++; + } + fprintf(stderr, "YO: %s, %zu\n", e.data().c_str(), e.data().size()); + upb::Status status; + upb_MiniTable* table = upb_MiniTable_Build( + e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); + ASSERT_NE(nullptr, table); + EXPECT_EQ(count, table->field_count); + absl::flat_hash_set offsets; + for (size_t i = 0; i < field_numbers.size(); i++) { + const upb_MiniTable_Field* f = &table->fields[i]; + EXPECT_EQ(field_numbers[i], f->number); + EXPECT_EQ(kUpb_FieldType_Float, f->descriptortype); + EXPECT_EQ(kUpb_FieldMode_Scalar, f->mode & kUpb_FieldMode_Mask); + EXPECT_TRUE(offsets.insert(f->offset).second); + EXPECT_TRUE(f->offset < table->size); + } + EXPECT_EQ(0, table->required_count); +} + +TEST_P(MiniTableTest, AllScalarTypesOneof) { + upb::Arena arena; + upb::MtDataEncoder e; + ASSERT_TRUE(e.StartMessage(0)); + int count = 0; + for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + ASSERT_TRUE(e.PutField(static_cast(i), i, 0)); + count++; + } + ASSERT_TRUE(e.StartOneof()); + for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + ASSERT_TRUE(e.PutOneofField(i)); + } + fprintf(stderr, "YO: %s\n", e.data().c_str()); + upb::Status status; + upb_MiniTable* table = upb_MiniTable_Build( + e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); + ASSERT_NE(nullptr, table) << status.error_message(); + EXPECT_EQ(count, table->field_count); + absl::flat_hash_set offsets; + for (int i = 0; i < 16; i++) { + const upb_MiniTable_Field* f = &table->fields[i]; + EXPECT_EQ(i + 1, f->number); + EXPECT_EQ(i + kUpb_FieldType_Double, f->descriptortype); + EXPECT_EQ(kUpb_FieldMode_Scalar, f->mode & kUpb_FieldMode_Mask); + // For a oneof all fields have the same offset. + EXPECT_EQ(table->fields[0].offset, f->offset); + // All presence fields should point to the same oneof case offset. + size_t case_ofs = _upb_oneofcase_ofs(f); + EXPECT_EQ(table->fields[0].presence, f->presence); + EXPECT_TRUE(f->offset < table->size); + EXPECT_TRUE(case_ofs < table->size); + EXPECT_TRUE(case_ofs != f->offset); + } + EXPECT_EQ(0, table->required_count); +} + +INSTANTIATE_TEST_SUITE_P(Platforms, MiniTableTest, + testing::Values(kUpb_MiniTablePlatform_32Bit, + kUpb_MiniTablePlatform_64Bit)); From 532dc1f0f0058aed288a0a8aaa34e782f61182e1 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 17:47:59 -0800 Subject: [PATCH 2/9] Renamed a few more constants to the new style. These are not in the public API and so were not prioritized before. No functional change here, just renames. --- cmake/google/protobuf/descriptor.upb.c | 306 ++++++++++++------------- upb/decode.c | 10 +- upb/def.c | 52 ++--- upb/encode.c | 22 +- upb/msg_internal.h | 57 ++--- upbc/protoc-gen-upb.cc | 26 +-- 6 files changed, 236 insertions(+), 237 deletions(-) diff --git a/cmake/google/protobuf/descriptor.upb.c b/cmake/google/protobuf/descriptor.upb.c index 08f314515b..87a290e72e 100644 --- a/cmake/google/protobuf/descriptor.upb.c +++ b/cmake/google/protobuf/descriptor.upb.c @@ -17,13 +17,13 @@ static const upb_MiniTable_Sub google_protobuf_FileDescriptorSet_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_FileDescriptorSet__fields[1] = { - {1, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_FileDescriptorSet_msginit = { &google_protobuf_FileDescriptorSet_submsgs[0], &google_protobuf_FileDescriptorSet__fields[0], - UPB_SIZE(8, 8), 1, upb_ExtMode_NonExtendable, 1, 255, 0, + UPB_SIZE(8, 8), 1, kUpb_ExtMode_NonExtendable, 1, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_FileDescriptorProto_submsgs[6] = { @@ -36,24 +36,24 @@ static const upb_MiniTable_Sub google_protobuf_FileDescriptorProto_submsgs[6] = }; static const upb_MiniTable_Field google_protobuf_FileDescriptorProto__fields[12] = { - {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(12, 24), 2, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {3, UPB_SIZE(36, 72), 0, 0, 12, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {4, UPB_SIZE(40, 80), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {5, UPB_SIZE(44, 88), 0, 1, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {6, UPB_SIZE(48, 96), 0, 4, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {7, UPB_SIZE(52, 104), 0, 2, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {8, UPB_SIZE(28, 56), 3, 3, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {9, UPB_SIZE(32, 64), 4, 5, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {10, UPB_SIZE(56, 112), 0, 0, 5, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {11, UPB_SIZE(60, 120), 0, 0, 5, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {12, UPB_SIZE(20, 40), 5, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 24), 2, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(36, 72), 0, 0, 12, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(40, 80), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(44, 88), 0, 1, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(48, 96), 0, 4, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(52, 104), 0, 2, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(28, 56), 3, 3, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {9, UPB_SIZE(32, 64), 4, 5, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(56, 112), 0, 0, 5, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {11, UPB_SIZE(60, 120), 0, 0, 5, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {12, UPB_SIZE(20, 40), 5, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_FileDescriptorProto_msginit = { &google_protobuf_FileDescriptorProto_submsgs[0], &google_protobuf_FileDescriptorProto__fields[0], - UPB_SIZE(64, 128), 12, upb_ExtMode_NonExtendable, 12, 255, 0, + UPB_SIZE(64, 128), 12, kUpb_ExtMode_NonExtendable, 12, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_DescriptorProto_submsgs[7] = { @@ -67,22 +67,22 @@ static const upb_MiniTable_Sub google_protobuf_DescriptorProto_submsgs[7] = { }; static const upb_MiniTable_Field google_protobuf_DescriptorProto__fields[10] = { - {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(16, 32), 0, 4, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {3, UPB_SIZE(20, 40), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {4, UPB_SIZE(24, 48), 0, 3, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {5, UPB_SIZE(28, 56), 0, 1, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {6, UPB_SIZE(32, 64), 0, 4, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {7, UPB_SIZE(12, 24), 2, 5, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {8, UPB_SIZE(36, 72), 0, 6, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {9, UPB_SIZE(40, 80), 0, 2, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {10, UPB_SIZE(44, 88), 0, 0, 12, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(16, 32), 0, 4, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(20, 40), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(24, 48), 0, 3, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(28, 56), 0, 1, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(32, 64), 0, 4, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(12, 24), 2, 5, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(36, 72), 0, 6, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {9, UPB_SIZE(40, 80), 0, 2, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(44, 88), 0, 0, 12, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_DescriptorProto_msginit = { &google_protobuf_DescriptorProto_submsgs[0], &google_protobuf_DescriptorProto__fields[0], - UPB_SIZE(48, 96), 10, upb_ExtMode_NonExtendable, 10, 255, 0, + UPB_SIZE(48, 96), 10, kUpb_ExtMode_NonExtendable, 10, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = { @@ -90,26 +90,26 @@ static const upb_MiniTable_Sub google_protobuf_DescriptorProto_ExtensionRange_su }; static const upb_MiniTable_Field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = { - {1, UPB_SIZE(4, 4), 1, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {2, UPB_SIZE(8, 8), 2, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {3, UPB_SIZE(12, 16), 3, 0, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 4), 1, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(8, 8), 2, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(12, 16), 3, 0, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_DescriptorProto_ExtensionRange_msginit = { &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0], &google_protobuf_DescriptorProto_ExtensionRange__fields[0], - UPB_SIZE(16, 24), 3, upb_ExtMode_NonExtendable, 3, 255, 0, + UPB_SIZE(16, 24), 3, kUpb_ExtMode_NonExtendable, 3, 255, 0, }; static const upb_MiniTable_Field google_protobuf_DescriptorProto_ReservedRange__fields[2] = { - {1, UPB_SIZE(4, 4), 1, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {2, UPB_SIZE(8, 8), 2, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 4), 1, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(8, 8), 2, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_DescriptorProto_ReservedRange_msginit = { NULL, &google_protobuf_DescriptorProto_ReservedRange__fields[0], - UPB_SIZE(16, 16), 2, upb_ExtMode_NonExtendable, 2, 255, 0, + UPB_SIZE(16, 16), 2, kUpb_ExtMode_NonExtendable, 2, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_ExtensionRangeOptions_submsgs[1] = { @@ -117,13 +117,13 @@ static const upb_MiniTable_Sub google_protobuf_ExtensionRangeOptions_submsgs[1] }; static const upb_MiniTable_Field google_protobuf_ExtensionRangeOptions__fields[1] = { - {999, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {999, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_ExtensionRangeOptions_msginit = { &google_protobuf_ExtensionRangeOptions_submsgs[0], &google_protobuf_ExtensionRangeOptions__fields[0], - UPB_SIZE(8, 8), 1, upb_ExtMode_Extendable, 0, 255, 0, + UPB_SIZE(8, 8), 1, kUpb_ExtMode_Extendable, 0, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_FieldDescriptorProto_submsgs[3] = { @@ -133,23 +133,23 @@ static const upb_MiniTable_Sub google_protobuf_FieldDescriptorProto_submsgs[3] = }; static const upb_MiniTable_Field google_protobuf_FieldDescriptorProto__fields[11] = { - {1, UPB_SIZE(24, 24), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(32, 40), 2, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {3, UPB_SIZE(12, 12), 3, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {4, UPB_SIZE(4, 4), 4, 1, 14, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {5, UPB_SIZE(8, 8), 5, 2, 14, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {6, UPB_SIZE(40, 56), 6, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {7, UPB_SIZE(48, 72), 7, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {8, UPB_SIZE(64, 104), 8, 0, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {9, UPB_SIZE(16, 16), 9, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {10, UPB_SIZE(56, 88), 10, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {17, UPB_SIZE(20, 20), 11, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, + {1, UPB_SIZE(24, 24), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(32, 40), 2, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(12, 12), 3, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(4, 4), 4, 1, 14, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(8, 8), 5, 2, 14, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(40, 56), 6, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(48, 72), 7, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(64, 104), 8, 0, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {9, UPB_SIZE(16, 16), 9, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(56, 88), 10, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {17, UPB_SIZE(20, 20), 11, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_FieldDescriptorProto_msginit = { &google_protobuf_FieldDescriptorProto_submsgs[0], &google_protobuf_FieldDescriptorProto__fields[0], - UPB_SIZE(72, 112), 11, upb_ExtMode_NonExtendable, 10, 255, 0, + UPB_SIZE(72, 112), 11, kUpb_ExtMode_NonExtendable, 10, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_OneofDescriptorProto_submsgs[1] = { @@ -157,14 +157,14 @@ static const upb_MiniTable_Sub google_protobuf_OneofDescriptorProto_submsgs[1] = }; static const upb_MiniTable_Field google_protobuf_OneofDescriptorProto__fields[2] = { - {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(12, 24), 2, 0, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 24), 2, 0, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_OneofDescriptorProto_msginit = { &google_protobuf_OneofDescriptorProto_submsgs[0], &google_protobuf_OneofDescriptorProto__fields[0], - UPB_SIZE(16, 32), 2, upb_ExtMode_NonExtendable, 2, 255, 0, + UPB_SIZE(16, 32), 2, kUpb_ExtMode_NonExtendable, 2, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_EnumDescriptorProto_submsgs[3] = { @@ -174,28 +174,28 @@ static const upb_MiniTable_Sub google_protobuf_EnumDescriptorProto_submsgs[3] = }; static const upb_MiniTable_Field google_protobuf_EnumDescriptorProto__fields[5] = { - {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(16, 32), 0, 2, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {3, UPB_SIZE(12, 24), 2, 1, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {4, UPB_SIZE(20, 40), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {5, UPB_SIZE(24, 48), 0, 0, 12, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(16, 32), 0, 2, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(12, 24), 2, 1, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(20, 40), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(24, 48), 0, 0, 12, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_EnumDescriptorProto_msginit = { &google_protobuf_EnumDescriptorProto_submsgs[0], &google_protobuf_EnumDescriptorProto__fields[0], - UPB_SIZE(32, 64), 5, upb_ExtMode_NonExtendable, 5, 255, 0, + UPB_SIZE(32, 64), 5, kUpb_ExtMode_NonExtendable, 5, 255, 0, }; static const upb_MiniTable_Field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = { - {1, UPB_SIZE(4, 4), 1, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {2, UPB_SIZE(8, 8), 2, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 4), 1, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(8, 8), 2, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = { NULL, &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0], - UPB_SIZE(16, 16), 2, upb_ExtMode_NonExtendable, 2, 255, 0, + UPB_SIZE(16, 16), 2, kUpb_ExtMode_NonExtendable, 2, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_EnumValueDescriptorProto_submsgs[1] = { @@ -203,15 +203,15 @@ static const upb_MiniTable_Sub google_protobuf_EnumValueDescriptorProto_submsgs[ }; static const upb_MiniTable_Field google_protobuf_EnumValueDescriptorProto__fields[3] = { - {1, UPB_SIZE(8, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(4, 4), 2, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {3, UPB_SIZE(16, 24), 3, 0, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(8, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(4, 4), 2, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 24), 3, 0, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_EnumValueDescriptorProto_msginit = { &google_protobuf_EnumValueDescriptorProto_submsgs[0], &google_protobuf_EnumValueDescriptorProto__fields[0], - UPB_SIZE(24, 32), 3, upb_ExtMode_NonExtendable, 3, 255, 0, + UPB_SIZE(24, 32), 3, kUpb_ExtMode_NonExtendable, 3, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_ServiceDescriptorProto_submsgs[2] = { @@ -220,15 +220,15 @@ static const upb_MiniTable_Sub google_protobuf_ServiceDescriptorProto_submsgs[2] }; static const upb_MiniTable_Field google_protobuf_ServiceDescriptorProto__fields[3] = { - {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(16, 32), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {3, UPB_SIZE(12, 24), 2, 1, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(16, 32), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(12, 24), 2, 1, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_ServiceDescriptorProto_msginit = { &google_protobuf_ServiceDescriptorProto_submsgs[0], &google_protobuf_ServiceDescriptorProto__fields[0], - UPB_SIZE(24, 48), 3, upb_ExtMode_NonExtendable, 3, 255, 0, + UPB_SIZE(24, 48), 3, kUpb_ExtMode_NonExtendable, 3, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_MethodDescriptorProto_submsgs[1] = { @@ -236,18 +236,18 @@ static const upb_MiniTable_Sub google_protobuf_MethodDescriptorProto_submsgs[1] }; static const upb_MiniTable_Field google_protobuf_MethodDescriptorProto__fields[6] = { - {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(12, 24), 2, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {3, UPB_SIZE(20, 40), 3, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {4, UPB_SIZE(28, 56), 4, 0, 11, kUpb_FieldMode_Scalar | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {5, UPB_SIZE(1, 1), 5, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {6, UPB_SIZE(2, 2), 6, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 24), 2, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(20, 40), 3, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(28, 56), 4, 0, 11, kUpb_FieldMode_Scalar | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(1, 1), 5, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(2, 2), 6, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_MethodDescriptorProto_msginit = { &google_protobuf_MethodDescriptorProto_submsgs[0], &google_protobuf_MethodDescriptorProto__fields[0], - UPB_SIZE(32, 64), 6, upb_ExtMode_NonExtendable, 6, 255, 0, + UPB_SIZE(32, 64), 6, kUpb_ExtMode_NonExtendable, 6, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_FileOptions_submsgs[2] = { @@ -256,33 +256,33 @@ static const upb_MiniTable_Sub google_protobuf_FileOptions_submsgs[2] = { }; static const upb_MiniTable_Field google_protobuf_FileOptions__fields[21] = { - {1, UPB_SIZE(20, 24), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {8, UPB_SIZE(28, 40), 2, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {9, UPB_SIZE(4, 4), 3, 1, 14, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {10, UPB_SIZE(8, 8), 4, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {11, UPB_SIZE(36, 56), 5, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {16, UPB_SIZE(9, 9), 6, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {17, UPB_SIZE(10, 10), 7, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {18, UPB_SIZE(11, 11), 8, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {20, UPB_SIZE(12, 12), 9, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {23, UPB_SIZE(13, 13), 10, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {27, UPB_SIZE(14, 14), 11, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {31, UPB_SIZE(15, 15), 12, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {36, UPB_SIZE(44, 72), 13, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {37, UPB_SIZE(52, 88), 14, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {39, UPB_SIZE(60, 104), 15, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {40, UPB_SIZE(68, 120), 16, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {41, UPB_SIZE(76, 136), 17, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {42, UPB_SIZE(16, 16), 18, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {44, UPB_SIZE(84, 152), 19, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {45, UPB_SIZE(92, 168), 20, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {999, UPB_SIZE(100, 184), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(20, 24), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(28, 40), 2, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {9, UPB_SIZE(4, 4), 3, 1, 14, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(8, 8), 4, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {11, UPB_SIZE(36, 56), 5, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {16, UPB_SIZE(9, 9), 6, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {17, UPB_SIZE(10, 10), 7, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {18, UPB_SIZE(11, 11), 8, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {20, UPB_SIZE(12, 12), 9, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {23, UPB_SIZE(13, 13), 10, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {27, UPB_SIZE(14, 14), 11, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {31, UPB_SIZE(15, 15), 12, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {36, UPB_SIZE(44, 72), 13, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {37, UPB_SIZE(52, 88), 14, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {39, UPB_SIZE(60, 104), 15, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {40, UPB_SIZE(68, 120), 16, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {41, UPB_SIZE(76, 136), 17, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {42, UPB_SIZE(16, 16), 18, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {44, UPB_SIZE(84, 152), 19, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {45, UPB_SIZE(92, 168), 20, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(100, 184), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_FileOptions_msginit = { &google_protobuf_FileOptions_submsgs[0], &google_protobuf_FileOptions__fields[0], - UPB_SIZE(104, 192), 21, upb_ExtMode_Extendable, 1, 255, 0, + UPB_SIZE(104, 192), 21, kUpb_ExtMode_Extendable, 1, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_MessageOptions_submsgs[1] = { @@ -290,17 +290,17 @@ static const upb_MiniTable_Sub google_protobuf_MessageOptions_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_MessageOptions__fields[5] = { - {1, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {2, UPB_SIZE(2, 2), 2, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {3, UPB_SIZE(3, 3), 3, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {7, UPB_SIZE(4, 4), 4, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {999, UPB_SIZE(8, 8), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(2, 2), 2, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(3, 3), 3, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(4, 4), 4, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(8, 8), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_MessageOptions_msginit = { &google_protobuf_MessageOptions_submsgs[0], &google_protobuf_MessageOptions__fields[0], - UPB_SIZE(16, 16), 5, upb_ExtMode_Extendable, 3, 255, 0, + UPB_SIZE(16, 16), 5, kUpb_ExtMode_Extendable, 3, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_FieldOptions_submsgs[3] = { @@ -310,20 +310,20 @@ static const upb_MiniTable_Sub google_protobuf_FieldOptions_submsgs[3] = { }; static const upb_MiniTable_Field google_protobuf_FieldOptions__fields[8] = { - {1, UPB_SIZE(4, 4), 1, 1, 14, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {2, UPB_SIZE(12, 12), 2, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {3, UPB_SIZE(13, 13), 3, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {5, UPB_SIZE(14, 14), 4, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {6, UPB_SIZE(8, 8), 5, 2, 14, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {10, UPB_SIZE(15, 15), 6, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {15, UPB_SIZE(16, 16), 7, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {999, UPB_SIZE(20, 24), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 4), 1, 1, 14, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 12), 2, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(13, 13), 3, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(14, 14), 4, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(8, 8), 5, 2, 14, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(15, 15), 6, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {15, UPB_SIZE(16, 16), 7, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(20, 24), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_FieldOptions_msginit = { &google_protobuf_FieldOptions_submsgs[0], &google_protobuf_FieldOptions__fields[0], - UPB_SIZE(24, 32), 8, upb_ExtMode_Extendable, 3, 255, 0, + UPB_SIZE(24, 32), 8, kUpb_ExtMode_Extendable, 3, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_OneofOptions_submsgs[1] = { @@ -331,13 +331,13 @@ static const upb_MiniTable_Sub google_protobuf_OneofOptions_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_OneofOptions__fields[1] = { - {999, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {999, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_OneofOptions_msginit = { &google_protobuf_OneofOptions_submsgs[0], &google_protobuf_OneofOptions__fields[0], - UPB_SIZE(8, 8), 1, upb_ExtMode_Extendable, 0, 255, 0, + UPB_SIZE(8, 8), 1, kUpb_ExtMode_Extendable, 0, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_EnumOptions_submsgs[1] = { @@ -345,15 +345,15 @@ static const upb_MiniTable_Sub google_protobuf_EnumOptions_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_EnumOptions__fields[3] = { - {2, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {3, UPB_SIZE(2, 2), 2, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {999, UPB_SIZE(4, 8), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {2, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(2, 2), 2, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(4, 8), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_EnumOptions_msginit = { &google_protobuf_EnumOptions_submsgs[0], &google_protobuf_EnumOptions__fields[0], - UPB_SIZE(8, 16), 3, upb_ExtMode_Extendable, 0, 255, 0, + UPB_SIZE(8, 16), 3, kUpb_ExtMode_Extendable, 0, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_EnumValueOptions_submsgs[1] = { @@ -361,14 +361,14 @@ static const upb_MiniTable_Sub google_protobuf_EnumValueOptions_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_EnumValueOptions__fields[2] = { - {1, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {999, UPB_SIZE(4, 8), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(4, 8), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_EnumValueOptions_msginit = { &google_protobuf_EnumValueOptions_submsgs[0], &google_protobuf_EnumValueOptions__fields[0], - UPB_SIZE(8, 16), 2, upb_ExtMode_Extendable, 1, 255, 0, + UPB_SIZE(8, 16), 2, kUpb_ExtMode_Extendable, 1, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_ServiceOptions_submsgs[1] = { @@ -376,14 +376,14 @@ static const upb_MiniTable_Sub google_protobuf_ServiceOptions_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_ServiceOptions__fields[2] = { - {33, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {999, UPB_SIZE(4, 8), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {33, UPB_SIZE(1, 1), 1, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(4, 8), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_ServiceOptions_msginit = { &google_protobuf_ServiceOptions_submsgs[0], &google_protobuf_ServiceOptions__fields[0], - UPB_SIZE(8, 16), 2, upb_ExtMode_Extendable, 0, 255, 0, + UPB_SIZE(8, 16), 2, kUpb_ExtMode_Extendable, 0, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_MethodOptions_submsgs[2] = { @@ -392,15 +392,15 @@ static const upb_MiniTable_Sub google_protobuf_MethodOptions_submsgs[2] = { }; static const upb_MiniTable_Field google_protobuf_MethodOptions__fields[3] = { - {33, UPB_SIZE(8, 8), 1, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, - {34, UPB_SIZE(4, 4), 2, 1, 14, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {999, UPB_SIZE(12, 16), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {33, UPB_SIZE(8, 8), 1, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {34, UPB_SIZE(4, 4), 2, 1, 14, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(12, 16), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_MethodOptions_msginit = { &google_protobuf_MethodOptions_submsgs[0], &google_protobuf_MethodOptions__fields[0], - UPB_SIZE(16, 24), 3, upb_ExtMode_Extendable, 0, 255, 0, + UPB_SIZE(16, 24), 3, kUpb_ExtMode_Extendable, 0, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_UninterpretedOption_submsgs[1] = { @@ -408,30 +408,30 @@ static const upb_MiniTable_Sub google_protobuf_UninterpretedOption_submsgs[1] = }; static const upb_MiniTable_Field google_protobuf_UninterpretedOption__fields[7] = { - {2, UPB_SIZE(56, 80), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {3, UPB_SIZE(32, 32), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {4, UPB_SIZE(8, 8), 2, 0, 4, kUpb_FieldMode_Scalar | (upb_FieldRep_8Byte << upb_FieldRep_Shift)}, - {5, UPB_SIZE(16, 16), 3, 0, 3, kUpb_FieldMode_Scalar | (upb_FieldRep_8Byte << upb_FieldRep_Shift)}, - {6, UPB_SIZE(24, 24), 4, 0, 1, kUpb_FieldMode_Scalar | (upb_FieldRep_8Byte << upb_FieldRep_Shift)}, - {7, UPB_SIZE(40, 48), 5, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {8, UPB_SIZE(48, 64), 6, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, + {2, UPB_SIZE(56, 80), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(32, 32), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(8, 8), 2, 0, 4, kUpb_FieldMode_Scalar | (kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(16, 16), 3, 0, 3, kUpb_FieldMode_Scalar | (kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(24, 24), 4, 0, 1, kUpb_FieldMode_Scalar | (kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(40, 48), 5, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(48, 64), 6, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_UninterpretedOption_msginit = { &google_protobuf_UninterpretedOption_submsgs[0], &google_protobuf_UninterpretedOption__fields[0], - UPB_SIZE(64, 96), 7, upb_ExtMode_NonExtendable, 0, 255, 0, + UPB_SIZE(64, 96), 7, kUpb_ExtMode_NonExtendable, 0, 255, 0, }; static const upb_MiniTable_Field google_protobuf_UninterpretedOption_NamePart__fields[2] = { - {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {2, UPB_SIZE(1, 1), 2, 0, 8, kUpb_FieldMode_Scalar | (upb_FieldRep_1Byte << upb_FieldRep_Shift)}, + {1, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(1, 1), 2, 0, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_UninterpretedOption_NamePart_msginit = { NULL, &google_protobuf_UninterpretedOption_NamePart__fields[0], - UPB_SIZE(16, 32), 2, upb_ExtMode_NonExtendable, 2, 255, 2, + UPB_SIZE(16, 32), 2, kUpb_ExtMode_NonExtendable, 2, 255, 2, }; static const upb_MiniTable_Sub google_protobuf_SourceCodeInfo_submsgs[1] = { @@ -439,27 +439,27 @@ static const upb_MiniTable_Sub google_protobuf_SourceCodeInfo_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_SourceCodeInfo__fields[1] = { - {1, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_SourceCodeInfo_msginit = { &google_protobuf_SourceCodeInfo_submsgs[0], &google_protobuf_SourceCodeInfo__fields[0], - UPB_SIZE(8, 8), 1, upb_ExtMode_NonExtendable, 1, 255, 0, + UPB_SIZE(8, 8), 1, kUpb_ExtMode_NonExtendable, 1, 255, 0, }; static const upb_MiniTable_Field google_protobuf_SourceCodeInfo_Location__fields[5] = { - {1, UPB_SIZE(20, 40), 0, 0, 5, kUpb_FieldMode_Array | upb_LabelFlags_IsPacked | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {2, UPB_SIZE(24, 48), 0, 0, 5, kUpb_FieldMode_Array | upb_LabelFlags_IsPacked | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {3, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {4, UPB_SIZE(12, 24), 2, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {6, UPB_SIZE(28, 56), 0, 0, 12, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(20, 40), 0, 0, 5, kUpb_FieldMode_Array | kUpb_LabelFlags_IsPacked | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(24, 48), 0, 0, 5, kUpb_FieldMode_Array | kUpb_LabelFlags_IsPacked | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(4, 8), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(12, 24), 2, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(28, 56), 0, 0, 12, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_SourceCodeInfo_Location_msginit = { NULL, &google_protobuf_SourceCodeInfo_Location__fields[0], - UPB_SIZE(32, 64), 5, upb_ExtMode_NonExtendable, 4, 255, 0, + UPB_SIZE(32, 64), 5, kUpb_ExtMode_NonExtendable, 4, 255, 0, }; static const upb_MiniTable_Sub google_protobuf_GeneratedCodeInfo_submsgs[1] = { @@ -467,26 +467,26 @@ static const upb_MiniTable_Sub google_protobuf_GeneratedCodeInfo_submsgs[1] = { }; static const upb_MiniTable_Field google_protobuf_GeneratedCodeInfo__fields[1] = { - {1, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, + {1, UPB_SIZE(0, 0), 0, 0, 11, kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_GeneratedCodeInfo_msginit = { &google_protobuf_GeneratedCodeInfo_submsgs[0], &google_protobuf_GeneratedCodeInfo__fields[0], - UPB_SIZE(8, 8), 1, upb_ExtMode_NonExtendable, 1, 255, 0, + UPB_SIZE(8, 8), 1, kUpb_ExtMode_NonExtendable, 1, 255, 0, }; static const upb_MiniTable_Field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = { - {1, UPB_SIZE(20, 32), 0, 0, 5, kUpb_FieldMode_Array | upb_LabelFlags_IsPacked | (upb_FieldRep_Pointer << upb_FieldRep_Shift)}, - {2, UPB_SIZE(12, 16), 1, 0, 12, kUpb_FieldMode_Scalar | (upb_FieldRep_StringView << upb_FieldRep_Shift)}, - {3, UPB_SIZE(4, 4), 2, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, - {4, UPB_SIZE(8, 8), 3, 0, 5, kUpb_FieldMode_Scalar | (upb_FieldRep_4Byte << upb_FieldRep_Shift)}, + {1, UPB_SIZE(20, 32), 0, 0, 5, kUpb_FieldMode_Array | kUpb_LabelFlags_IsPacked | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 16), 1, 0, 12, kUpb_FieldMode_Scalar | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(4, 4), 2, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(8, 8), 3, 0, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, }; const upb_MiniTable google_protobuf_GeneratedCodeInfo_Annotation_msginit = { NULL, &google_protobuf_GeneratedCodeInfo_Annotation__fields[0], - UPB_SIZE(24, 48), 4, upb_ExtMode_NonExtendable, 4, 255, 0, + UPB_SIZE(24, 48), 4, kUpb_ExtMode_NonExtendable, 4, 255, 0, }; static const upb_MiniTable *messages_layout[27] = { diff --git a/upb/decode.c b/upb/decode.c index 1e07d42c32..683e4ec32b 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -742,7 +742,7 @@ static const char* decode_msgset(upb_Decoder* d, const char* ptr, .fields = NULL, .size = 0, .field_count = 0, - .ext = upb_ExtMode_IsMessageSet_ITEM, + .ext = kUpb_ExtMode_IsMessageSet_ITEM, .dense_below = 0, .table_mask = -1}; return decode_group(d, ptr, msg, &item_layout, 1); @@ -780,19 +780,19 @@ static const upb_MiniTable_Field* decode_findfield(upb_Decoder* d, if (d->extreg) { switch (l->ext) { - case upb_ExtMode_Extendable: { + case kUpb_ExtMode_Extendable: { const upb_MiniTable_Extension* ext = _upb_extreg_get(d->extreg, l, field_number); if (ext) return &ext->field; break; } - case upb_ExtMode_IsMessageSet: + case kUpb_ExtMode_IsMessageSet: if (field_number == _UPB_MSGSET_ITEM) { static upb_MiniTable_Field item = {0, 0, 0, 0, TYPE_MSGSET_ITEM, 0}; return &item; } break; - case upb_ExtMode_IsMessageSet_ITEM: + case kUpb_ExtMode_IsMessageSet_ITEM: switch (field_number) { case _UPB_MSGSET_TYPEID: { static upb_MiniTable_Field type_id = { @@ -885,7 +885,7 @@ static const char* decode_known(upb_Decoder* d, const char* ptr, const upb_MiniTable_Sub* subs = layout->subs; uint8_t mode = field->mode; - if (UPB_UNLIKELY(mode & upb_LabelFlags_IsExtension)) { + if (UPB_UNLIKELY(mode & kUpb_LabelFlags_IsExtension)) { const upb_MiniTable_Extension* ext_layout = (const upb_MiniTable_Extension*)field; upb_Message_Extension* ext = diff --git a/upb/def.c b/upb/def.c index f5331d1599..2dcc793771 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1426,43 +1426,43 @@ static void fill_fieldlayout(upb_MiniTable_Field* field, if (upb_FieldDef_IsMap(f)) { field->mode = - kUpb_FieldMode_Map | (upb_FieldRep_Pointer << upb_FieldRep_Shift); + kUpb_FieldMode_Map | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift); } else if (upb_FieldDef_IsRepeated(f)) { field->mode = - kUpb_FieldMode_Array | (upb_FieldRep_Pointer << upb_FieldRep_Shift); + kUpb_FieldMode_Array | (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift); } else { /* Maps descriptor type -> elem_size_lg2. */ static const uint8_t sizes[] = { -1, /* invalid descriptor type */ - upb_FieldRep_8Byte, /* DOUBLE */ - upb_FieldRep_4Byte, /* FLOAT */ - upb_FieldRep_8Byte, /* INT64 */ - upb_FieldRep_8Byte, /* UINT64 */ - upb_FieldRep_4Byte, /* INT32 */ - upb_FieldRep_8Byte, /* FIXED64 */ - upb_FieldRep_4Byte, /* FIXED32 */ - upb_FieldRep_1Byte, /* BOOL */ - upb_FieldRep_StringView, /* STRING */ - upb_FieldRep_Pointer, /* GROUP */ - upb_FieldRep_Pointer, /* MESSAGE */ - upb_FieldRep_StringView, /* BYTES */ - upb_FieldRep_4Byte, /* UINT32 */ - upb_FieldRep_4Byte, /* ENUM */ - upb_FieldRep_4Byte, /* SFIXED32 */ - upb_FieldRep_8Byte, /* SFIXED64 */ - upb_FieldRep_4Byte, /* SINT32 */ - upb_FieldRep_8Byte, /* SINT64 */ + kUpb_FieldRep_8Byte, /* DOUBLE */ + kUpb_FieldRep_4Byte, /* FLOAT */ + kUpb_FieldRep_8Byte, /* INT64 */ + kUpb_FieldRep_8Byte, /* UINT64 */ + kUpb_FieldRep_4Byte, /* INT32 */ + kUpb_FieldRep_8Byte, /* FIXED64 */ + kUpb_FieldRep_4Byte, /* FIXED32 */ + kUpb_FieldRep_1Byte, /* BOOL */ + kUpb_FieldRep_StringView, /* STRING */ + kUpb_FieldRep_Pointer, /* GROUP */ + kUpb_FieldRep_Pointer, /* MESSAGE */ + kUpb_FieldRep_StringView, /* BYTES */ + kUpb_FieldRep_4Byte, /* UINT32 */ + kUpb_FieldRep_4Byte, /* ENUM */ + kUpb_FieldRep_4Byte, /* SFIXED32 */ + kUpb_FieldRep_8Byte, /* SFIXED64 */ + kUpb_FieldRep_4Byte, /* SINT32 */ + kUpb_FieldRep_8Byte, /* SINT64 */ }; field->mode = kUpb_FieldMode_Scalar | - (sizes[field->descriptortype] << upb_FieldRep_Shift); + (sizes[field->descriptortype] << kUpb_FieldRep_Shift); } if (upb_FieldDef_IsPacked(f)) { - field->mode |= upb_LabelFlags_IsPacked; + field->mode |= kUpb_LabelFlags_IsPacked; } if (upb_FieldDef_IsExtension(f)) { - field->mode |= upb_LabelFlags_IsExtension; + field->mode |= kUpb_LabelFlags_IsExtension; } } @@ -1500,12 +1500,12 @@ static void make_layout(symtab_addctx* ctx, const upb_MessageDef* m) { if (upb_MessageDef_ExtensionRangeCount(m) > 0) { if (google_protobuf_MessageOptions_message_set_wire_format(m->opts)) { - l->ext = upb_ExtMode_IsMessageSet; + l->ext = kUpb_ExtMode_IsMessageSet; } else { - l->ext = upb_ExtMode_Extendable; + l->ext = kUpb_ExtMode_Extendable; } } else { - l->ext = upb_ExtMode_NonExtendable; + l->ext = kUpb_ExtMode_NonExtendable; } /* TODO(haberman): initialize fast tables so that reflection-based parsing diff --git a/upb/encode.c b/upb/encode.c index c339c9d81e..a60d58a96e 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -288,7 +288,7 @@ static void encode_array(upb_encstate* e, const upb_Message* msg, const upb_MiniTable_Sub* subs, const upb_MiniTable_Field* f) { const upb_Array* arr = *UPB_PTR_AT(msg, f->offset, upb_Array*); - bool packed = f->mode & upb_LabelFlags_IsPacked; + bool packed = f->mode & kUpb_LabelFlags_IsPacked; size_t pre_len = e->limit - e->ptr; if (arr == NULL || arr->len == 0) { @@ -442,23 +442,29 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg, if (f->presence == 0) { /* Proto3 presence or map/array. */ const void* mem = UPB_PTR_AT(msg, f->offset, void); - switch (f->mode >> upb_FieldRep_Shift) { - case upb_FieldRep_1Byte: { + switch (f->mode >> kUpb_FieldRep_Shift) { + case kUpb_FieldRep_1Byte: { char ch; memcpy(&ch, mem, 1); return ch != 0; } - case upb_FieldRep_4Byte: { +#if UINTPTR_MAX == 0xffffffff + case upb_FieldRep_Pointer: +#endif + case kUpb_FieldRep_4Byte: { uint32_t u32; memcpy(&u32, mem, 4); return u32 != 0; } - case upb_FieldRep_8Byte: { +#if UINTPTR_MAX != 0xffffffff + case kUpb_FieldRep_Pointer: +#endif + case kUpb_FieldRep_8Byte: { uint64_t u64; memcpy(&u64, mem, 8); return u64 != 0; } - case upb_FieldRep_StringView: { + case kUpb_FieldRep_StringView: { const upb_StringView* str = (const upb_StringView*)mem; return str->size != 0; } @@ -532,7 +538,7 @@ static void encode_message(upb_encstate* e, const upb_Message* msg, } } - if (m->ext != upb_ExtMode_NonExtendable) { + if (m->ext != kUpb_ExtMode_NonExtendable) { /* Encode all extensions together. Unlike C++, we do not attempt to keep * these in field number order relative to normal fields or even to each * other. */ @@ -541,7 +547,7 @@ static void encode_message(upb_encstate* e, const upb_Message* msg, if (ext_count) { const upb_Message_Extension* end = ext + ext_count; for (; ext != end; ext++) { - if (UPB_UNLIKELY(m->ext == upb_ExtMode_IsMessageSet)) { + if (UPB_UNLIKELY(m->ext == kUpb_ExtMode_IsMessageSet)) { encode_msgset_item(e, ext); } else { encode_field(e, &ext->data, &ext->ext->sub, &ext->ext->field); diff --git a/upb/msg_internal.h b/upb/msg_internal.h index 1cad56df7e..88f894fa71 100644 --- a/upb/msg_internal.h +++ b/upb/msg_internal.h @@ -75,7 +75,7 @@ typedef struct { uint16_t submsg_index; // undefined if descriptortype != MESSAGE/GROUP/ENUM uint8_t descriptortype; uint8_t mode; /* upb_FieldMode | upb_LabelFlags | - (upb_FieldRep << upb_FieldRep_Shift) */ + (upb_FieldRep << kUpb_FieldRep_Shift) */ } upb_MiniTable_Field; typedef enum { @@ -87,28 +87,21 @@ typedef enum { } upb_FieldMode; /* Extra flags on the mode field. */ -enum upb_LabelFlags { - upb_LabelFlags_IsPacked = 4, - upb_LabelFlags_IsExtension = 8, -}; +typedef enum { + kUpb_LabelFlags_IsPacked = 4, + kUpb_LabelFlags_IsExtension = 8, +} upb_LabelFlags; /* Representation in the message. Derivable from descriptortype and mode, but * fast access helps the serializer. */ -enum upb_FieldRep { - upb_FieldRep_1Byte = 0, - upb_FieldRep_4Byte = 1, - upb_FieldRep_8Byte = 2, - upb_FieldRep_StringView = 3, - -#if UINTPTR_MAX == 0xffffffff - upb_FieldRep_Pointer = upb_FieldRep_4Byte, -#else - upb_FieldRep_Pointer = upb_FieldRep_8Byte, -#endif - - upb_FieldRep_Shift = - 6, /* Bit offset of the rep in upb_MiniTable_Field.mode */ -}; +typedef enum { + kUpb_FieldRep_1Byte = 0, + kUpb_FieldRep_4Byte = 1, + kUpb_FieldRep_Pointer = 2, + kUpb_FieldRep_StringView = 3, + kUpb_FieldRep_8Byte = 4, + kUpb_FieldRep_Shift = 5, // Bit offset of the rep in upb_MiniTable_Field.mode +} upb_FieldRep; UPB_INLINE upb_FieldMode upb_FieldMode_Get(const upb_MiniTable_Field* field) { return (upb_FieldMode)(field->mode & 3); @@ -160,11 +153,15 @@ typedef union { } upb_MiniTable_Sub; typedef enum { - upb_ExtMode_NonExtendable = 0, // Non-extendable message. - upb_ExtMode_Extendable = 1, // Normal extendable message. - upb_ExtMode_IsMessageSet = 2, // MessageSet message. - upb_ExtMode_IsMessageSet_ITEM = + kUpb_ExtMode_NonExtendable = 0, // Non-extendable message. + kUpb_ExtMode_Extendable = 1, // Normal extendable message. + kUpb_ExtMode_IsMessageSet = 2, // MessageSet message. + kUpb_ExtMode_IsMessageSet_ITEM = 3, // MessageSet item (temporary only, see decode.c) + + // During table building we steal a bit to indicate that the message is a map + // entry. *Only* used during table building! + kUpb_ExtMode_IsMapEntry = 4, } upb_ExtMode; /* MessageSet wire format is: @@ -225,8 +222,7 @@ UPB_INLINE uint64_t upb_MiniTable_requiredmask(const upb_MiniTable* l) { return ((1ULL << n) - 1) << 1; } -/** upb_ExtensionRegistry - * ****************************************************************/ +/** upb_ExtensionRegistry *****************************************************/ /* Adds the given extension info for message type |l| and field number |num| * into the registry. Returns false if this message type and field number were @@ -241,8 +237,7 @@ const upb_MiniTable_Extension* _upb_extreg_get(const upb_ExtensionRegistry* r, const upb_MiniTable* l, uint32_t num); -/** upb_Message - * *******************************************************************/ +/** upb_Message ***************************************************************/ /* Internal members of a upb_Message that track unknown fields and/or * extensions. We can change this without breaking binary compatibility. We put @@ -315,8 +310,7 @@ void _upb_Message_DiscardUnknown_shallow(upb_Message* msg); bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len, upb_Arena* arena); -/** upb_Message_Extension - * ***************************************************************/ +/** upb_Message_Extension *****************************************************/ /* The internal representation of an extension is self-describing: it contains * enough information that we can serialize it to binary format without needing @@ -773,8 +767,7 @@ UPB_INLINE void _upb_msg_map_set_value(void* msg, const void* val, } } -/** _upb_mapsorter - * *************************************************************/ +/** _upb_mapsorter ************************************************************/ /* _upb_mapsorter sorts maps and provides ordered iteration over the entries. * Since maps can be recursive (map values can be messages which contain other diff --git a/upbc/protoc-gen-upb.cc b/upbc/protoc-gen-upb.cc index def169f91c..7e52153baf 100644 --- a/upbc/protoc-gen-upb.cc +++ b/upbc/protoc-gen-upb.cc @@ -278,20 +278,20 @@ std::string SizeLg2(const protobuf::FieldDescriptor* field) { std::string SizeRep(const protobuf::FieldDescriptor* field) { switch (field->cpp_type()) { case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: - return "upb_FieldRep_Pointer"; + return "kUpb_FieldRep_Pointer"; case protobuf::FieldDescriptor::CPPTYPE_ENUM: case protobuf::FieldDescriptor::CPPTYPE_FLOAT: case protobuf::FieldDescriptor::CPPTYPE_INT32: case protobuf::FieldDescriptor::CPPTYPE_UINT32: - return "upb_FieldRep_4Byte"; + return "kUpb_FieldRep_4Byte"; case protobuf::FieldDescriptor::CPPTYPE_BOOL: - return "upb_FieldRep_1Byte"; + return "kUpb_FieldRep_1Byte"; case protobuf::FieldDescriptor::CPPTYPE_DOUBLE: case protobuf::FieldDescriptor::CPPTYPE_INT64: case protobuf::FieldDescriptor::CPPTYPE_UINT64: - return "upb_FieldRep_8Byte"; + return "kUpb_FieldRep_8Byte"; case protobuf::FieldDescriptor::CPPTYPE_STRING: - return "upb_FieldRep_StringView"; + return "kUpb_FieldRep_StringView"; default: fprintf(stderr, "Unexpected type"); abort(); @@ -1154,24 +1154,24 @@ void WriteField(const protobuf::FieldDescriptor* field, std::string rep; if (field->is_map()) { mode = "kUpb_FieldMode_Map"; - rep = "upb_FieldRep_Pointer"; + rep = "kUpb_FieldRep_Pointer"; } else if (field->is_repeated()) { mode = "kUpb_FieldMode_Array"; - rep = "upb_FieldRep_Pointer"; + rep = "kUpb_FieldRep_Pointer"; } else { mode = "kUpb_FieldMode_Scalar"; rep = SizeRep(field); } if (field->is_packed()) { - absl::StrAppend(&mode, " | upb_LabelFlags_IsPacked"); + absl::StrAppend(&mode, " | kUpb_LabelFlags_IsPacked"); } if (field->is_extension()) { - absl::StrAppend(&mode, " | upb_LabelFlags_IsExtension"); + absl::StrAppend(&mode, " | kUpb_LabelFlags_IsExtension"); } - output("{$0, $1, $2, $3, $4, $5 | ($6 << upb_FieldRep_Shift)}", + output("{$0, $1, $2, $3, $4, $5 | ($6 << kUpb_FieldRep_Shift)}", field->number(), offset, presence, submsg_index, TableDescriptorType(field), mode, rep); } @@ -1275,13 +1275,13 @@ void WriteMessage(const protobuf::Descriptor* message, Output& output, table_mask = (table.size() - 1) << 3; } - std::string msgext = "upb_ExtMode_NonExtendable"; + std::string msgext = "kUpb_ExtMode_NonExtendable"; if (message->extension_range_count()) { if (message->options().message_set_wire_format()) { - msgext = "upb_ExtMode_IsMessageSet"; + msgext = "kUpb_ExtMode_IsMessageSet"; } else { - msgext = "upb_ExtMode_Extendable"; + msgext = "kUpb_ExtMode_Extendable"; } } From 44363393f3dcbadb00bce89305b6ff70f6ab9f29 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 18:07:12 -0800 Subject: [PATCH 3/9] Backed out a functional refactoring. --- upb/encode.c | 6 ------ upb/msg_internal.h | 13 ++++++++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/upb/encode.c b/upb/encode.c index a60d58a96e..67f90f37a2 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -448,17 +448,11 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg, memcpy(&ch, mem, 1); return ch != 0; } -#if UINTPTR_MAX == 0xffffffff - case upb_FieldRep_Pointer: -#endif case kUpb_FieldRep_4Byte: { uint32_t u32; memcpy(&u32, mem, 4); return u32 != 0; } -#if UINTPTR_MAX != 0xffffffff - case kUpb_FieldRep_Pointer: -#endif case kUpb_FieldRep_8Byte: { uint64_t u64; memcpy(&u64, mem, 8); diff --git a/upb/msg_internal.h b/upb/msg_internal.h index 88f894fa71..9960923ea1 100644 --- a/upb/msg_internal.h +++ b/upb/msg_internal.h @@ -97,10 +97,17 @@ typedef enum { typedef enum { kUpb_FieldRep_1Byte = 0, kUpb_FieldRep_4Byte = 1, - kUpb_FieldRep_Pointer = 2, + kUpb_FieldRep_8Byte = 2, kUpb_FieldRep_StringView = 3, - kUpb_FieldRep_8Byte = 4, - kUpb_FieldRep_Shift = 5, // Bit offset of the rep in upb_MiniTable_Field.mode + +#if UINTPTR_MAX == 0xffffffff + kUpb_FieldRep_Pointer = kUpb_FieldRep_4Byte, +#else + kUpb_FieldRep_Pointer = kUpb_FieldRep_8Byte, +#endif + + kUpb_FieldRep_Shift = + 6, /* Bit offset of the rep in upb_MiniTable_Field.mode */ } upb_FieldRep; UPB_INLINE upb_FieldMode upb_FieldMode_Get(const upb_MiniTable_Field* field) { From 4586eea76624581ab8a9433ce6f3feb09b48e68e Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 18:10:09 -0800 Subject: [PATCH 4/9] Fixed clang-format. --- upb/def.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/upb/def.c b/upb/def.c index 2dcc793771..712d68d638 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1433,7 +1433,7 @@ static void fill_fieldlayout(upb_MiniTable_Field* field, } else { /* Maps descriptor type -> elem_size_lg2. */ static const uint8_t sizes[] = { - -1, /* invalid descriptor type */ + -1, /* invalid descriptor type */ kUpb_FieldRep_8Byte, /* DOUBLE */ kUpb_FieldRep_4Byte, /* FLOAT */ kUpb_FieldRep_8Byte, /* INT64 */ From 8ede0d552d4f058dc1c6ac0e2ecd4d36cee14372 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 18:33:27 -0800 Subject: [PATCH 5/9] Tests are passing. --- BUILD | 22 ++++++++++++++++++++++ cmake/CMakeLists.txt | 6 ++++++ upb/encode.c | 6 ++++++ upb/mini_table.c | 12 ++++++------ upb/msg_internal.h | 15 +++------------ 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/BUILD b/BUILD index 31e93b798b..1158e48d0e 100644 --- a/BUILD +++ b/BUILD @@ -109,6 +109,28 @@ cc_library( ], ) +cc_library( + name = "mini_table", + srcs = ["upb/mini_table.c"], + hdrs = [ + "upb/mini_table.h", + "upb/mini_table.hpp", + ], + copts = UPB_DEFAULT_COPTS, + visibility = ["//visibility:public"], + deps = [":upb"], +) + +cc_test( + name = "mini_table_test", + srcs = ["upb/mini_table_test.cc"], + deps = [ + ":mini_table", + "@com_google_googletest//:gtest_main", + "@com_google_absl//absl/container:flat_hash_set", + ], +) + cc_library( name = "fastdecode", srcs = [ diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 30e6e93c06..a06bd604ac 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -81,6 +81,12 @@ target_link_libraries(upb fastdecode port /third_party/utf8_range) +add_library(mini_table + ../upb/mini_table.c + ../upb/mini_table.h + ../upb/mini_table.hpp) +target_link_libraries(mini_table + upb) add_library(fastdecode ../upb/decode.h ../upb/decode_fast.c diff --git a/upb/encode.c b/upb/encode.c index 67f90f37a2..a60d58a96e 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -448,11 +448,17 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg, memcpy(&ch, mem, 1); return ch != 0; } +#if UINTPTR_MAX == 0xffffffff + case upb_FieldRep_Pointer: +#endif case kUpb_FieldRep_4Byte: { uint32_t u32; memcpy(&u32, mem, 4); return u32 != 0; } +#if UINTPTR_MAX != 0xffffffff + case kUpb_FieldRep_Pointer: +#endif case kUpb_FieldRep_8Byte: { uint64_t u64; memcpy(&u64, mem, 8); diff --git a/upb/mini_table.c b/upb/mini_table.c index 4347b1515e..44fc5c7ff3 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -418,9 +418,9 @@ static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch, static void upb_MtDecoder_ModifyField(upb_MtDecoder* d, uint32_t mod, upb_MiniTable_Field* field) { if (mod & kUpb_EncodedFieldModifier_IsUnpacked) { - field->mode &= ~upb_LabelFlags_IsPacked; + field->mode &= ~kUpb_LabelFlags_IsPacked; } else { - field->mode |= upb_LabelFlags_IsPacked; + field->mode |= kUpb_LabelFlags_IsPacked; } bool singular = mod & kUpb_EncodedFieldModifier_IsProto3Singular; @@ -802,7 +802,7 @@ upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, decoder.table->size = 0; decoder.table->field_count = 0; - decoder.table->ext = upb_ExtMode_NonExtendable; + decoder.table->ext = kUpb_ExtMode_NonExtendable; decoder.table->dense_below = 0; decoder.table->table_mask = 0; decoder.table->required_count = 0; @@ -825,7 +825,7 @@ upb_MiniTable* upb_MiniTable_BuildMessageSet(upb_MiniTablePlatform platform, ret->size = 0; ret->field_count = 0; - ret->ext = upb_ExtMode_IsMessageSet; + ret->ext = kUpb_ExtMode_IsMessageSet; ret->dense_below = 0; ret->table_mask = 0; ret->required_count = 0; @@ -863,7 +863,7 @@ upb_MiniTable* upb_MiniTable_BuildMapEntry(upb_FieldType key_type, ret->size = UPB_ALIGN_UP(2 * field_size, 8); ret->field_count = 2; - ret->ext = upb_ExtMode_NonExtendable | upb_ExtMode_IsMapEntry; + ret->ext = kUpb_ExtMode_NonExtendable | kUpb_ExtMode_IsMapEntry; ret->dense_below = 2; ret->table_mask = 0; ret->required_count = 0; @@ -919,7 +919,7 @@ void upb_MiniTable_SetSubMessage(upb_MiniTable* table, const upb_MiniTable* sub) { assert((uintptr_t)table->fields <= (uintptr_t)field && (uintptr_t)field < (uintptr_t)(table->fields + table->field_count)); - if (sub->ext & upb_ExtMode_IsMapEntry) { + if (sub->ext & kUpb_ExtMode_IsMapEntry) { field->mode = (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift) | kUpb_FieldMode_Map; } diff --git a/upb/msg_internal.h b/upb/msg_internal.h index 9960923ea1..35fe565cbf 100644 --- a/upb/msg_internal.h +++ b/upb/msg_internal.h @@ -92,22 +92,13 @@ typedef enum { kUpb_LabelFlags_IsExtension = 8, } upb_LabelFlags; -/* Representation in the message. Derivable from descriptortype and mode, but - * fast access helps the serializer. */ typedef enum { kUpb_FieldRep_1Byte = 0, kUpb_FieldRep_4Byte = 1, - kUpb_FieldRep_8Byte = 2, + kUpb_FieldRep_Pointer = 2, kUpb_FieldRep_StringView = 3, - -#if UINTPTR_MAX == 0xffffffff - kUpb_FieldRep_Pointer = kUpb_FieldRep_4Byte, -#else - kUpb_FieldRep_Pointer = kUpb_FieldRep_8Byte, -#endif - - kUpb_FieldRep_Shift = - 6, /* Bit offset of the rep in upb_MiniTable_Field.mode */ + kUpb_FieldRep_8Byte = 4, + kUpb_FieldRep_Shift = 5, // Bit offset of the rep in upb_MiniTable_Field.mode } upb_FieldRep; UPB_INLINE upb_FieldMode upb_FieldMode_Get(const upb_MiniTable_Field* field) { From 05d23ea95583c74d74783228b47bd0aea99480e4 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 20:35:28 -0800 Subject: [PATCH 6/9] Fixed UBSAN errors. --- upb/encode.c | 2 +- upb/mini_table.c | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/upb/encode.c b/upb/encode.c index a60d58a96e..ff84275ffb 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -449,7 +449,7 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg, return ch != 0; } #if UINTPTR_MAX == 0xffffffff - case upb_FieldRep_Pointer: + case kUpb_FieldRep_Pointer: #endif case kUpb_FieldRep_4Byte: { uint32_t u32; diff --git a/upb/mini_table.c b/upb/mini_table.c index 44fc5c7ff3..e48fb675f2 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -561,7 +561,7 @@ static void upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr, size_t len, uint32_t last_field_number = 0; bool need_dense_below = d->table != NULL; - d->end = ptr + len; + d->end = UPB_PTRADD(ptr, len); while (ptr < d->end) { char ch = *ptr++; @@ -650,8 +650,10 @@ static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) { upb_MtDecoder_PushItem(d, item); } - qsort(d->vec.data, d->vec.size, sizeof(*d->vec.data), - upb_MtDecoder_CompareFields); + if (d->vec.size) { + qsort(d->vec.data, d->vec.size, sizeof(*d->vec.data), + upb_MtDecoder_CompareFields); + } return true; } @@ -735,7 +737,7 @@ size_t upb_MtDecoder_Place(upb_MtDecoder* d, upb_FieldRep rep) { } static bool upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { - upb_LayoutItem* end = d->vec.data + d->vec.size; + upb_LayoutItem* end = UPB_PTRADD(d->vec.data, d->vec.size); // Compute offsets. for (upb_LayoutItem* item = d->vec.data; item < end; item++) { From f5246b70fdf30e4222bb658439b4780d97302ee5 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Mar 2022 20:36:27 -0800 Subject: [PATCH 7/9] clang-format --- upb/mini_table.c | 42 +++++++++++++++++++----------------------- upb/mini_table.h | 8 +++----- upb/mini_table_test.cc | 8 ++++---- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/upb/mini_table.c b/upb/mini_table.c index e48fb675f2..3efe15cace 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -573,7 +573,8 @@ static void upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr, size_t len, upb_MiniTable_SetField(d, ch, field, msg_modifiers, sub_count); } else if (kUpb_EncodedValue_MinModifier <= ch && ch <= kUpb_EncodedValue_MaxModifier) { - ptr = upb_MtDecoder_ParseModifier(d, ptr, ch, *field_count, &msg_modifiers); + ptr = + upb_MtDecoder_ParseModifier(d, ptr, ch, *field_count, &msg_modifiers); } else if (ch == kUpb_EncodedValue_End) { if (!d->table) { upb_MtDecoder_ErrorFormat(d, "Extensions cannot have oneofs."); @@ -686,19 +687,16 @@ static void upb_MtDecoder_AssignHasbits(upb_MiniTable* ret) { ret->size = upb_MiniTable_DivideRoundUp(last_hasbit, 8); } -size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep, upb_MiniTablePlatform platform) { +size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep, + upb_MiniTablePlatform platform) { static const uint8_t kRepToSize32[] = { - [kUpb_FieldRep_1Byte] = 1, - [kUpb_FieldRep_4Byte] = 4, - [kUpb_FieldRep_Pointer] = 4, - [kUpb_FieldRep_StringView] = 8, + [kUpb_FieldRep_1Byte] = 1, [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 4, [kUpb_FieldRep_StringView] = 8, [kUpb_FieldRep_8Byte] = 8, }; static const uint8_t kRepToSize64[] = { - [kUpb_FieldRep_1Byte] = 1, - [kUpb_FieldRep_4Byte] = 4, - [kUpb_FieldRep_Pointer] = 8, - [kUpb_FieldRep_StringView] = 16, + [kUpb_FieldRep_1Byte] = 1, [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 8, [kUpb_FieldRep_StringView] = 16, [kUpb_FieldRep_8Byte] = 8, }; assert(sizeof(upb_StringView) == @@ -707,19 +705,16 @@ size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep, upb_MiniTablePlatform platform) : kRepToSize64[rep]; } -size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep, upb_MiniTablePlatform platform) { +size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep, + upb_MiniTablePlatform platform) { static const uint8_t kRepToAlign32[] = { - [kUpb_FieldRep_1Byte] = 1, - [kUpb_FieldRep_4Byte] = 4, - [kUpb_FieldRep_Pointer] = 4, - [kUpb_FieldRep_StringView] = 4, + [kUpb_FieldRep_1Byte] = 1, [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 4, [kUpb_FieldRep_StringView] = 4, [kUpb_FieldRep_8Byte] = 8, }; static const uint8_t kRepToAlign64[] = { - [kUpb_FieldRep_1Byte] = 1, - [kUpb_FieldRep_4Byte] = 4, - [kUpb_FieldRep_Pointer] = 8, - [kUpb_FieldRep_StringView] = 8, + [kUpb_FieldRep_1Byte] = 1, [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_Pointer] = 8, [kUpb_FieldRep_StringView] = 8, [kUpb_FieldRep_8Byte] = 8, }; assert(UPB_ALIGN_OF(upb_StringView) == @@ -781,7 +776,8 @@ static bool upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, upb_MiniTablePlatform platform, upb_Arena* arena, void** buf, - size_t* buf_size, upb_Status* status) { + size_t* buf_size, + upb_Status* status) { upb_MtDecoder decoder = { .platform = platform, .vec = @@ -843,7 +839,8 @@ upb_MiniTable* upb_MiniTable_BuildMapEntry(upb_FieldType key_type, if (!ret || !fields) return NULL; upb_MiniTable_Sub* subs = NULL; - if (value_type == kUpb_FieldType_Message || value_type == kUpb_FieldType_Group) { + if (value_type == kUpb_FieldType_Message || + value_type == kUpb_FieldType_Group) { subs = upb_Arena_Malloc(arena, sizeof(*subs)); if (!subs) return NULL; } @@ -929,8 +926,7 @@ void upb_MiniTable_SetSubMessage(upb_MiniTable* table, table_sub->submsg = sub; } -void upb_MiniTable_SetSubEnum(upb_MiniTable* table, - upb_MiniTable_Field* field, +void upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTable_Field* field, const upb_MiniTable_Enum* sub) { assert((uintptr_t)table->fields <= (uintptr_t)field && (uintptr_t)field < (uintptr_t)(table->fields + table->field_count)); diff --git a/upb/mini_table.h b/upb/mini_table.h index 0f82875b1a..b8d607f944 100644 --- a/upb/mini_table.h +++ b/upb/mini_table.h @@ -101,7 +101,6 @@ char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* buf); char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* buf, uint32_t field_num); - /** upb_MiniTable *************************************************************/ typedef enum { @@ -121,8 +120,7 @@ upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len, void upb_MiniTable_SetSubMessage(upb_MiniTable* table, upb_MiniTable_Field* field, const upb_MiniTable* sub); -void upb_MiniTable_SetSubEnum(upb_MiniTable* table, - upb_MiniTable_Field* field, +void upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTable_Field* field, const upb_MiniTable_Enum* sub); upb_MiniTable_Extension* upb_MiniTable_BuildExtensions(const char* data, @@ -154,9 +152,9 @@ upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, size_t* buf_size, upb_Status* status); #ifdef __cplusplus -} /* extern "C" */ +} /* extern "C" */ #endif #include "upb/port_undef.inc" -#endif /* UPB_MINI_TABLE_H_ */ +#endif /* UPB_MINI_TABLE_H_ */ diff --git a/upb/mini_table_test.cc b/upb/mini_table_test.cc index 94d2814eda..9425608227 100644 --- a/upb/mini_table_test.cc +++ b/upb/mini_table_test.cc @@ -50,7 +50,7 @@ TEST_P(MiniTableTest, AllScalarTypes) { upb::MtDataEncoder e; ASSERT_TRUE(e.StartMessage(0)); int count = 0; - for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) { ASSERT_TRUE(e.PutField(static_cast(i), i, 0)); count++; } @@ -77,7 +77,7 @@ TEST_P(MiniTableTest, AllRepeatedTypes) { upb::MtDataEncoder e; ASSERT_TRUE(e.StartMessage(0)); int count = 0; - for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) { ASSERT_TRUE(e.PutField(static_cast(i), i, kUpb_FieldModifier_IsRepeated)); count++; @@ -135,12 +135,12 @@ TEST_P(MiniTableTest, AllScalarTypesOneof) { upb::MtDataEncoder e; ASSERT_TRUE(e.StartMessage(0)); int count = 0; - for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) { ASSERT_TRUE(e.PutField(static_cast(i), i, 0)); count++; } ASSERT_TRUE(e.StartOneof()); - for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) { + for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) { ASSERT_TRUE(e.PutOneofField(i)); } fprintf(stderr, "YO: %s\n", e.data().c_str()); From 84054360442f13de158cd252d39e30ad4c0733c3 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 6 Mar 2022 10:44:47 -0800 Subject: [PATCH 8/9] Addressed PR comments. --- benchmarks/compare.py | 4 ++-- upb/mini_table.c | 53 +++++++++++++++++++++++++----------------- upb/mini_table.h | 6 ++++- upb/mini_table.hpp | 1 + upb/mini_table_test.cc | 10 ++++---- upb/msg_internal.h | 4 ++-- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/benchmarks/compare.py b/benchmarks/compare.py index 55ed699582..8c27ea72b4 100755 --- a/benchmarks/compare.py +++ b/benchmarks/compare.py @@ -81,8 +81,8 @@ def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False): print("{} {} {} ns/op".format(*values), file=f) Run("sort {} -o {} ".format(txt_filename, txt_filename)) - Run("CC=clang bazel build -c opt --copt=-g --copt=-march=native tests:conformance_upb" + extra_args) - Run("cp -f bazel-bin/tests/conformance_upb {}.bin".format(outbase)) + Run("CC=clang bazel build -c opt --copt=-g --copt=-march=native :conformance_upb" + extra_args) + Run("cp -f bazel-bin/conformance_upb {}.bin".format(outbase)) baseline = "main" diff --git a/upb/mini_table.c b/upb/mini_table.c index 3efe15cace..757f40cbfb 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -60,11 +60,11 @@ typedef enum { } upb_EncodedType; typedef enum { - kUpb_EncodedFieldModifier_IsUnpacked = 1, - kUpb_EncodedFieldModifier_JspbString = 2, + kUpb_EncodedFieldModifier_IsUnpacked = 1 << 0, + kUpb_EncodedFieldModifier_JspbString = 1 << 1, // upb only. - kUpb_EncodedFieldModifier_IsProto3Singular = 4, - kUpb_EncodedFieldModifier_IsRequired = 8, + kUpb_EncodedFieldModifier_IsProto3Singular = 1 << 2, + kUpb_EncodedFieldModifier_IsRequired = 1 << 3, } upb_EncodedFieldModifier; enum { @@ -77,6 +77,8 @@ enum { kUpb_EncodedValue_MaxSkip = '~', kUpb_EncodedValue_OneofSeparator = '~', kUpb_EncodedValue_FieldSeparator = '|', + kUpb_EncodedValue_MinOneofField = ' ', + kUpb_EncodedValue_MaxOneofField = 'b', }; char upb_ToBase92(int8_t ch) { @@ -90,7 +92,7 @@ char upb_ToBase92(int8_t ch) { 'w', 'x', 'y', 'z', '{', '|', '}', '~', }; - assert(0 <= ch && ch < 92); + UPB_ASSERT(0 <= ch && ch < 92); return kUpb_ToBase92[ch]; } @@ -139,6 +141,7 @@ static char* upb_MtDataEncoder_Put(upb_MtDataEncoder* e, char* ptr, char ch) { static char* upb_MtDataEncoder_PutBase92Varint(upb_MtDataEncoder* e, char* ptr, uint32_t val, int min, int max) { int shift = _upb_Log2Ceiling(upb_FromBase92(max) - upb_FromBase92(min) + 1); + UPB_ASSERT(shift <= 6); uint32_t mask = (1 << shift) - 1; do { uint32_t bits = val & mask; @@ -186,7 +189,7 @@ char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr, if (field_num <= in->last_field_num) return NULL; if (in->last_field_num + 1 != field_num) { // Put skip. - assert(field_num > in->last_field_num); + UPB_ASSERT(field_num > in->last_field_num); uint32_t skip = field_num - in->last_field_num; ptr = upb_MtDataEncoder_PutBase92Varint( e, ptr, skip, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip); @@ -197,6 +200,8 @@ char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr, // Put field type. int encoded_type = kUpb_TypeToEncoded[type]; if (modifiers & kUpb_FieldModifier_IsRepeated) { + // Repeated fields shift the type number up (unlike other modifiers which + // are bit flags). encoded_type += kUpb_EncodedType_RepeatedBase; } ptr = upb_MtDataEncoder_Put(e, ptr, encoded_type); @@ -272,11 +277,9 @@ typedef struct { // Index of the corresponding field. When this is a oneof field, the field's // offset will be the index of the next field in a linked list. uint16_t field_index; + uint16_t offset; upb_FieldRep rep; upb_LayoutItemType type; - - // Used for temporary storage while assigning offsets (internal only). - uint16_t offset; } upb_LayoutItem; typedef struct { @@ -329,16 +332,19 @@ static const char* upb_MiniTable_DecodeBase92Varint(upb_MtDecoder* d, uint32_t* out_val) { uint32_t val = 0; uint32_t shift = 0; + const int bits_per_char = + _upb_Log2Ceiling(upb_FromBase92(max) - upb_FromBase92(min)); char ch = first_ch; while (1) { uint32_t bits = upb_FromBase92(ch) - upb_FromBase92(min); + UPB_ASSERT(shift < 32 - bits_per_char); val |= bits << shift; if (ptr == d->end || *ptr < min || max < *ptr) { *out_val = val; return ptr; } ch = *ptr++; - shift += _upb_Log2Ceiling(upb_FromBase92(max) - upb_FromBase92(min)); + shift += bits_per_char; } } @@ -475,8 +481,9 @@ static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d, char first_ch, upb_LayoutItem* item) { uint32_t field_num; - ptr = upb_MiniTable_DecodeBase92Varint(d, ptr, first_ch, upb_ToBase92(0), - upb_ToBase92(63), &field_num); + ptr = upb_MiniTable_DecodeBase92Varint( + d, ptr, first_ch, kUpb_EncodedValue_MinOneofField, + kUpb_EncodedValue_MaxOneofField, &field_num); upb_MiniTable_Field* f = (void*)upb_MiniTable_FindFieldByNumber(d->table, field_num); @@ -615,7 +622,9 @@ static void upb_MtDecoder_ParseMessage(upb_MtDecoder* d, const char* data, &d->table->field_count, &sub_count); // Return unused memory from fields array. - assert(d->table->field_count <= len); + UPB_ASSERT(d->table->field_count <= len); + UPB_ASSERT( + _upb_Arena_IsLastAlloc(d->arena, d->fields, sizeof(*d->fields) * len)); d->fields = upb_Arena_Realloc(d->arena, d->fields, sizeof(*d->fields) * len, sizeof(*d->fields) * d->table->field_count); d->table->fields = d->fields; @@ -699,8 +708,8 @@ size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep, [kUpb_FieldRep_Pointer] = 8, [kUpb_FieldRep_StringView] = 16, [kUpb_FieldRep_8Byte] = 8, }; - assert(sizeof(upb_StringView) == - UPB_SIZE(kRepToSize32, kRepToSize64)[kUpb_FieldRep_StringView]); + UPB_ASSERT(sizeof(upb_StringView) == + UPB_SIZE(kRepToSize32, kRepToSize64)[kUpb_FieldRep_StringView]); return platform == kUpb_MiniTablePlatform_32Bit ? kRepToSize32[rep] : kRepToSize64[rep]; } @@ -717,8 +726,8 @@ size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep, [kUpb_FieldRep_Pointer] = 8, [kUpb_FieldRep_StringView] = 8, [kUpb_FieldRep_8Byte] = 8, }; - assert(UPB_ALIGN_OF(upb_StringView) == - UPB_SIZE(kRepToAlign32, kRepToAlign64)[kUpb_FieldRep_StringView]); + UPB_ASSERT(UPB_ALIGN_OF(upb_StringView) == + UPB_SIZE(kRepToAlign32, kRepToAlign64)[kUpb_FieldRep_StringView]); return platform == kUpb_MiniTablePlatform_32Bit ? kRepToAlign32[rep] : kRepToAlign64[rep]; } @@ -916,8 +925,9 @@ upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len, void upb_MiniTable_SetSubMessage(upb_MiniTable* table, upb_MiniTable_Field* field, const upb_MiniTable* sub) { - assert((uintptr_t)table->fields <= (uintptr_t)field && - (uintptr_t)field < (uintptr_t)(table->fields + table->field_count)); + UPB_ASSERT((uintptr_t)table->fields <= (uintptr_t)field && + (uintptr_t)field < + (uintptr_t)(table->fields + table->field_count)); if (sub->ext & kUpb_ExtMode_IsMapEntry) { field->mode = (kUpb_FieldRep_Pointer << kUpb_FieldRep_Shift) | kUpb_FieldMode_Map; @@ -928,8 +938,9 @@ void upb_MiniTable_SetSubMessage(upb_MiniTable* table, void upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTable_Field* field, const upb_MiniTable_Enum* sub) { - assert((uintptr_t)table->fields <= (uintptr_t)field && - (uintptr_t)field < (uintptr_t)(table->fields + table->field_count)); + UPB_ASSERT((uintptr_t)table->fields <= (uintptr_t)field && + (uintptr_t)field < + (uintptr_t)(table->fields + table->field_count)); upb_MiniTable_Sub* table_sub = (void*)&table->subs[field->submsg_index]; table_sub->subenum = sub; } diff --git a/upb/mini_table.h b/upb/mini_table.h index b8d607f944..ace5ea9411 100644 --- a/upb/mini_table.h +++ b/upb/mini_table.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2021, Google LLC + * Copyright (c) 2009-2022, Google LLC * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -151,6 +151,10 @@ upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, upb_Arena* arena, void** buf, size_t* buf_size, upb_Status* status); +// For testing only. +char upb_ToBase92(int8_t ch); +char upb_FromBase92(uint8_t ch); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/upb/mini_table.hpp b/upb/mini_table.hpp index b0654387ba..934821141b 100644 --- a/upb/mini_table.hpp +++ b/upb/mini_table.hpp @@ -76,6 +76,7 @@ class MtDataEncoder { bool operator()(T&& func) { char* end = func(buf_); if (!end) return false; + str_.reserve(_upb_Log2Ceiling(str_.size() + (end - buf_))); str_.append(buf_, end - buf_); return true; } diff --git a/upb/mini_table_test.cc b/upb/mini_table_test.cc index 9425608227..7106e5ce2f 100644 --- a/upb/mini_table_test.cc +++ b/upb/mini_table_test.cc @@ -54,7 +54,6 @@ TEST_P(MiniTableTest, AllScalarTypes) { ASSERT_TRUE(e.PutField(static_cast(i), i, 0)); count++; } - fprintf(stderr, "YO: %s\n", e.data().c_str()); upb::Status status; upb_MiniTable* table = upb_MiniTable_Build( e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); @@ -82,7 +81,6 @@ TEST_P(MiniTableTest, AllRepeatedTypes) { kUpb_FieldModifier_IsRepeated)); count++; } - fprintf(stderr, "YO: %s\n", e.data().c_str()); upb::Status status; upb_MiniTable* table = upb_MiniTable_Build( e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); @@ -112,7 +110,6 @@ TEST_P(MiniTableTest, Skips) { ASSERT_TRUE(e.PutField(kUpb_FieldType_Float, field_number, 0)); count++; } - fprintf(stderr, "YO: %s, %zu\n", e.data().c_str(), e.data().size()); upb::Status status; upb_MiniTable* table = upb_MiniTable_Build( e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); @@ -143,7 +140,6 @@ TEST_P(MiniTableTest, AllScalarTypesOneof) { for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) { ASSERT_TRUE(e.PutOneofField(i)); } - fprintf(stderr, "YO: %s\n", e.data().c_str()); upb::Status status; upb_MiniTable* table = upb_MiniTable_Build( e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr()); @@ -170,3 +166,9 @@ TEST_P(MiniTableTest, AllScalarTypesOneof) { INSTANTIATE_TEST_SUITE_P(Platforms, MiniTableTest, testing::Values(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit)); + +TEST(MiniTablePlatformIndependentTest, Base92Roundtrip) { + for (char i = 0; i < 92; i++) { + EXPECT_EQ(i, upb_FromBase92(upb_ToBase92(i))); + } +} diff --git a/upb/msg_internal.h b/upb/msg_internal.h index 35fe565cbf..c1ee62b1c4 100644 --- a/upb/msg_internal.h +++ b/upb/msg_internal.h @@ -95,8 +95,8 @@ typedef enum { typedef enum { kUpb_FieldRep_1Byte = 0, kUpb_FieldRep_4Byte = 1, - kUpb_FieldRep_Pointer = 2, - kUpb_FieldRep_StringView = 3, + kUpb_FieldRep_StringView = 2, + kUpb_FieldRep_Pointer = 3, kUpb_FieldRep_8Byte = 4, kUpb_FieldRep_Shift = 5, // Bit offset of the rep in upb_MiniTable_Field.mode } upb_FieldRep; From 7c4d12e8560ce9aa19dd3a933ed2c95c7f49f575 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 6 Mar 2022 13:00:13 -0800 Subject: [PATCH 9/9] Addressed PR comments. --- upb/mini_table.c | 42 ++++++++++++++++++++++++------------------ upb/mini_table.hpp | 4 +++- upb/msg.c | 6 +++--- upb/msg_internal.h | 3 +++ upb/upb.h | 26 +++++++++++++++++++++++++- 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/upb/mini_table.c b/upb/mini_table.c index 757f40cbfb..834492e7ac 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -265,10 +265,13 @@ const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber( /** Data decoder **************************************************************/ +// Note: we sort by this number when calculating layout order. typedef enum { - kUpb_LayoutItemType_Field, // Non-oneof field data. - kUpb_LayoutItemType_OneofField, // Oneof field data. kUpb_LayoutItemType_OneofCase, // Oneof case. + kUpb_LayoutItemType_OneofField, // Oneof field data. + kUpb_LayoutItemType_Field, // Non-oneof field data. + + kUpb_LayoutItemType_Max = kUpb_LayoutItemType_Field, } upb_LayoutItemType; #define kUpb_LayoutItem_IndexSentinel ((uint16_t)-1) @@ -621,32 +624,34 @@ static void upb_MtDecoder_ParseMessage(upb_MtDecoder* d, const char* data, upb_MtDecoder_Parse(d, data, len, d->fields, sizeof(*d->fields), &d->table->field_count, &sub_count); - // Return unused memory from fields array. - UPB_ASSERT(d->table->field_count <= len); - UPB_ASSERT( - _upb_Arena_IsLastAlloc(d->arena, d->fields, sizeof(*d->fields) * len)); - d->fields = upb_Arena_Realloc(d->arena, d->fields, sizeof(*d->fields) * len, - sizeof(*d->fields) * d->table->field_count); + upb_Arena_ShrinkLast(d->arena, d->fields, sizeof(*d->fields) * len, + sizeof(*d->fields) * d->table->field_count); d->table->fields = d->fields; upb_MtDecoder_AllocateSubs(d, sub_count); } #define UPB_COMPARE_INTEGERS(a, b) ((a) < (b) ? -1 : ((a) == (b) ? 0 : 1)) +#define UPB_COMBINE(rep, ty, idx) (((rep << type_bits) | ty) << idx_bits) | idx int upb_MtDecoder_CompareFields(const void* _a, const void* _b) { const upb_LayoutItem* a = _a; const upb_LayoutItem* b = _b; // Currently we just sort by: - // 1. rep (descending, so largest fields are first) - // 2. is_case (descending, so oneof cases are first) - // 2. field_number (ascending, so smallest numbers are first) - // + // 1. rep (smallest fields first) + // 2. type (oneof cases first) + // 2. field_number (smallest numbers first) // The main goal of this is to reduce space lost to padding. - if (a->rep != b->rep) return UPB_COMPARE_INTEGERS(a->rep, b->rep); - if (a->type != b->type) return UPB_COMPARE_INTEGERS(a->type, b->type); - return UPB_COMPARE_INTEGERS(b->field_index, a->field_index); + // Later we may have more subtle reasons to prefer a different ordering. + const int rep_bits = _upb_Log2Ceiling(kUpb_FieldRep_Max); + const int type_bits = _upb_Log2Ceiling(kUpb_LayoutItemType_Max); + const int idx_bits = (sizeof(a->field_index) * 8); + UPB_ASSERT(idx_bits + rep_bits + type_bits < 32); + uint32_t a_packed = UPB_COMBINE(a->rep, a->type, a->field_index); + uint32_t b_packed = UPB_COMBINE(b->rep, b->type, b->field_index); + return UPB_COMPARE_INTEGERS(a_packed, b_packed); } +#undef UPB_COMBINE #undef UPB_COMPARE_INTEGERS static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) { @@ -656,7 +661,8 @@ static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) { upb_MiniTable_Field* f = &d->fields[i]; if (f->offset >= kOneofBase) continue; upb_LayoutItem item = {.field_index = i, - .rep = f->mode >> kUpb_FieldRep_Shift}; + .rep = f->mode >> kUpb_FieldRep_Shift, + .type = kUpb_LayoutItemType_Field}; upb_MtDecoder_PushItem(d, item); } @@ -756,6 +762,7 @@ static bool upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { while (true) { f->presence = ~item->offset; if (f->offset == kUpb_LayoutItem_IndexSentinel) break; + UPB_ASSERT(f->offset - kOneofBase < d->table->field_count); f = &d->fields[f->offset - kOneofBase]; } } @@ -903,8 +910,7 @@ upb_MiniTable_Extension* upb_MiniTable_BuildExtensions(const char* data, exts = upb_Arena_Malloc(arena, len); upb_MtDecoder_CheckOutOfMemory(&decoder, exts); upb_MtDecoder_Parse(&decoder, data, len, exts, sizeof(*exts), &count, NULL); - exts = upb_Arena_Realloc(arena, exts, sizeof(*exts) * len, - sizeof(*exts) * count); + upb_Arena_ShrinkLast(arena, exts, sizeof(*exts) * len, sizeof(*exts) * count); done: *ext_count = count; diff --git a/upb/mini_table.hpp b/upb/mini_table.hpp index 934821141b..06405f9da3 100644 --- a/upb/mini_table.hpp +++ b/upb/mini_table.hpp @@ -76,7 +76,9 @@ class MtDataEncoder { bool operator()(T&& func) { char* end = func(buf_); if (!end) return false; - str_.reserve(_upb_Log2Ceiling(str_.size() + (end - buf_))); + // C++ does not guarantee that string has doubling growth behavior, but + // we need it to avoid O(n^2). + str_.reserve(_upb_Log2CeilingSize(str_.size() + (end - buf_))); str_.append(buf_, end - buf_); return true; } diff --git a/upb/msg.c b/upb/msg.c index 3734fc5491..a1efe1165c 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -55,7 +55,7 @@ static bool realloc_internal(upb_Message* msg, size_t need, upb_Arena* arena) { upb_Message_Internal* in = upb_Message_Getinternal(msg); if (!in->internal) { /* No internal data, allocate from scratch. */ - size_t size = UPB_MAX(128, _upb_Log2Ceilingsize(need + overhead)); + size_t size = UPB_MAX(128, _upb_Log2CeilingSize(need + overhead)); upb_Message_InternalData* internal = upb_Arena_Malloc(arena, size); if (!internal) return false; internal->size = size; @@ -64,7 +64,7 @@ static bool realloc_internal(upb_Message* msg, size_t need, upb_Arena* arena) { in->internal = internal; } else if (in->internal->ext_begin - in->internal->unknown_end < need) { /* Internal data is too small, reallocate. */ - size_t new_size = _upb_Log2Ceilingsize(in->internal->size + need); + size_t new_size = _upb_Log2CeilingSize(in->internal->size + need); size_t ext_bytes = in->internal->size - in->internal->ext_begin; size_t new_ext_begin = new_size - ext_bytes; upb_Message_InternalData* internal = @@ -310,7 +310,7 @@ bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, /* Grow s->entries if necessary. */ if (sorted->end > s->cap) { - s->cap = _upb_Log2Ceilingsize(sorted->end); + s->cap = _upb_Log2CeilingSize(sorted->end); s->entries = realloc(s->entries, s->cap * sizeof(*s->entries)); if (!s->entries) return false; } diff --git a/upb/msg_internal.h b/upb/msg_internal.h index c1ee62b1c4..9c1ce5e3b2 100644 --- a/upb/msg_internal.h +++ b/upb/msg_internal.h @@ -92,13 +92,16 @@ typedef enum { kUpb_LabelFlags_IsExtension = 8, } upb_LabelFlags; +// Note: we sort by this number when calculating layout order. typedef enum { kUpb_FieldRep_1Byte = 0, kUpb_FieldRep_4Byte = 1, kUpb_FieldRep_StringView = 2, kUpb_FieldRep_Pointer = 3, kUpb_FieldRep_8Byte = 4, + kUpb_FieldRep_Shift = 5, // Bit offset of the rep in upb_MiniTable_Field.mode + kUpb_FieldRep_Max = kUpb_FieldRep_8Byte, } upb_FieldRep; UPB_INLINE upb_FieldMode upb_FieldMode_Get(const upb_MiniTable_Field* field) { diff --git a/upb/upb.h b/upb/upb.h index 12aecb46cb..3f5d27722e 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -223,8 +223,32 @@ UPB_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) { return ret; } +// Call to shrink the last alloc from this arena. +// REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena. +// We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if +// this was not the last alloc. +UPB_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr, size_t oldsize, + size_t size) { + _upb_ArenaHead* h = (_upb_ArenaHead*)a; + oldsize = UPB_ALIGN_MALLOC(oldsize); + size = UPB_ALIGN_MALLOC(size); + UPB_ASSERT((char*)ptr + oldsize == h->ptr); // Must be the last alloc. + UPB_ASSERT(size <= oldsize); + h->ptr = (char*)ptr + size; +} + UPB_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize, size_t size) { + _upb_ArenaHead* h = (_upb_ArenaHead*)a; + oldsize = UPB_ALIGN_MALLOC(oldsize); + size = UPB_ALIGN_MALLOC(size); + if (size <= oldsize) { + if ((char*)ptr + oldsize == h->ptr) { + upb_Arena_ShrinkLast(a, ptr, oldsize, size); + } + return ptr; + } + void* ret = upb_Arena_Malloc(a, size); if (ret && oldsize > 0) { @@ -332,7 +356,7 @@ UPB_INLINE int _upb_Log2Ceiling(int x) { #endif } -UPB_INLINE int _upb_Log2Ceilingsize(int x) { return 1 << _upb_Log2Ceiling(x); } +UPB_INLINE int _upb_Log2CeilingSize(int x) { return 1 << _upb_Log2Ceiling(x); } #include "upb/port_undef.inc"