Skip to content

Commit c69dd68

Browse files
habermancopybara-github
authored andcommitted
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 parent c1e0853 commit c69dd68

39 files changed

+593
-196
lines changed

src/google/protobuf/compiler/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ cc_binary(
164164
copts = COPTS,
165165
visibility = [
166166
"//src/google/protobuf:__subpackages__",
167+
"//upb:__subpackages__",
167168
],
168169
deps = [
169170
":command_line_interface",

upb/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ upb_amalgamation(
406406
":mem_internal",
407407
":message",
408408
":message_accessors",
409+
":message_copy",
409410
":message_internal",
410411
":message_internal_types",
411412
":message_tagged_ptr",
@@ -456,6 +457,7 @@ upb_amalgamation(
456457
":mem_internal",
457458
":message",
458459
":message_accessors",
460+
":message_copy",
459461
":message_internal",
460462
":message_internal_types",
461463
":message_tagged_ptr",
@@ -506,6 +508,7 @@ upb_amalgamation(
506508
":mem_internal",
507509
":message",
508510
":message_accessors",
511+
":message_copy",
509512
":message_internal",
510513
":message_internal_types",
511514
":message_tagged_ptr",

upb/port/def.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
323323

324324
#if defined(UPB_IS_GOOGLE3) && !defined(UPB_BOOTSTRAP_STAGE0)
325325
#define UPB_DESC(sym) proto2_##sym
326+
#define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init
327+
#elif defined(UPB_BOOTSTRAP_STAGE0)
328+
#define UPB_DESC(sym) google_protobuf_##sym
329+
#define UPB_DESC_MINITABLE(sym) google__protobuf__##sym##_msg_init()
326330
#else
327331
#define UPB_DESC(sym) google_protobuf_##sym
332+
#define UPB_DESC_MINITABLE(sym) &google__protobuf__##sym##_msg_init
328333
#endif

upb/port/undef.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#undef UPB_GNUC_MIN
5050
#undef UPB_DESCRIPTOR_UPB_H_FILENAME
5151
#undef UPB_DESC
52+
#undef UPB_DESC_MINITABLE
5253
#undef UPB_IS_GOOGLE3
5354
#undef UPB_ATOMIC
5455
#undef UPB_USE_C11_ATOMICS

upb/reflection/BUILD

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
# license that can be found in the LICENSE file or at
66
# https://developers.google.com/open-source/licenses/bsd
77

8+
load(
9+
"//upb/cmake:build_defs.bzl",
10+
"staleness_test",
11+
)
12+
load(
13+
"//src/google/protobuf/editions:defaults.bzl",
14+
"compile_edition_defaults",
15+
"embed_edition_defaults",
16+
)
817
load("//bazel:build_defs.bzl", "UPB_DEFAULT_COPTS")
918
load("//bazel:upb_minitable_proto_library.bzl", "upb_minitable_proto_library")
1019
load("//bazel:upb_proto_library.bzl", "upb_proto_reflection_library")
@@ -108,6 +117,7 @@ bootstrap_cc_library(
108117
"internal/method_def.h",
109118
"internal/oneof_def.h",
110119
"internal/service_def.h",
120+
"internal/upb_edition_defaults.h",
111121
"message.h",
112122
"message.hpp",
113123
"message_def.h",
@@ -125,11 +135,13 @@ bootstrap_cc_library(
125135
"//upb:mem",
126136
"//upb:message",
127137
"//upb:message_accessors",
138+
"//upb:message_copy",
128139
"//upb:message_value",
129140
"//upb:mini_descriptor",
130141
"//upb:mini_descriptor_internal",
131142
"//upb:mini_table",
132143
"//upb:port",
144+
"//upb/base:internal",
133145
],
134146
)
135147

@@ -171,6 +183,30 @@ cc_test(
171183
],
172184
)
173185

186+
compile_edition_defaults(
187+
name = "upb_edition_defaults",
188+
srcs = [
189+
"//:descriptor_proto",
190+
],
191+
maximum_edition = "2023",
192+
minimum_edition = "PROTO2",
193+
)
194+
195+
embed_edition_defaults(
196+
name = "embedded_upb_edition_defaults_generate",
197+
defaults = "upb_edition_defaults",
198+
output = "generated/internal/upb_edition_defaults.h",
199+
placeholder = "DEFAULTS_VALUE",
200+
template = "internal/upb_edition_defaults.h.template",
201+
)
202+
203+
staleness_test(
204+
name = "bootstrap_upb_defaults_staleness_test",
205+
outs = ["internal/upb_edition_defaults.h"],
206+
generated_pattern = "generated/%s",
207+
target_files = ["internal/upb_edition_defaults.h"],
208+
)
209+
174210
# begin:github_only
175211
filegroup(
176212
name = "source_files",

upb/reflection/def_pool.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "upb/reflection/internal/file_def.h"
1818
#include "upb/reflection/internal/message_def.h"
1919
#include "upb/reflection/internal/service_def.h"
20+
#include "upb/reflection/internal/upb_edition_defaults.h"
2021

2122
// Must be last.
2223
#include "upb/port/def.inc"
@@ -27,6 +28,7 @@ struct upb_DefPool {
2728
upb_strtable files; // file_name -> (upb_FileDef*)
2829
upb_inttable exts; // (upb_MiniTableExtension*) -> (upb_FieldDef*)
2930
upb_ExtensionRegistry* extreg;
31+
const UPB_DESC(FeatureSetDefaults) * feature_set_defaults;
3032
upb_MiniTablePlatform platform;
3133
void* scratch_data;
3234
size_t scratch_size;
@@ -39,6 +41,8 @@ void upb_DefPool_Free(upb_DefPool* s) {
3941
upb_gfree(s);
4042
}
4143

44+
static const char serialized_defaults[] = UPB_INTERNAL_UPB_EDITION_DEFAULTS;
45+
4246
upb_DefPool* upb_DefPool_New(void) {
4347
upb_DefPool* s = upb_gmalloc(sizeof(*s));
4448
if (!s) return NULL;
@@ -58,6 +62,10 @@ upb_DefPool* upb_DefPool_New(void) {
5862
if (!s->extreg) goto err;
5963

6064
s->platform = kUpb_MiniTablePlatform_Native;
65+
s->feature_set_defaults = UPB_DESC(FeatureSetDefaults_parse)(
66+
serialized_defaults, sizeof(serialized_defaults) - 1, s->arena);
67+
68+
if (!s->feature_set_defaults) goto err;
6169

6270
return s;
6371

@@ -66,6 +74,11 @@ upb_DefPool* upb_DefPool_New(void) {
6674
return NULL;
6775
}
6876

77+
const UPB_DESC(FeatureSetDefaults) *
78+
upb_DefPool_FeatureSetDefaults(const upb_DefPool* s) {
79+
return s->feature_set_defaults;
80+
}
81+
6982
bool _upb_DefPool_InsertExt(upb_DefPool* s, const upb_MiniTableExtension* ext,
7083
const upb_FieldDef* f) {
7184
return upb_inttable_insert(&s->exts, (uintptr_t)ext, upb_value_constptr(f),
@@ -279,7 +292,11 @@ static const upb_FileDef* upb_DefBuilder_AddFileToPool(
279292
remove_filedef(s, builder->file);
280293
builder->file = NULL;
281294
}
282-
} else if (!builder->arena || !builder->tmp_arena) {
295+
} else if (!builder->arena || !builder->tmp_arena ||
296+
!upb_strtable_init(&builder->feature_cache, 16,
297+
builder->tmp_arena) ||
298+
!(builder->legacy_features =
299+
UPB_DESC(FeatureSet_new)(builder->tmp_arena))) {
283300
_upb_DefBuilder_OomErr(builder);
284301
} else {
285302
_upb_FileDef_Create(builder, file_proto);
@@ -312,6 +329,8 @@ static const upb_FileDef* _upb_DefPool_AddFile(
312329

313330
upb_DefBuilder ctx = {
314331
.symtab = s,
332+
.tmp_buf = NULL,
333+
.tmp_buf_size = 0,
315334
.layout = layout,
316335
.platform = s->platform,
317336
.msg_count = 0,

upb/reflection/def_pool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ UPB_API void upb_DefPool_Free(upb_DefPool* s);
2626

2727
UPB_API upb_DefPool* upb_DefPool_New(void);
2828

29+
UPB_API const UPB_DESC(FeatureSetDefaults) *
30+
upb_DefPool_FeatureSetDefaults(const upb_DefPool* s);
31+
2932
UPB_API const upb_MessageDef* upb_DefPool_FindMessageByName(
3033
const upb_DefPool* s, const char* sym);
3134

upb/reflection/enum_def.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
#include "upb/port/def.inc"
2424

2525
struct upb_EnumDef {
26-
const UPB_DESC(EnumOptions) * opts;
26+
const UPB_DESC(EnumOptions*) opts;
27+
const UPB_DESC(FeatureSet*) resolved_features;
2728
const upb_MiniTableEnum* layout; // Only for proto2.
2829
const upb_FileDef* file;
2930
const upb_MessageDef* containing_type; // Could be merged with "file".
@@ -37,8 +38,10 @@ struct upb_EnumDef {
3738
int res_range_count;
3839
int res_name_count;
3940
int32_t defaultval;
40-
bool is_closed;
4141
bool is_sorted; // Whether all of the values are defined in ascending order.
42+
#if UINTPTR_MAX == 0xffffffff
43+
uint32_t padding; // Increase size to a multiple of 8.
44+
#endif
4245
};
4346

4447
upb_EnumDef* _upb_EnumDef_At(const upb_EnumDef* e, int i) {
@@ -140,7 +143,11 @@ const upb_EnumValueDef* upb_EnumDef_Value(const upb_EnumDef* e, int i) {
140143
return _upb_EnumValueDef_At(e->values, i);
141144
}
142145

143-
bool upb_EnumDef_IsClosed(const upb_EnumDef* e) { return e->is_closed; }
146+
bool upb_EnumDef_IsClosed(const upb_EnumDef* e) {
147+
if (UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3) return false;
148+
return UPB_DESC(FeatureSet_enum_type)(e->resolved_features) ==
149+
UPB_DESC(FeatureSet_CLOSED);
150+
}
144151

145152
bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
146153
upb_StringView* out) {
@@ -209,13 +216,18 @@ static upb_StringView* _upb_EnumReservedNames_New(
209216

210217
static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
211218
const UPB_DESC(EnumDescriptorProto) * enum_proto,
219+
const UPB_DESC(FeatureSet*) parent_features,
212220
upb_EnumDef* e) {
213221
const UPB_DESC(EnumValueDescriptorProto)* const* values;
214222
const UPB_DESC(EnumDescriptorProto_EnumReservedRange)* const* res_ranges;
215223
const upb_StringView* res_names;
216224
upb_StringView name;
217225
size_t n_value, n_res_range, n_res_name;
218226

227+
UPB_DEF_SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto);
228+
e->resolved_features = _upb_DefBuilder_ResolveFeatures(
229+
ctx, parent_features, UPB_DESC(EnumOptions_features)(e->opts));
230+
219231
// Must happen before _upb_DefBuilder_Add()
220232
e->file = _upb_DefBuilder_File(ctx);
221233

@@ -225,9 +237,6 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
225237
_upb_DefBuilder_Add(ctx, e->full_name,
226238
_upb_DefType_Pack(e, UPB_DEFTYPE_ENUM));
227239

228-
e->is_closed = (!UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3) &&
229-
(upb_FileDef_Syntax(e->file) == kUpb_Syntax_Proto2);
230-
231240
values = UPB_DESC(EnumDescriptorProto_value)(enum_proto, &n_value);
232241

233242
bool ok = upb_strtable_init(&e->ntoi, n_value, ctx->arena);
@@ -238,8 +247,8 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
238247

239248
e->defaultval = 0;
240249
e->value_count = n_value;
241-
e->values =
242-
_upb_EnumValueDefs_New(ctx, prefix, n_value, values, e, &e->is_sorted);
250+
e->values = _upb_EnumValueDefs_New(ctx, prefix, n_value, values,
251+
e->resolved_features, e, &e->is_sorted);
243252

244253
if (n_value == 0) {
245254
_upb_DefBuilder_Errf(ctx, "enums must contain at least one value (%s)",
@@ -256,11 +265,9 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
256265
e->res_name_count = n_res_name;
257266
e->res_names = _upb_EnumReservedNames_New(ctx, n_res_name, res_names);
258267

259-
UPB_DEF_SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto);
260-
261268
upb_inttable_compact(&e->iton, ctx->arena);
262269

263-
if (e->is_closed) {
270+
if (upb_EnumDef_IsClosed(e)) {
264271
if (ctx->layout) {
265272
UPB_ASSERT(ctx->enum_count < ctx->layout->enum_count);
266273
e->layout = ctx->layout->enums[ctx->enum_count++];
@@ -272,10 +279,11 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
272279
}
273280
}
274281

275-
upb_EnumDef* _upb_EnumDefs_New(
276-
upb_DefBuilder* ctx, int n,
277-
const UPB_DESC(EnumDescriptorProto) * const* protos,
278-
const upb_MessageDef* containing_type) {
282+
upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n,
283+
const UPB_DESC(EnumDescriptorProto*)
284+
const* protos,
285+
const UPB_DESC(FeatureSet*) parent_features,
286+
const upb_MessageDef* containing_type) {
279287
_upb_DefType_CheckPadding(sizeof(upb_EnumDef));
280288

281289
// If a containing type is defined then get the full name from that.
@@ -285,7 +293,7 @@ upb_EnumDef* _upb_EnumDefs_New(
285293

286294
upb_EnumDef* e = _upb_DefBuilder_Alloc(ctx, sizeof(upb_EnumDef) * n);
287295
for (int i = 0; i < n; i++) {
288-
create_enumdef(ctx, name, protos[i], &e[i]);
296+
create_enumdef(ctx, name, protos[i], parent_features, &e[i]);
289297
e[i].containing_type = containing_type;
290298
}
291299
return e;

upb/reflection/enum_def.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
4040

4141
const char* upb_EnumDef_Name(const upb_EnumDef* e);
4242
const UPB_DESC(EnumOptions) * upb_EnumDef_Options(const upb_EnumDef* e);
43+
const UPB_DESC(FeatureSet) * upb_EnumDef_ResolvedFeatures(const upb_EnumDef* e);
4344

4445
upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i);
4546
int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e);

0 commit comments

Comments
 (0)