Internal changes/optimizations

PiperOrigin-RevId: 632146543
Protobuf Team Bot 2024-05-09 07:43:54 -07:00 committed by Copybara-Service
parent bc80135f10
commit 85c57193d5
3 changed files with 16 additions and 10 deletions

View File

@ -53,10 +53,6 @@ public class ExtensionRegistryLite {
// applications. Need to support this feature on smaller granularity.
private static volatile boolean eagerlyParseMessageSets = false;
// short circuit the ExtensionRegistryFactory via assumevalues trickery
@SuppressWarnings("JavaOptionalSuggestions")
private static boolean doFullRuntimeInheritanceCheck = true;
// Visible for testing.
static final String EXTENSION_CLASS_NAME = "com.google.protobuf.Extension";
@ -88,9 +84,9 @@ public class ExtensionRegistryLite {
* available.
*/
public static ExtensionRegistryLite newInstance() {
return doFullRuntimeInheritanceCheck
? ExtensionRegistryFactory.create()
: new ExtensionRegistryLite();
return Protobuf.assumeLiteRuntime
? new ExtensionRegistryLite()
: ExtensionRegistryFactory.create();
}
private static volatile ExtensionRegistryLite emptyRegistry;
@ -100,7 +96,7 @@ public class ExtensionRegistryLite {
* ExtensionRegistry} (if the full (non-Lite) proto libraries are available).
*/
public static ExtensionRegistryLite getEmptyRegistry() {
if (!doFullRuntimeInheritanceCheck) {
if (Protobuf.assumeLiteRuntime) {
return EMPTY_REGISTRY_LITE;
}
ExtensionRegistryLite result = emptyRegistry;
@ -148,7 +144,7 @@ public class ExtensionRegistryLite {
if (GeneratedMessageLite.GeneratedExtension.class.isAssignableFrom(extension.getClass())) {
add((GeneratedMessageLite.GeneratedExtension<?, ?>) extension);
}
if (doFullRuntimeInheritanceCheck && ExtensionRegistryFactory.isFullRegistry(this)) {
if (!Protobuf.assumeLiteRuntime && ExtensionRegistryFactory.isFullRegistry(this)) {
try {
this.getClass().getMethod("add", ExtensionClassHolder.INSTANCE).invoke(this, extension);
} catch (Exception e) {

View File

@ -125,6 +125,9 @@ final class ManifestSchemaFactory implements SchemaFactory {
};
private static MessageInfoFactory getDescriptorMessageInfoFactory() {
if (Protobuf.assumeLiteRuntime) {
return EMPTY_FACTORY;
}
try {
Class<?> clazz = Class.forName("com.google.protobuf.DescriptorMessageInfoFactory");
return (MessageInfoFactory) clazz.getDeclaredMethod("getInstance").invoke(null);
@ -134,6 +137,6 @@ final class ManifestSchemaFactory implements SchemaFactory {
}
private static boolean useLiteRuntime(Class<?> messageType) {
return GeneratedMessageLite.class.isAssignableFrom(messageType);
return Protobuf.assumeLiteRuntime || GeneratedMessageLite.class.isAssignableFrom(messageType);
}
}

View File

@ -22,6 +22,13 @@ import java.util.concurrent.ConcurrentMap;
final class Protobuf {
private static final Protobuf INSTANCE = new Protobuf();
// short circuit the full runtime support via assumevalues trickery
// this value should only be set to true via AppReduce -assumevalues
// directives when doing whole program optimization of Android applications
// to enable discarding of full runtime support.
@SuppressWarnings("JavaOptionalSuggestions")
static boolean assumeLiteRuntime = false;
private final SchemaFactory schemaFactory;
// TODO: Consider using ClassValue instead.