|
|
|
/*
|
|
|
|
* 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/reflection/message.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "upb/internal/table.h"
|
|
|
|
#include "upb/map.h"
|
|
|
|
#include "upb/msg.h"
|
|
|
|
#include "upb/reflection/def_builder.h"
|
|
|
|
#include "upb/reflection/def_pool.h"
|
|
|
|
#include "upb/reflection/def_type.h"
|
|
|
|
#include "upb/reflection/field_def.h"
|
|
|
|
#include "upb/reflection/message_def.h"
|
|
|
|
#include "upb/reflection/oneof_def.h"
|
|
|
|
|
|
|
|
// Must be last.
|
|
|
|
#include "upb/port_def.inc"
|
|
|
|
|
|
|
|
static size_t get_field_size(const upb_MiniTable_Field* f) {
|
|
|
|
static unsigned char sizes[] = {
|
|
|
|
0, /* 0 */
|
|
|
|
8, /* kUpb_FieldType_Double */
|
|
|
|
4, /* kUpb_FieldType_Float */
|
|
|
|
8, /* kUpb_FieldType_Int64 */
|
|
|
|
8, /* kUpb_FieldType_UInt64 */
|
|
|
|
4, /* kUpb_FieldType_Int32 */
|
|
|
|
8, /* kUpb_FieldType_Fixed64 */
|
|
|
|
4, /* kUpb_FieldType_Fixed32 */
|
|
|
|
1, /* kUpb_FieldType_Bool */
|
|
|
|
sizeof(upb_StringView), /* kUpb_FieldType_String */
|
|
|
|
sizeof(void*), /* kUpb_FieldType_Group */
|
|
|
|
sizeof(void*), /* kUpb_FieldType_Message */
|
|
|
|
sizeof(upb_StringView), /* kUpb_FieldType_Bytes */
|
|
|
|
4, /* kUpb_FieldType_UInt32 */
|
|
|
|
4, /* kUpb_FieldType_Enum */
|
|
|
|
4, /* kUpb_FieldType_SFixed32 */
|
|
|
|
8, /* kUpb_FieldType_SFixed64 */
|
|
|
|
4, /* kUpb_FieldType_SInt32 */
|
|
|
|
8, /* kUpb_FieldType_SInt64 */
|
|
|
|
};
|
|
|
|
return upb_IsRepeatedOrMap(f) ? sizeof(void*) : sizes[f->descriptortype];
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_Message* upb_Message_New(const upb_MessageDef* m, upb_Arena* a) {
|
|
|
|
return _upb_Message_New(upb_MessageDef_MiniTable(m), a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool in_oneof(const upb_MiniTable_Field* field) {
|
|
|
|
return field->presence < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static upb_MessageValue _upb_Message_Getraw(const upb_Message* msg,
|
|
|
|
const upb_FieldDef* f) {
|
|
|
|
const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
|
|
|
|
const char* mem = UPB_PTR_AT(msg, field->offset, char);
|
|
|
|
upb_MessageValue val = {0};
|
|
|
|
memcpy(&val, mem, get_field_size(field));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f) {
|
|
|
|
assert(upb_FieldDef_HasPresence(f));
|
|
|
|
if (upb_FieldDef_IsExtension(f)) {
|
|
|
|
const upb_MiniTable_Extension* ext = _upb_FieldDef_ExtensionMiniTable(f);
|
|
|
|
return _upb_Message_Getext(msg, ext) != NULL;
|
|
|
|
} else {
|
|
|
|
const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
|
|
|
|
if (in_oneof(field)) {
|
|
|
|
return _upb_getoneofcase_field(msg, field) == field->number;
|
|
|
|
} else if (field->presence > 0) {
|
|
|
|
return _upb_hasbit_field(msg, field);
|
|
|
|
} else {
|
|
|
|
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message ||
|
|
|
|
field->descriptortype == kUpb_FieldType_Group);
|
|
|
|
return _upb_Message_Getraw(msg, f).msg_val != NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
|
|
|
|
const upb_OneofDef* o) {
|
|
|
|
const upb_FieldDef* f = upb_OneofDef_Field(o, 0);
|
|
|
|
if (upb_OneofDef_IsSynthetic(o)) {
|
|
|
|
UPB_ASSERT(upb_OneofDef_FieldCount(o) == 1);
|
|
|
|
return upb_Message_Has(msg, f) ? f : NULL;
|
|
|
|
} else {
|
|
|
|
const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
|
|
|
|
uint32_t oneof_case = _upb_getoneofcase_field(msg, field);
|
|
|
|
f = oneof_case ? upb_OneofDef_LookupNumber(o, oneof_case) : NULL;
|
|
|
|
UPB_ASSERT((f != NULL) == (oneof_case != 0));
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_MessageValue upb_Message_Get(const upb_Message* msg,
|
|
|
|
const upb_FieldDef* f) {
|
|
|
|
if (upb_FieldDef_IsExtension(f)) {
|
|
|
|
const upb_Message_Extension* ext =
|
|
|
|
_upb_Message_Getext(msg, _upb_FieldDef_ExtensionMiniTable(f));
|
|
|
|
if (ext) {
|
|
|
|
upb_MessageValue val;
|
|
|
|
memcpy(&val, &ext->data, sizeof(val));
|
|
|
|
return val;
|
|
|
|
} else if (upb_FieldDef_IsRepeated(f)) {
|
|
|
|
return (upb_MessageValue){.array_val = NULL};
|
|
|
|
}
|
|
|
|
} else if (!upb_FieldDef_HasPresence(f) || upb_Message_Has(msg, f)) {
|
|
|
|
return _upb_Message_Getraw(msg, f);
|
|
|
|
}
|
|
|
|
return upb_FieldDef_Default(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
|
|
|
|
const upb_FieldDef* f,
|
|
|
|
upb_Arena* a) {
|
|
|
|
UPB_ASSERT(upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsRepeated(f));
|
|
|
|
if (upb_FieldDef_HasPresence(f) && !upb_Message_Has(msg, f)) {
|
|
|
|
// We need to skip the upb_Message_Get() call in this case.
|
|
|
|
goto make;
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_MessageValue val = upb_Message_Get(msg, f);
|
|
|
|
if (val.array_val) {
|
|
|
|
return (upb_MutableMessageValue){.array = (upb_Array*)val.array_val};
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_MutableMessageValue ret;
|
|
|
|
make:
|
|
|
|
if (!a) return (upb_MutableMessageValue){.array = NULL};
|
|
|
|
if (upb_FieldDef_IsMap(f)) {
|
|
|
|
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
|
|
|
|
const upb_FieldDef* key =
|
|
|
|
upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_KeyFieldNumber);
|
|
|
|
const upb_FieldDef* value =
|
|
|
|
upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_ValueFieldNumber);
|
|
|
|
ret.map =
|
|
|
|
upb_Map_New(a, upb_FieldDef_CType(key), upb_FieldDef_CType(value));
|
|
|
|
} else if (upb_FieldDef_IsRepeated(f)) {
|
|
|
|
ret.array = upb_Array_New(a, upb_FieldDef_CType(f));
|
|
|
|
} else {
|
|
|
|
UPB_ASSERT(upb_FieldDef_IsSubMessage(f));
|
|
|
|
ret.msg = upb_Message_New(upb_FieldDef_MessageSubDef(f), a);
|
|
|
|
}
|
|
|
|
|
|
|
|
val.array_val = ret.array;
|
|
|
|
upb_Message_Set(msg, f, val, a);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f,
|
|
|
|
upb_MessageValue val, upb_Arena* a) {
|
|
|
|
if (upb_FieldDef_IsExtension(f)) {
|
|
|
|
upb_Message_Extension* ext = _upb_Message_GetOrCreateExtension(
|
|
|
|
msg, _upb_FieldDef_ExtensionMiniTable(f), a);
|
|
|
|
if (!ext) return false;
|
|
|
|
memcpy(&ext->data, &val, sizeof(val));
|
|
|
|
} else {
|
|
|
|
const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
|
Treat unlinked sub-messages in the MiniTable as unknown
This is an observable behavior change in the decoder. After submitting this CL, clients of the decoder can assume that any unlinked sub-messages will be treated as unknown, rather than crashing.
Unlinked sub-messages must never have values present in the message. We can verify this with asserts. Since the values are never set, the encoder should never encounter data for any unlinked sub-message.
```
name old cpu/op new cpu/op delta
BM_ArenaOneAlloc 18.3ns ± 9% 17.9ns ± 2% ~ (p=0.690 n=5+5)
BM_ArenaInitialBlockOneAlloc 6.40ns ± 1% 6.68ns ±10% ~ (p=0.730 n=4+5)
BM_LoadAdsDescriptor_Upb<NoLayout> 5.09ms ± 2% 5.03ms ± 3% ~ (p=0.222 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 5.45ms ± 3% 5.43ms ± 1% ~ (p=0.905 n=5+4)
BM_LoadAdsDescriptor_Proto2<NoLayout> 10.9ms ± 1% 10.8ms ± 1% -1.09% (p=0.016 n=5+4)
BM_LoadAdsDescriptor_Proto2<WithLayout> 11.3ms ± 9% 11.1ms ± 3% ~ (p=0.841 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 11.2µs ± 3% 11.3µs ± 3% ~ (p=0.222 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 10.3µs ± 5% 10.5µs ± 5% ~ (p=0.310 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 11.4µs ±18% 11.0µs ± 2% ~ (p=1.000 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 10.5µs ±17% 10.6µs ±19% ~ (p=0.421 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 20.5µs ± 2% 20.2µs ± 2% ~ (p=0.222 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 10.8µs ± 2% 10.9µs ± 4% ~ (p=0.841 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 10.5µs ± 3% 10.6µs ± 3% ~ (p=0.690 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 9.22µs ± 2% 9.23µs ± 3% ~ (p=1.000 n=5+5)
BM_SerializeDescriptor_Proto2 6.05µs ± 3% 5.90µs ± 3% ~ (p=0.222 n=5+5)
BM_SerializeDescriptor_Upb 10.2µs ± 3% 10.6µs ±14% ~ (p=0.841 n=5+5)
name old time/op new time/op delta
BM_ArenaOneAlloc 18.3ns ± 9% 17.9ns ± 2% ~ (p=0.841 n=5+5)
BM_ArenaInitialBlockOneAlloc 6.42ns ± 1% 6.69ns ±10% ~ (p=0.730 n=4+5)
BM_LoadAdsDescriptor_Upb<NoLayout> 5.10ms ± 2% 5.05ms ± 3% ~ (p=0.222 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 5.47ms ± 3% 5.45ms ± 1% ~ (p=0.905 n=5+4)
BM_LoadAdsDescriptor_Proto2<NoLayout> 10.9ms ± 1% 10.8ms ± 1% -1.11% (p=0.016 n=5+4)
BM_LoadAdsDescriptor_Proto2<WithLayout> 11.4ms ± 9% 11.1ms ± 3% ~ (p=0.841 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 11.2µs ± 3% 11.3µs ± 3% ~ (p=0.222 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 10.3µs ± 5% 10.5µs ± 5% ~ (p=0.151 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 11.5µs ±18% 11.0µs ± 2% ~ (p=1.000 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 10.5µs ±17% 10.7µs ±19% ~ (p=0.421 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 20.6µs ± 2% 20.3µs ± 2% ~ (p=0.222 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 10.9µs ± 2% 10.9µs ± 4% ~ (p=0.841 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 10.6µs ± 3% 10.6µs ± 3% ~ (p=0.690 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 9.24µs ± 2% 9.25µs ± 3% ~ (p=1.000 n=5+5)
BM_SerializeDescriptor_Proto2 6.07µs ± 3% 5.91µs ± 3% ~ (p=0.222 n=5+5)
BM_SerializeDescriptor_Upb 10.3µs ± 3% 10.6µs ±14% ~ (p=0.841 n=5+5)
name old INSTRUCTIONS/op new INSTRUCTIONS/op delta
BM_ArenaOneAlloc 201 ± 0% 201 ± 0% ~ (p=0.841 n=5+5)
BM_ArenaInitialBlockOneAlloc 69.0 ± 0% 69.0 ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Upb<NoLayout> 33.9M ± 0% 34.1M ± 0% +0.66% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 35.6M ± 0% 35.8M ± 0% +0.64% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 70.8M ± 0% 70.8M ± 0% ~ (p=0.548 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 71.6M ± 0% 71.6M ± 0% ~ (p=0.151 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 137k ± 0% 141k ± 0% +2.87% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 125k ± 0% 128k ± 0% +2.83% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 135k ± 0% 139k ± 0% +2.89% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 124k ± 0% 127k ± 0% +2.85% (p=0.016 n=5+4)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 201k ± 0% 201k ± 0% ~ (p=0.222 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 107k ± 0% 107k ± 0% ~ (p=1.000 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 105k ± 0% 105k ± 0% ~ (p=0.286 n=5+4)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 86.5k ± 0% 86.5k ± 0% ~ (p=0.222 n=5+5)
BM_SerializeDescriptor_Proto2 60.3k ± 0% 60.3k ± 0% ~ (p=0.071 n=5+5)
BM_SerializeDescriptor_Upb 111k ± 0% 111k ± 0% ~ (p=0.841 n=5+5)
name old CYCLES/op new CYCLES/op delta
BM_ArenaOneAlloc 60.0 ± 7% 58.8 ± 0% -2.15% (p=0.016 n=5+5)
BM_ArenaInitialBlockOneAlloc 21.0 ± 0% 21.0 ± 0% ~ (p=1.000 n=5+5)
BM_LoadAdsDescriptor_Upb<NoLayout> 16.9M ± 0% 16.9M ± 0% ~ (p=0.056 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 17.9M ± 1% 18.0M ± 1% ~ (p=0.095 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 35.9M ± 1% 35.8M ± 1% ~ (p=0.421 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 36.5M ± 0% 36.5M ± 0% ~ (p=0.841 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 37.2k ± 0% 37.3k ± 0% ~ (p=0.222 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 34.1k ± 0% 34.7k ± 0% +1.66% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 36.4k ± 0% 36.7k ± 0% +0.83% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 33.3k ± 1% 34.1k ± 1% +2.39% (p=0.008 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 68.1k ± 1% 68.0k ± 1% ~ (p=0.421 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 36.0k ± 1% 36.1k ± 1% ~ (p=0.841 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 35.3k ± 1% 35.5k ± 1% ~ (p=0.151 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 30.7k ± 0% 30.9k ± 1% ~ (p=0.151 n=5+5)
BM_SerializeDescriptor_Proto2 20.3k ± 2% 19.7k ± 3% ~ (p=0.151 n=5+5)
BM_SerializeDescriptor_Upb 33.6k ± 0% 33.7k ± 2% ~ (p=1.000 n=5+5)
name old allocs/op new allocs/op delta
BM_ArenaOneAlloc 1.19 ± 0% 1.19 ± 0% ~ (all samples are equal)
BM_ArenaInitialBlockOneAlloc 0.19 ± 0% 0.19 ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Upb<NoLayout> 6.00k ± 0% 6.00k ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Upb<WithLayout> 5.99k ± 0% 5.99k ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Proto2<NoLayout> 77.8k ± 0% 77.8k ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Proto2<WithLayout> 79.0k ± 0% 79.0k ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<UseArena, Copy> 7.19 ± 0% 7.19 ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<UseArena, Alias> 7.19 ± 0% 7.19 ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 0.19 ± 0% 0.19 ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 0.19 ± 0% 0.19 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 765 ± 0% 765 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 10.2 ± 0% 10.2 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 1.19 ± 0% 1.19 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 1.19 ± 0% 1.19 ± 0% ~ (all samples are equal)
BM_SerializeDescriptor_Proto2 0.19 ± 0% 0.19 ± 0% ~ (all samples are equal)
BM_SerializeDescriptor_Upb 0.19 ± 0% 0.19 ± 0% ~ (all samples are equal)
name old peak-mem(Bytes)/op new peak-mem(Bytes)/op delta
BM_ArenaOneAlloc 344 ± 0% 344 ± 0% ~ (all samples are equal)
BM_ArenaInitialBlockOneAlloc 112 ± 0% 112 ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Upb<NoLayout> 9.64M ± 0% 9.64M ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Upb<WithLayout> 9.70M ± 0% 9.70M ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Proto2<NoLayout> 6.38M ± 0% 6.38M ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Proto2<WithLayout> 6.44M ± 0% 6.44M ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<UseArena, Copy> 36.5k ± 0% 36.5k ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<UseArena, Alias> 36.5k ± 0% 36.5k ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 112 ± 0% 112 ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 112 ± 0% 112 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 35.8k ± 0% 35.8k ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 40.8k ± 0% 40.8k ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 112 ± 0% 112 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 112 ± 0% 112 ± 0% ~ (all samples are equal)
BM_SerializeDescriptor_Proto2 112 ± 0% 112 ± 0% ~ (all samples are equal)
BM_SerializeDescriptor_Upb 112 ± 0% 112 ± 0% ~ (all samples are equal)
name old speed new speed delta
BM_LoadAdsDescriptor_Upb<NoLayout> 147MB/s ± 2% 148MB/s ± 3% ~ (p=0.222 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 137MB/s ± 3% 137MB/s ± 1% ~ (p=0.905 n=5+4)
BM_LoadAdsDescriptor_Proto2<NoLayout> 68.6MB/s ± 1% 69.3MB/s ± 1% +1.10% (p=0.016 n=5+4)
BM_LoadAdsDescriptor_Proto2<WithLayout> 66.0MB/s ± 9% 67.4MB/s ± 3% ~ (p=0.841 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 675MB/s ± 3% 667MB/s ± 3% ~ (p=0.222 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 730MB/s ± 5% 718MB/s ± 5% ~ (p=0.310 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 663MB/s ±16% 685MB/s ± 2% ~ (p=1.000 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 723MB/s ±15% 712MB/s ±16% ~ (p=0.421 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 367MB/s ± 2% 372MB/s ± 2% ~ (p=0.222 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 694MB/s ± 2% 691MB/s ± 4% ~ (p=0.841 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 714MB/s ± 3% 709MB/s ± 3% ~ (p=0.690 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 816MB/s ± 2% 816MB/s ± 3% ~ (p=1.000 n=5+5)
BM_SerializeDescriptor_Proto2 1.24GB/s ± 3% 1.28GB/s ± 3% ~ (p=0.222 n=5+5)
BM_SerializeDescriptor_Upb 734MB/s ± 3% 713MB/s ±13% ~ (p=0.841 n=5+5)
```
PiperOrigin-RevId: 477770562
2 years ago
|
|
|
|
|
|
|
// Building reflection should always cause all sub-message types to be
|
|
|
|
// linked, but double-check here just for extra assurance.
|
|
|
|
UPB_ASSERT(!upb_FieldDef_IsSubMessage(f) ||
|
|
|
|
upb_MessageDef_MiniTable(upb_FieldDef_ContainingType(f))
|
|
|
|
->subs[field->submsg_index]
|
|
|
|
.submsg);
|
|
|
|
|
|
|
|
char* mem = UPB_PTR_AT(msg, field->offset, char);
|
|
|
|
memcpy(mem, &val, get_field_size(field));
|
|
|
|
if (field->presence > 0) {
|
|
|
|
_upb_sethas_field(msg, field);
|
|
|
|
} else if (in_oneof(field)) {
|
|
|
|
*_upb_oneofcase_field(msg, field) = field->number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void upb_Message_ClearField(upb_Message* msg, const upb_FieldDef* f) {
|
|
|
|
if (upb_FieldDef_IsExtension(f)) {
|
|
|
|
_upb_Message_Clearext(msg, _upb_FieldDef_ExtensionMiniTable(f));
|
|
|
|
} else {
|
|
|
|
const upb_MiniTable_Field* field = upb_FieldDef_MiniTable(f);
|
|
|
|
char* mem = UPB_PTR_AT(msg, field->offset, char);
|
|
|
|
|
|
|
|
if (field->presence > 0) {
|
|
|
|
_upb_clearhas_field(msg, field);
|
|
|
|
} else if (in_oneof(field)) {
|
|
|
|
uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
|
|
|
|
if (*oneof_case != field->number) return;
|
|
|
|
*oneof_case = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(mem, 0, get_field_size(field));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m) {
|
|
|
|
_upb_Message_Clear(msg, upb_MessageDef_MiniTable(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
|
|
|
|
const upb_DefPool* ext_pool, const upb_FieldDef** out_f,
|
|
|
|
upb_MessageValue* out_val, size_t* iter) {
|
|
|
|
size_t i = *iter;
|
|
|
|
size_t n = upb_MessageDef_FieldCount(m);
|
|
|
|
const upb_MessageValue zero = {0};
|
|
|
|
UPB_UNUSED(ext_pool);
|
|
|
|
|
|
|
|
/* Iterate over normal fields, returning the first one that is set. */
|
|
|
|
while (++i < n) {
|
|
|
|
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
|
|
|
|
upb_MessageValue val = _upb_Message_Getraw(msg, f);
|
|
|
|
|
|
|
|
/* Skip field if unset or empty. */
|
|
|
|
if (upb_FieldDef_HasPresence(f)) {
|
|
|
|
if (!upb_Message_Has(msg, f)) continue;
|
|
|
|
} else {
|
|
|
|
upb_MessageValue test = val;
|
|
|
|
if (upb_FieldDef_IsString(f) && !upb_FieldDef_IsRepeated(f)) {
|
|
|
|
/* Clear string pointer, only size matters (ptr could be non-NULL). */
|
|
|
|
test.str_val.data = NULL;
|
|
|
|
}
|
|
|
|
/* Continue if NULL or 0. */
|
|
|
|
if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
|
|
|
|
|
|
|
|
/* Continue on empty array or map. */
|
|
|
|
if (upb_FieldDef_IsMap(f)) {
|
|
|
|
if (upb_Map_Size(test.map_val) == 0) continue;
|
|
|
|
} else if (upb_FieldDef_IsRepeated(f)) {
|
|
|
|
if (upb_Array_Size(test.array_val) == 0) continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_val = val;
|
|
|
|
*out_f = f;
|
|
|
|
*iter = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext_pool) {
|
|
|
|
/* Return any extensions that are set. */
|
|
|
|
size_t count;
|
|
|
|
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &count);
|
|
|
|
if (i - n < count) {
|
|
|
|
ext += count - 1 - (i - n);
|
|
|
|
memcpy(out_val, &ext->data, sizeof(*out_val));
|
|
|
|
*out_f = _upb_DefPool_FindExtensionByMiniTable(ext_pool, ext->ext);
|
|
|
|
*iter = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*iter = i;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
|
|
|
|
int depth) {
|
|
|
|
size_t iter = kUpb_Message_Begin;
|
|
|
|
const upb_FieldDef* f;
|
|
|
|
upb_MessageValue val;
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (--depth == 0) return false;
|
|
|
|
|
|
|
|
_upb_Message_DiscardUnknown_shallow(msg);
|
|
|
|
|
|
|
|
while (upb_Message_Next(msg, m, NULL /*ext_pool*/, &f, &val, &iter)) {
|
|
|
|
const upb_MessageDef* subm = upb_FieldDef_MessageSubDef(f);
|
|
|
|
if (!subm) continue;
|
|
|
|
if (upb_FieldDef_IsMap(f)) {
|
|
|
|
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(subm, 2);
|
|
|
|
const upb_MessageDef* val_m = upb_FieldDef_MessageSubDef(val_f);
|
|
|
|
upb_Map* map = (upb_Map*)val.map_val;
|
|
|
|
size_t iter = kUpb_Map_Begin;
|
|
|
|
|
|
|
|
if (!val_m) continue;
|
|
|
|
|
|
|
|
while (upb_MapIterator_Next(map, &iter)) {
|
|
|
|
upb_MessageValue map_val = upb_MapIterator_Value(map, iter);
|
|
|
|
if (!_upb_Message_DiscardUnknown((upb_Message*)map_val.msg_val, val_m,
|
|
|
|
depth)) {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (upb_FieldDef_IsRepeated(f)) {
|
|
|
|
const upb_Array* arr = val.array_val;
|
|
|
|
size_t i, n = upb_Array_Size(arr);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
upb_MessageValue elem = upb_Array_Get(arr, i);
|
|
|
|
if (!_upb_Message_DiscardUnknown((upb_Message*)elem.msg_val, subm,
|
|
|
|
depth)) {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!_upb_Message_DiscardUnknown((upb_Message*)val.msg_val, subm,
|
|
|
|
depth)) {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
|
|
|
|
int maxdepth) {
|
|
|
|
return _upb_Message_DiscardUnknown(msg, m, maxdepth);
|
|
|
|
}
|