From 23886fae77e160956f5f07ffe0f8797cefd5643d Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Wed, 9 Oct 2024 19:28:15 -0700 Subject: [PATCH] Throw NPE (instead of UnsupportedOperationException) if an invalid enum value is detected. This should never happen, so I don't think it matters much exactly what kind of exception we throw. We could even arguably return null, but this option saves a lot of space while still preserving some error checking. See https://godbolt.org/z/jKhcKs3x1 for code gen. This generates much tighter bytecode and ARM assembly than alternatives. As this code is generated many times over, small wins in code size here can reduce icache pressure, APK size, and OAT size. This java code: ```java Object uoe() { throw new UnsupportedOperationException(); } Object npe2() { throw null; } ``` Generates this dex code: ``` .method uoe()Ljava/lang/Object; new-instance v0, Ljava/lang/UnsupportedOperationException; invoke-direct {v0}, Ljava/lang/UnsupportedOperationException;->()V throw v0 .end method .method npe2()Ljava/lang/Object; const/4 v0, 0x0 throw v0 .end method ``` Which generates this OAT code: ``` java.lang.Object SomeProto.uoe() [84 bytes] 0x000081c0 sub x16, sp, #0x2000 (8192) 0x000081c4 ldr wzr, [x16] StackMap[0] native_pc=0x41c8, dex_pc=0x0, register_mask=0x0, stack_mask=0b 0x000081c8 str x0, [sp, #-48]! 0x000081cc str x22, [sp, #24] 0x000081d0 stp x23, lr, [sp, #32] 0x000081d4 ldr x21, [x21] StackMap[1] native_pc=0x41d8, dex_pc=0x0, register_mask=0x2, stack_mask=0b 0x000081d8 mov x22, x1 0x000081dc adrp x0, #+0x4000 (addr 0x0000c000) 0x000081e0 ldr w0, [x0, #4] 0x000081e4 ldr lr, [tr, #464] ; pAllocObjectInitialized 0x000081e8 blr lr StackMap[2] native_pc=0x41ec, dex_pc=0x0, register_mask=0x400000, stack_mask=0b 0x000081ec dmb ishst 0x000081f0 mov x1, x0 0x000081f4 mov x23, x1 0x000081f8 adrp x0, #+0x4000 (addr 0x0000c000) 0x000081fc ldr w0, [x0, #12] 0x00008200 ldr lr, [x0, #24] 0x00008204 blr lr StackMap[3] native_pc=0x4208, dex_pc=0x2, register_mask=0xc00000, stack_mask=0b 0x00008208 mov x0, x23 0x0000820c ldr lr, [tr, #1264] ; pDeliverException 0x00008210 blr lr StackMap[4] native_pc=0x4214, dex_pc=0x5, register_mask=0xc00000, stack_mask=0b java.lang.Object SomeProto.npe2() [36 bytes] 0x000080d0 sub x16, sp, #0x2000 (8192) 0x000080d4 ldr wzr, [x16] StackMap[0] native_pc=0x40d8, dex_pc=0x0, register_mask=0x0, stack_mask=0b 0x000080d8 str x0, [sp, #-32]! 0x000080dc stp x22, lr, [sp, #16] 0x000080e0 ldr x21, [x21] StackMap[1] native_pc=0x40e4, dex_pc=0x0, register_mask=0x2, stack_mask=0b 0x000080e4 mov x22, x1 0x000080e8 mov w0, #0x0 0x000080ec ldr lr, [tr, #1264] ; pDeliverException 0x000080f0 blr lr StackMap[2] native_pc=0x40f4, dex_pc=0x1, register_mask=0x400000, stack_mask=0b ``` This saves 84-36 = 48 bytes of OAT per method. PiperOrigin-RevId: 684258075 --- src/google/protobuf/compiler/java/lite/message.cc | 13 ++++++------- src/google/protobuf/message_lite.h | 5 +++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/google/protobuf/compiler/java/lite/message.cc b/src/google/protobuf/compiler/java/lite/message.cc index 79c3c3e642ed7..7b8a1284c81ce 100644 --- a/src/google/protobuf/compiler/java/lite/message.cc +++ b/src/google/protobuf/compiler/java/lite/message.cc @@ -357,7 +357,6 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { printer->Print( "}\n" - "// fall through\n" "case GET_DEFAULT_INSTANCE: {\n" " return DEFAULT_INSTANCE;\n" "}\n" @@ -384,8 +383,6 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { " return parser;\n", "classname", name_resolver_->GetImmutableClassName(descriptor_)); - printer->Outdent(); - if (HasRequiredFields(descriptor_)) { printer->Print( "}\n" @@ -409,11 +406,13 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { printer->Outdent(); printer->Print( - " }\n" - " throw new UnsupportedOperationException();\n" "}\n" - "\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "// Should never happen. Generates tight code to throw an exception.\n" + "throw null;\n"); + printer->Outdent(); + printer->Print( + "}\n" + "\n"); printer->Print( "\n" diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 58c5c2bdddd07..bbd9dc9b386d0 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -77,6 +77,10 @@ class MessageTableTester; namespace internal { +namespace v2 { +class TableDriven; +} // namespace v2 + class MessageCreator { public: using Func = void* (*)(const void*, void*, Arena*); @@ -987,6 +991,7 @@ class PROTOBUF_EXPORT MessageLite { friend class internal::WeakFieldMap; friend class internal::WireFormatLite; friend class internal::RustMapHelper; + friend class internal::v2::TableDriven; friend internal::MessageCreator; template