|
|
|
// Protocol Buffers - Google's data interchange format
|
|
|
|
// Copyright 2023 Google LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
// This should #undef all macros #defined in def.inc
|
|
|
|
|
|
|
|
#undef UPB_SIZE
|
|
|
|
#undef UPB_PTR_AT
|
|
|
|
#undef UPB_MAPTYPE_STRING
|
|
|
|
#undef UPB_EXPORT
|
|
|
|
#undef UPB_INLINE
|
|
|
|
#undef UPB_API
|
|
|
|
#undef UPB_API_INLINE
|
|
|
|
#undef UPB_ALIGN_UP
|
|
|
|
#undef UPB_ALIGN_DOWN
|
|
|
|
#undef UPB_ALIGN_MALLOC
|
|
|
|
#undef UPB_ALIGN_OF
|
|
|
|
#undef UPB_ALIGN_AS
|
|
|
|
#undef UPB_MALLOC_ALIGN
|
|
|
|
#undef UPB_LIKELY
|
|
|
|
#undef UPB_UNLIKELY
|
|
|
|
#undef UPB_FORCEINLINE
|
|
|
|
#undef UPB_NOINLINE
|
|
|
|
#undef UPB_NORETURN
|
|
|
|
#undef UPB_PRINTF
|
|
|
|
#undef UPB_MAX
|
|
|
|
#undef UPB_MIN
|
|
|
|
#undef UPB_UNUSED
|
|
|
|
#undef UPB_ASSUME
|
|
|
|
#undef UPB_ASSERT
|
|
|
|
#undef UPB_UNREACHABLE
|
|
|
|
#undef UPB_SETJMP
|
|
|
|
#undef UPB_LONGJMP
|
|
|
|
#undef UPB_PTRADD
|
|
|
|
#undef UPB_MUSTTAIL
|
|
|
|
#undef UPB_FASTTABLE_SUPPORTED
|
upb is self-hosting!
This CL changes the upb compiler to no longer depend on C++ protobuf libraries. upb now uses its own reflection libraries to implement its code generator.
# Key Benefits
1. upb can now use its own reflection libraries throughout the compiler. This makes upb more consistent and principled, and gives us more chances to dogfood our own C++ reflection API. This highlighted several parts of the C++ reflection API that were incomplete.
2. This CL removes code duplication that previously existed in the compiler. The upb reflection library has code to build MiniDescriptors and MiniTables out of descriptors, but prior to this CL the upb compiler could not use it. The upb compiler had a separate copy of this logic, and the compiler's copy of this logic was especially tricky and hard to maintain. This CL removes the separate copy of that logic.
3. This CL (mostly) removes upb's dependency on the C++ protobuf library. We still depend on `protoc` (the binary), but the runtime and compiler no longer link against C++'s libraries. This opens up the possibility of speeding up some builds significantly if we can use a prebuilt `protoc` binary.
# Bootstrap Stages
To bootstrap, we check in a copy of our generated code for `descriptor.proto` and `plugin.proto`. This allows the compiler to depend on the generated code for these two protos without creating a circular dependency. This code is checked in to the `stage0` directory.
The bootstrapping process is divided into a few stages. All `cc_library()`, `upb_proto_library()`, and `cc_binary()` targets that would otherwise be circular participate in this staging process. That currently includes:
* `//third_party/upb:descriptor_upb_proto`
* `//third_party/upb:plugin_upb_proto`
* `//third_party/upb:reflection`
* `//third_party/upb:reflection_internal`
* `//third_party/upbc:common`
* `//third_party/upbc:file_layout`
* `//third_party/upbc:plugin`
* `//third_party/upbc:protoc-gen-upb`
For each of these targets, we produce a rule for each stage (the logic for this is nicely encapsulated in Blaze/Bazel macros like `bootstrap_cc_library()` and `bootstrap_upb_proto_library()`, so the `BUILD` file remains readable). For example:
* `//third_party/upb:descriptor_upb_proto_stage0`
* `//third_party/upb:descriptor_upb_proto_stage1`
* `//third_party/upb:descriptor_upb_proto`
The stages are:
1. `stage0`: This uses the checked-in version of the generated code. The stage0 compiler is correct and outputs the same code as all other compilers, but it is unnecessarily slow because its protos were compiled in bootstrap mode. The stage0 compiler is used to generate protos for stage1.
2. `stage1`: The stage1 compiler is correct and fast, and therefore we use it in almost all cases (eg. `upb_proto_library()`). However its own protos were not generated using `upb_proto_library()`, so its `cc_library()` targets cannot be safely mixed with `upb_proto_library()`, as this would lead to duplicate symbols.
3. final (no stage): The final compiler is identical to the `stage1` compiler. The only difference is that its protos were built with `upb_proto_library()`. This doesn't matter very much for the compiler binary, but for the `cc_library()` targets like `//third_party/upb:reflection`, only the final targets can be safely linked in by other applications.
# "Bootstrap Mode" Protos
The checked-in generated code is generated in a special "bootstrap" mode that is a bit different than normal generated code. Bootstrap mode avoids depending on the internal representation of MiniTables or the messages, at the cost of slower runtime performance.
Bootstrap mode only interacts with MiniTables and messages using public APIs such as `upb_MiniTable_Build()`, `upb_Message_GetInt32()`, etc. This is very important as it allows us to change the internal representation without needing to regenerate our bootstrap protos. This will make it far easier to write CLs that change the internal representation, because it avoids the awkward dance of trying to regenerate the bootstrap protos when the compiler itself is broken due to bootstrap protos being out of date.
The bootstrap generated code does have two downsides:
1. The accessors are less efficient, because they look up MiniTable fields by number instead of hard-coding the MiniTableField into the generated code.
2. It requires runtime initialization of the MiniTables, which costs CPU cycles at startup, and also allocates memory which is never freed. Per google3 rules this is not really a leak, since this memory is still reachable via static variables, but it is undesirable in many contexts. We could fix this part by introducing the equivalent of `google::protobuf::ShutdownProtobufLibrary()`).
These downsides are fine for the bootstrapping process, but they are reason enough not to enable bootstrap mode in general for all protos.
# Bootstrapping Always Uses OSS Protos
To enable smooth syncing between Google3 and OSS, we always use an OSS version of the checked in generated code for `stage0`, even in google3.
This requires that the google3 code can be switched to reference the OSS proto names using a preprocessor define. We introduce the `UPB_DESC(xyz)` macro for this, which will expand into either `proto2_xyz` or `google_protobuf_xyz`. Any libraries used in `stage0` must use `UPB_DESC(xyz)` rather than refer to the symbol names directly.
PiperOrigin-RevId: 501458451
2 years ago
|
|
|
#undef UPB_FASTTABLE_MASK
|
|
|
|
#undef UPB_FASTTABLE
|
|
|
|
#undef UPB_FASTTABLE_INIT
|
|
|
|
#undef UPB_POISON_MEMORY_REGION
|
|
|
|
#undef UPB_UNPOISON_MEMORY_REGION
|
|
|
|
#undef UPB_ASAN
|
|
|
|
#undef UPB_ASAN_GUARD_SIZE
|
|
|
|
#undef UPB_CLANG_ASAN
|
|
|
|
#undef UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN
|
|
|
|
#undef UPB_DEPRECATED
|
|
|
|
#undef UPB_GNUC_MIN
|
upb is self-hosting!
This CL changes the upb compiler to no longer depend on C++ protobuf libraries. upb now uses its own reflection libraries to implement its code generator.
# Key Benefits
1. upb can now use its own reflection libraries throughout the compiler. This makes upb more consistent and principled, and gives us more chances to dogfood our own C++ reflection API. This highlighted several parts of the C++ reflection API that were incomplete.
2. This CL removes code duplication that previously existed in the compiler. The upb reflection library has code to build MiniDescriptors and MiniTables out of descriptors, but prior to this CL the upb compiler could not use it. The upb compiler had a separate copy of this logic, and the compiler's copy of this logic was especially tricky and hard to maintain. This CL removes the separate copy of that logic.
3. This CL (mostly) removes upb's dependency on the C++ protobuf library. We still depend on `protoc` (the binary), but the runtime and compiler no longer link against C++'s libraries. This opens up the possibility of speeding up some builds significantly if we can use a prebuilt `protoc` binary.
# Bootstrap Stages
To bootstrap, we check in a copy of our generated code for `descriptor.proto` and `plugin.proto`. This allows the compiler to depend on the generated code for these two protos without creating a circular dependency. This code is checked in to the `stage0` directory.
The bootstrapping process is divided into a few stages. All `cc_library()`, `upb_proto_library()`, and `cc_binary()` targets that would otherwise be circular participate in this staging process. That currently includes:
* `//third_party/upb:descriptor_upb_proto`
* `//third_party/upb:plugin_upb_proto`
* `//third_party/upb:reflection`
* `//third_party/upb:reflection_internal`
* `//third_party/upbc:common`
* `//third_party/upbc:file_layout`
* `//third_party/upbc:plugin`
* `//third_party/upbc:protoc-gen-upb`
For each of these targets, we produce a rule for each stage (the logic for this is nicely encapsulated in Blaze/Bazel macros like `bootstrap_cc_library()` and `bootstrap_upb_proto_library()`, so the `BUILD` file remains readable). For example:
* `//third_party/upb:descriptor_upb_proto_stage0`
* `//third_party/upb:descriptor_upb_proto_stage1`
* `//third_party/upb:descriptor_upb_proto`
The stages are:
1. `stage0`: This uses the checked-in version of the generated code. The stage0 compiler is correct and outputs the same code as all other compilers, but it is unnecessarily slow because its protos were compiled in bootstrap mode. The stage0 compiler is used to generate protos for stage1.
2. `stage1`: The stage1 compiler is correct and fast, and therefore we use it in almost all cases (eg. `upb_proto_library()`). However its own protos were not generated using `upb_proto_library()`, so its `cc_library()` targets cannot be safely mixed with `upb_proto_library()`, as this would lead to duplicate symbols.
3. final (no stage): The final compiler is identical to the `stage1` compiler. The only difference is that its protos were built with `upb_proto_library()`. This doesn't matter very much for the compiler binary, but for the `cc_library()` targets like `//third_party/upb:reflection`, only the final targets can be safely linked in by other applications.
# "Bootstrap Mode" Protos
The checked-in generated code is generated in a special "bootstrap" mode that is a bit different than normal generated code. Bootstrap mode avoids depending on the internal representation of MiniTables or the messages, at the cost of slower runtime performance.
Bootstrap mode only interacts with MiniTables and messages using public APIs such as `upb_MiniTable_Build()`, `upb_Message_GetInt32()`, etc. This is very important as it allows us to change the internal representation without needing to regenerate our bootstrap protos. This will make it far easier to write CLs that change the internal representation, because it avoids the awkward dance of trying to regenerate the bootstrap protos when the compiler itself is broken due to bootstrap protos being out of date.
The bootstrap generated code does have two downsides:
1. The accessors are less efficient, because they look up MiniTable fields by number instead of hard-coding the MiniTableField into the generated code.
2. It requires runtime initialization of the MiniTables, which costs CPU cycles at startup, and also allocates memory which is never freed. Per google3 rules this is not really a leak, since this memory is still reachable via static variables, but it is undesirable in many contexts. We could fix this part by introducing the equivalent of `google::protobuf::ShutdownProtobufLibrary()`).
These downsides are fine for the bootstrapping process, but they are reason enough not to enable bootstrap mode in general for all protos.
# Bootstrapping Always Uses OSS Protos
To enable smooth syncing between Google3 and OSS, we always use an OSS version of the checked in generated code for `stage0`, even in google3.
This requires that the google3 code can be switched to reference the OSS proto names using a preprocessor define. We introduce the `UPB_DESC(xyz)` macro for this, which will expand into either `proto2_xyz` or `google_protobuf_xyz`. Any libraries used in `stage0` must use `UPB_DESC(xyz)` rather than refer to the symbol names directly.
PiperOrigin-RevId: 501458451
2 years ago
|
|
|
#undef UPB_DESCRIPTOR_UPB_H_FILENAME
|
|
|
|
#undef UPB_DESC
|
Implement feature inheritance and legacy editions for upb.
This switches upb to using legacy editions for all proto2/proto3 logic. This does not yet enable code generation for editions protos (ie. we do not yet turn on `FEATURE_SUPPORTS_EDITIONS`), but with feature inheritance in place, this will be a much smaller follow-on change.
There is a ~10% increase in allocations, but only a ~1% increase in peak memory. There are some <5% increases in instructions and cycles, but apparently no increase in time:
```
name old cpu/op new cpu/op delta
BM_ArenaOneAlloc 17.8ns ±11% 16.9ns ±17% ~ (p=0.310 n=5+5)
BM_ArenaInitialBlockOneAlloc 5.99ns ±13% 5.35ns ± 2% ~ (p=0.421 n=5+5)
BM_ArenaFuseUnbalanced/2 71.4ns ±11% 63.1ns ± 3% ~ (p=0.095 n=5+5)
BM_ArenaFuseUnbalanced/8 509ns ± 2% 532ns ±15% ~ (p=0.421 n=5+5)
BM_ArenaFuseUnbalanced/64 4.73µs ±20% 4.43µs ±10% ~ (p=0.841 n=5+5)
BM_ArenaFuseUnbalanced/128 9.77µs ±12% 8.64µs ± 4% ~ (p=0.095 n=5+5)
BM_ArenaFuseBalanced/2 67.5ns ±13% 62.6ns ± 3% ~ (p=0.841 n=5+5)
BM_ArenaFuseBalanced/8 552ns ±23% 496ns ±25% ~ (p=0.222 n=5+5)
BM_ArenaFuseBalanced/64 4.76µs ±14% 4.24µs ± 4% ~ (p=0.421 n=5+5)
BM_ArenaFuseBalanced/128 10.2µs ±14% 8.6µs ± 4% -15.61% (p=0.016 n=5+5)
BM_LoadAdsDescriptor_Upb<NoLayout> 6.20ms ±12% 6.18ms ±16% ~ (p=0.421 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 6.91ms ±12% 6.63ms ± 3% ~ (p=0.690 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 15.0ms ±12% 13.7ms ± 3% ~ (p=0.421 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 15.1ms ±13% 13.8ms ± 3% ~ (p=0.548 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 14.4µs ±13% 13.2µs ± 3% ~ (p=0.548 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 12.8µs ±12% 11.8µs ± 3% ~ (p=0.222 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 13.7µs ±12% 12.9µs ± 3% ~ (p=1.000 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 13.1µs ±11% 11.6µs ± 3% ~ (p=0.056 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 24.7µs ±12% 22.6µs ± 8% ~ (p=0.310 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 11.6µs ±13% 10.9µs ± 2% ~ (p=1.000 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 11.7µs ±10% 10.6µs ± 3% ~ (p=0.310 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 13.4µs ±12% 12.3µs ± 4% ~ (p=0.310 n=5+5)
BM_SerializeDescriptor_Proto2 6.62µs ±13% 6.00µs ± 6% ~ (p=0.056 n=5+5)
BM_SerializeDescriptor_Upb 11.1µs ±13% 10.3µs ± 3% ~ (p=1.000 n=5+5)
name old time/op new time/op delta
BM_ArenaOneAlloc 17.9ns ±12% 17.0ns ±17% ~ (p=0.310 n=5+5)
BM_ArenaInitialBlockOneAlloc 6.03ns ±14% 5.36ns ± 2% ~ (p=0.421 n=5+5)
BM_ArenaFuseUnbalanced/2 71.9ns ±12% 63.3ns ± 3% ~ (p=0.095 n=5+5)
BM_ArenaFuseUnbalanced/8 511ns ± 2% 533ns ±15% ~ (p=0.421 n=5+5)
BM_ArenaFuseUnbalanced/64 4.75µs ±20% 4.44µs ±10% ~ (p=0.841 n=5+5)
BM_ArenaFuseUnbalanced/128 9.83µs ±12% 8.66µs ± 4% ~ (p=0.151 n=5+5)
BM_ArenaFuseBalanced/2 67.8ns ±13% 62.7ns ± 3% ~ (p=0.841 n=5+5)
BM_ArenaFuseBalanced/8 555ns ±24% 497ns ±26% ~ (p=0.222 n=5+5)
BM_ArenaFuseBalanced/64 4.79µs ±14% 4.25µs ± 4% ~ (p=0.310 n=5+5)
BM_ArenaFuseBalanced/128 10.3µs ±14% 8.6µs ± 4% -15.93% (p=0.016 n=5+5)
BM_LoadAdsDescriptor_Upb<NoLayout> 6.25ms ±12% 6.20ms ±16% ~ (p=0.421 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 6.96ms ±13% 6.65ms ± 3% ~ (p=0.690 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 15.2ms ±12% 13.7ms ± 3% ~ (p=0.421 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 15.3ms ±14% 13.8ms ± 3% ~ (p=0.548 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 14.5µs ±14% 13.2µs ± 3% ~ (p=0.690 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 12.8µs ±12% 11.8µs ± 3% ~ (p=0.222 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 13.8µs ±13% 13.0µs ± 3% ~ (p=1.000 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 13.2µs ±12% 11.6µs ± 3% ~ (p=0.056 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 24.9µs ±12% 22.6µs ± 8% ~ (p=0.310 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 11.7µs ±14% 10.9µs ± 2% ~ (p=1.000 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 11.7µs ±11% 10.7µs ± 3% ~ (p=0.222 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 13.5µs ±12% 12.3µs ± 4% ~ (p=0.310 n=5+5)
BM_SerializeDescriptor_Proto2 6.65µs ±13% 6.01µs ± 6% ~ (p=0.056 n=5+5)
BM_SerializeDescriptor_Upb 11.2µs ±13% 10.3µs ± 3% ~ (p=1.000 n=5+5)
name old INSTRUCTIONS/op new INSTRUCTIONS/op delta
BM_ArenaOneAlloc 189 ± 0% 189 ± 0% ~ (p=0.881 n=5+5)
BM_ArenaInitialBlockOneAlloc 69.0 ± 0% 69.0 ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/2 458 ± 0% 458 ± 0% ~ (p=1.000 n=5+5)
BM_ArenaFuseUnbalanced/8 3.28k ±15% 3.60k ± 0% ~ (p=0.286 n=5+4)
BM_ArenaFuseUnbalanced/64 28.6k ± 2% 29.2k ± 0% +2.17% (p=0.032 n=5+4)
BM_ArenaFuseUnbalanced/128 57.9k ± 1% 57.9k ± 1% ~ (p=1.000 n=5+5)
BM_ArenaFuseBalanced/2 482 ± 0% 482 ± 0% ~ (p=0.421 n=5+5)
BM_ArenaFuseBalanced/8 3.35k ±14% 3.35k ±14% ~ (p=0.841 n=5+5)
BM_ArenaFuseBalanced/64 29.2k ± 2% 29.3k ± 1% ~ (p=0.421 n=5+5)
BM_ArenaFuseBalanced/128 59.2k ± 1% 59.3k ± 1% ~ (p=0.556 n=4+5)
BM_LoadAdsDescriptor_Upb<NoLayout> 37.3M ± 0% 38.2M ± 0% +2.39% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 40.9M ± 0% 41.7M ± 0% +2.02% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 87.2M ± 0% 88.3M ± 1% +1.25% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 88.0M ± 0% 88.9M ± 1% +1.13% (p=0.016 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 154k ± 0% 154k ± 0% ~ (p=1.000 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 143k ± 0% 143k ± 0% ~ (p=0.310 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 153k ± 0% 153k ± 0% ~ (p=1.016 n=5+4)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 142k ± 0% 142k ± 0% ~ (p=0.127 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 213k ± 1% 217k ± 5% ~ (p=1.000 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 122k ± 0% 123k ± 0% +0.86% (p=0.008 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 120k ± 0% 120k ± 0% ~ (p=0.421 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 124k ± 0% 124k ± 0% ~ (p=0.587 n=5+5)
BM_SerializeDescriptor_Proto2 63.5k ± 0% 63.5k ± 0% ~ (p=0.278 n=5+5)
BM_SerializeDescriptor_Upb 111k ± 0% 111k ± 0% ~ (p=1.000 n=5+5)
name old CYCLES/op new CYCLES/op delta
BM_ArenaOneAlloc 53.5 ± 0% 53.4 ± 0% ~ (p=0.095 n=5+5)
BM_ArenaInitialBlockOneAlloc 17.5 ± 1% 17.4 ± 0% ~ (p=0.087 n=5+5)
BM_ArenaFuseUnbalanced/2 206 ± 0% 206 ± 0% ~ (p=0.548 n=5+5)
BM_ArenaFuseUnbalanced/8 1.55k ±12% 1.67k ± 1% ~ (p=0.548 n=5+5)
BM_ArenaFuseUnbalanced/64 14.1k ± 8% 14.1k ± 1% ~ (p=0.222 n=5+5)
BM_ArenaFuseUnbalanced/128 28.2k ± 1% 28.3k ± 1% ~ (p=0.548 n=5+5)
BM_ArenaFuseBalanced/2 205 ± 0% 204 ± 0% ~ (p=0.548 n=5+5)
BM_ArenaFuseBalanced/8 1.57k ±12% 1.56k ±12% ~ (p=0.421 n=5+5)
BM_ArenaFuseBalanced/64 13.9k ± 2% 13.9k ± 1% ~ (p=1.000 n=5+5)
BM_ArenaFuseBalanced/128 28.1k ± 1% 28.2k ± 1% ~ (p=0.730 n=4+5)
BM_LoadAdsDescriptor_Upb<NoLayout> 18.7M ± 0% 19.3M ± 1% +3.38% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 20.9M ± 0% 21.6M ± 0% +3.09% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 43.4M ± 0% 44.4M ± 1% +2.33% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 44.0M ± 0% 44.9M ± 2% +1.92% (p=0.016 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 42.0k ± 1% 43.0k ± 1% +2.32% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 38.2k ± 1% 38.4k ± 0% +0.74% (p=0.032 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 41.6k ± 0% 42.6k ± 1% +2.51% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 37.6k ± 0% 38.1k ± 0% +1.34% (p=0.008 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 71.9k ± 1% 74.1k ± 6% ~ (p=1.000 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 35.4k ± 1% 35.8k ± 0% +1.10% (p=0.008 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 34.6k ± 1% 34.9k ± 1% ~ (p=0.095 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 40.5k ± 0% 40.0k ± 1% -1.36% (p=0.008 n=5+5)
BM_SerializeDescriptor_Proto2 20.1k ± 1% 19.7k ± 4% ~ (p=0.421 n=5+5)
BM_SerializeDescriptor_Upb 33.7k ± 0% 33.7k ± 0% ~ (p=0.222 n=5+5)
name old allocs/op new allocs/op delta
BM_ArenaOneAlloc 1.00 ± 0% 1.00 ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/2 2.00 ± 0% 2.00 ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/8 8.00 ± 0% 8.00 ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/64 64.0 ± 0% 64.0 ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/128 128 ± 0% 128 ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/2 2.00 ± 0% 2.00 ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/8 8.00 ± 0% 8.00 ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/64 64.0 ± 0% 64.0 ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/128 128 ± 0% 128 ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Upb<NoLayout> 6.21k ± 0% 6.93k ± 0% +11.54% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 6.54k ± 0% 6.96k ± 0% +6.34% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 124k ± 0% 124k ± 0% +0.00% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 126k ± 0% 126k ± 0% +0.00% (p=0.008 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 7.00 ± 0% 7.00 ± 0% ~ (all samples are equal)
BM_Parse_Upb_FileDesc<UseArena, Alias> 7.00 ± 0% 7.00 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 709 ± 0% 709 ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 8.00 ± 0% 8.00 ± 0% ~ (all samples are equal)
name old peak-mem(Bytes)/op new peak-mem(Bytes)/op delta
BM_ArenaOneAlloc 328 ± 0% 328 ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/2 656 ± 0% 656 ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/8 2.62k ± 0% 2.62k ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/64 21.0k ± 0% 21.0k ± 0% ~ (all samples are equal)
BM_ArenaFuseUnbalanced/128 42.0k ± 0% 42.0k ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/2 656 ± 0% 656 ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/8 2.62k ± 0% 2.62k ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/64 21.0k ± 0% 21.0k ± 0% ~ (all samples are equal)
BM_ArenaFuseBalanced/128 42.0k ± 0% 42.0k ± 0% ~ (all samples are equal)
BM_LoadAdsDescriptor_Upb<NoLayout> 10.2M ± 0% 10.4M ± 0% +1.15% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 10.5M ± 0% 10.5M ± 0% +0.11% (p=0.008 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 7.14M ± 0% 7.14M ± 0% ~ (p=0.317 n=4+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 7.18M ± 0% 7.18M ± 0% ~ (p=0.159 n=5+4)
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_Proto2<FileDesc, NoArena, Copy> 35.4k ± 0% 35.4k ± 0% ~ (all samples are equal)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 65.3k ± 0% 65.3k ± 0% ~ (all samples are equal)
name old items/s new items/s delta
BM_ArenaFuseUnbalanced/2 28.2M ±12% 31.7M ± 3% ~ (p=0.095 n=5+5)
BM_ArenaFuseUnbalanced/8 15.7M ± 2% 15.1M ±14% ~ (p=0.421 n=5+5)
BM_ArenaFuseUnbalanced/64 13.7M ±18% 14.5M ± 9% ~ (p=0.841 n=5+5)
BM_ArenaFuseUnbalanced/128 13.2M ±12% 14.8M ± 5% ~ (p=0.095 n=5+5)
BM_ArenaFuseBalanced/2 29.9M ±12% 32.0M ± 3% ~ (p=0.841 n=5+5)
BM_ArenaFuseBalanced/8 14.8M ±28% 16.5M ±22% ~ (p=0.222 n=5+5)
BM_ArenaFuseBalanced/64 13.6M ±15% 15.1M ± 4% ~ (p=0.421 n=5+5)
BM_ArenaFuseBalanced/128 12.6M ±15% 14.9M ± 4% +17.88% (p=0.016 n=5+5)
name old speed new speed delta
BM_LoadAdsDescriptor_Upb<NoLayout> 128MB/s ±11% 128MB/s ±14% ~ (p=0.421 n=5+5)
BM_LoadAdsDescriptor_Upb<WithLayout> 115MB/s ±12% 119MB/s ± 3% ~ (p=0.690 n=5+5)
BM_LoadAdsDescriptor_Proto2<NoLayout> 52.9MB/s ±12% 57.6MB/s ± 3% ~ (p=0.421 n=5+5)
BM_LoadAdsDescriptor_Proto2<WithLayout> 52.6MB/s ±14% 57.2MB/s ± 2% ~ (p=0.548 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Copy> 527MB/s ±14% 571MB/s ± 3% ~ (p=0.548 n=5+5)
BM_Parse_Upb_FileDesc<UseArena, Alias> 595MB/s ±11% 640MB/s ± 3% ~ (p=0.222 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 553MB/s ±12% 582MB/s ± 3% ~ (p=1.000 n=5+5)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 576MB/s ±12% 649MB/s ± 3% ~ (p=0.056 n=5+5)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 307MB/s ±13% 334MB/s ± 8% ~ (p=0.310 n=5+5)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 653MB/s ±13% 689MB/s ± 2% ~ (p=1.000 n=5+5)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 650MB/s ±10% 708MB/s ± 3% ~ (p=0.310 n=5+5)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 564MB/s ±12% 614MB/s ± 4% ~ (p=0.310 n=5+5)
BM_SerializeDescriptor_Proto2 1.15GB/s ±12% 1.25GB/s ± 5% ~ (p=0.056 n=5+5)
BM_SerializeDescriptor_Upb 684MB/s ±12% 730MB/s ± 3% ~ (p=1.000 n=5+5)
```
This adds about 5Ki of code size. Some of this likely comes from the fact that we now link in `message/copy.c` to perform a deep copy of a FeatureSet proto.
```
$ /google/bin/releases/protobuf-team/bloaty/bloaty-google3-diff --blaze-build-opts="-c opt" third_party/upb/upb/conformance/conformance_upb
FILE SIZE VM SIZE
-------------- --------------
+0.5% +4.19Ki +0.5% +4.19Ki .text
+0.4% +656 +0.4% +656 .rodata
+0.1% +504 [ = ] 0 .strtab
+0.2% +384 [ = ] 0 .symtab
+0.2% +280 +0.2% +280 .eh_frame
+0.2% +216 +0.2% +216 .rela.dyn
+0.3% +96 +0.3% +96 .data.rel.ro
+0.2% +64 +0.2% +64 .eh_frame_hdr
+1.1% +16 [ = ] 0 .got.plt
+0.2% +8 +0.2% +8 .rela.plt
-4.6% -8 -4.6% -8 [LOAD #2 [RX]]
-50.0% -48 [ = ] 0 [Unmapped]
[ = ] 0 -81.7% -1.47Ki .relro_padding
+0.1% +6.30Ki +0.0% +4.00Ki TOTAL
```
PiperOrigin-RevId: 579321454
1 year ago
|
|
|
#undef UPB_DESC_MINITABLE
|
|
|
|
#undef UPB_IS_GOOGLE3
|
Allow for fuse/free races in `upb_Arena`.
Implementation is by kfm@, I only added the portability code around it.
`upb_Arena` was designed to be only thread-compatible. However, fusing of arenas muddies the waters somewhat, because two distinct `upb_Arena` objects will end up sharing state when fused. This causes a `upb_Arena_Free(a)` to interfere with `upb_Arena_Fuse(b, c)` if `a` and `b` were previously fused.
It turns out that we can use atomics to fix this with about a 35% regression in fuse performance (see below). Arena create+free does not regress, thanks to special-case logic in Free().
`upb_Arena` is still a thread-compatible type, and it is still never safe to call `upb_Arena_xxx(a)` and `upb_Arena_yyy(a)` in parallel. However you can at least now call `upb_Arena_Free(a)` and `upb_Arena_Fuse(b, c)` in parallel, even if `a` and `b` were previously fused.
Note that `upb_Arena_Fuse(a, b)` and `upb_Arena_Fuse(c, d)` is still not allowed if `b` and `c` were previously fused. In practice this means that fuses must still be single-threaded within a single fused group.
Performance results:
```
name old cpu/op new cpu/op delta
BM_ArenaOneAlloc 18.6ns ± 1% 18.6ns ± 1% ~ (p=0.726 n=18+17)
BM_ArenaInitialBlockOneAlloc 6.28ns ± 1% 5.73ns ± 1% -8.68% (p=0.000 n=17+20)
BM_ArenaFuseUnbalanced/2 44.1ns ± 2% 60.4ns ± 1% +37.05% (p=0.000 n=18+19)
BM_ArenaFuseUnbalanced/8 370ns ± 2% 500ns ± 1% +35.12% (p=0.000 n=19+20)
BM_ArenaFuseUnbalanced/64 3.52µs ± 1% 4.71µs ± 1% +33.80% (p=0.000 n=18+19)
BM_ArenaFuseUnbalanced/128 7.20µs ± 1% 9.72µs ± 2% +34.93% (p=0.000 n=16+19)
BM_ArenaFuseBalanced/2 44.4ns ± 2% 61.4ns ± 1% +38.23% (p=0.000 n=20+17)
BM_ArenaFuseBalanced/8 373ns ± 2% 509ns ± 1% +36.57% (p=0.000 n=19+17)
BM_ArenaFuseBalanced/64 3.55µs ± 2% 4.79µs ± 1% +34.80% (p=0.000 n=19+19)
BM_ArenaFuseBalanced/128 7.26µs ± 1% 9.76µs ± 1% +34.45% (p=0.000 n=17+19)
BM_LoadAdsDescriptor_Upb<NoLayout> 5.66ms ± 1% 5.69ms ± 1% +0.57% (p=0.013 n=18+20)
BM_LoadAdsDescriptor_Upb<WithLayout> 6.30ms ± 1% 6.36ms ± 1% +0.90% (p=0.000 n=19+18)
BM_LoadAdsDescriptor_Proto2<NoLayout> 12.1ms ± 1% 12.1ms ± 1% ~ (p=0.118 n=18+18)
BM_LoadAdsDescriptor_Proto2<WithLayout> 12.2ms ± 1% 12.3ms ± 1% +0.50% (p=0.006 n=18+18)
BM_Parse_Upb_FileDesc<UseArena, Copy> 12.7µs ± 1% 12.7µs ± 1% ~ (p=0.194 n=20+19)
BM_Parse_Upb_FileDesc<UseArena, Alias> 11.6µs ± 1% 11.6µs ± 1% ~ (p=0.192 n=20+20)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 12.5µs ± 1% 12.5µs ± 0% ~ (p=0.750 n=18+14)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 11.4µs ± 1% 11.3µs ± 1% -0.34% (p=0.046 n=19+19)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 25.4µs ± 1% 25.7µs ± 2% +1.37% (p=0.000 n=18+18)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 12.1µs ± 2% 12.1µs ± 1% ~ (p=0.143 n=18+18)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 11.9µs ± 3% 11.9µs ± 1% ~ (p=0.076 n=17+19)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 13.2µs ± 1% 13.2µs ± 1% ~ (p=0.053 n=19+19)
BM_SerializeDescriptor_Proto2 5.97µs ± 4% 5.90µs ± 4% ~ (p=0.093 n=17+19)
BM_SerializeDescriptor_Upb 10.4µs ± 1% 10.4µs ± 1% ~ (p=0.909 n=17+18)
name old time/op new time/op delta
BM_ArenaOneAlloc 18.7ns ± 2% 18.6ns ± 0% ~ (p=0.607 n=18+17)
BM_ArenaInitialBlockOneAlloc 6.29ns ± 1% 5.74ns ± 1% -8.71% (p=0.000 n=17+19)
BM_ArenaFuseUnbalanced/2 44.1ns ± 1% 60.6ns ± 1% +37.21% (p=0.000 n=17+19)
BM_ArenaFuseUnbalanced/8 371ns ± 2% 500ns ± 1% +35.02% (p=0.000 n=19+16)
BM_ArenaFuseUnbalanced/64 3.53µs ± 1% 4.72µs ± 1% +33.85% (p=0.000 n=18+19)
BM_ArenaFuseUnbalanced/128 7.22µs ± 1% 9.73µs ± 2% +34.87% (p=0.000 n=16+19)
BM_ArenaFuseBalanced/2 44.5ns ± 2% 61.5ns ± 1% +38.22% (p=0.000 n=20+17)
BM_ArenaFuseBalanced/8 373ns ± 2% 510ns ± 1% +36.58% (p=0.000 n=19+16)
BM_ArenaFuseBalanced/64 3.56µs ± 2% 4.80µs ± 1% +34.87% (p=0.000 n=19+19)
BM_ArenaFuseBalanced/128 7.27µs ± 1% 9.77µs ± 1% +34.40% (p=0.000 n=17+19)
BM_LoadAdsDescriptor_Upb<NoLayout> 5.67ms ± 1% 5.71ms ± 1% +0.60% (p=0.011 n=18+20)
BM_LoadAdsDescriptor_Upb<WithLayout> 6.32ms ± 1% 6.37ms ± 1% +0.87% (p=0.000 n=19+18)
BM_LoadAdsDescriptor_Proto2<NoLayout> 12.1ms ± 1% 12.2ms ± 1% ~ (p=0.126 n=18+19)
BM_LoadAdsDescriptor_Proto2<WithLayout> 12.2ms ± 1% 12.3ms ± 1% +0.51% (p=0.002 n=18+18)
BM_Parse_Upb_FileDesc<UseArena, Copy> 12.7µs ± 1% 12.7µs ± 1% ~ (p=0.149 n=20+19)
BM_Parse_Upb_FileDesc<UseArena, Alias> 11.6µs ± 1% 11.6µs ± 1% ~ (p=0.211 n=20+20)
BM_Parse_Upb_FileDesc<InitBlock, Copy> 12.5µs ± 1% 12.5µs ± 1% ~ (p=0.986 n=18+15)
BM_Parse_Upb_FileDesc<InitBlock, Alias> 11.4µs ± 1% 11.3µs ± 1% ~ (p=0.081 n=19+18)
BM_Parse_Proto2<FileDesc, NoArena, Copy> 25.4µs ± 1% 25.8µs ± 2% +1.41% (p=0.000 n=18+18)
BM_Parse_Proto2<FileDesc, UseArena, Copy> 12.1µs ± 2% 12.1µs ± 1% ~ (p=0.558 n=19+18)
BM_Parse_Proto2<FileDesc, InitBlock, Copy> 12.0µs ± 3% 11.9µs ± 1% ~ (p=0.165 n=17+19)
BM_Parse_Proto2<FileDescSV, InitBlock, Alias> 13.2µs ± 1% 13.2µs ± 1% ~ (p=0.070 n=19+19)
BM_SerializeDescriptor_Proto2 5.98µs ± 4% 5.92µs ± 3% ~ (p=0.138 n=17+19)
BM_SerializeDescriptor_Upb 10.4µs ± 1% 10.4µs ± 1% ~ (p=0.858 n=17+18)
```
PiperOrigin-RevId: 518573683
2 years ago
|
|
|
#undef UPB_ATOMIC
|
|
|
|
#undef UPB_USE_C11_ATOMICS
|
|
|
|
#undef UPB_PRIVATE
|
|
|
|
#undef UPB_ONLYBITS
|