⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Conversation

@ejona86
Copy link
Member

@ejona86 ejona86 commented Oct 8, 2025

Just a sketch, but the neat thing is if we trigger the ServiceLoader optimization, that will keep the constructors for us, so we could use the hard-coded list or the fake-ServiceLoader-iterator and it should work fine either way.

@ejona86 ejona86 changed the title Sketch: Trigger R8's ServiceLoader optimization Trigger R8's ServiceLoader optimization Jan 15, 2026
@ejona86
Copy link
Member Author

ejona86 commented Jan 15, 2026

I proved this works with R8 with optimizations by looking at the dex results. I am keeping it as draft as I need to investigate some potential runtime issues it might cause inside Google due to how the build is structured (related to cl/856425076).

My hacks to get proguard-android-optimize.txt limping along:

diff --git a/android-interop-testing/build.gradle b/android-interop-testing/build.gradle
index 17551465f..cb376e013 100644
--- a/android-interop-testing/build.gradle
+++ b/android-interop-testing/build.gradle
@@ -47,7 +47,7 @@ android {
         debug { minifyEnabled false }
         release {
             minifyEnabled true
-            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
         }
     }
     lintOptions {
@@ -78,10 +78,10 @@ dependencies {
             libraries.androidx.test.rules,
             libraries.opencensus.contrib.grpc.metrics
 
-    implementation (project(':grpc-services')) {
-        exclude group: 'com.google.protobuf'
-        exclude group: 'com.google.guava'
-    }
+    //implementation (project(':grpc-services')) {
+    //    exclude group: 'com.google.protobuf'
+    //    exclude group: 'com.google.guava'
+    //}
 
     androidTestImplementation 'androidx.test.ext:junit:1.1.3',
             'androidx.test:runner:1.4.0'
diff --git a/interop-testing/src/main/java/io/grpc/testing/integration/TestServiceImpl.java b/interop-testing/src/main/java/io/grpc/testing/integration/TestServiceImpl.java
index a9ee93824..bfd03b08e 100644
--- a/interop-testing/src/main/java/io/grpc/testing/integration/TestServiceImpl.java
+++ b/interop-testing/src/main/java/io/grpc/testing/integration/TestServiceImpl.java
@@ -28,8 +28,8 @@ import io.grpc.ServerInterceptor;
 import io.grpc.Status;
 import io.grpc.StatusRuntimeException;
 import io.grpc.internal.LogExceptionRunnable;
-import io.grpc.services.CallMetricRecorder;
-import io.grpc.services.MetricRecorder;
+//import io.grpc.services.CallMetricRecorder;
+//import io.grpc.services.MetricRecorder;
 import io.grpc.stub.ServerCallStreamObserver;
 import io.grpc.stub.StreamObserver;
 import io.grpc.testing.integration.Messages.Payload;
@@ -65,20 +65,20 @@ public class TestServiceImpl implements io.grpc.BindableService, AsyncService {
 
   private final ScheduledExecutorService executor;
   private final ByteString compressableBuffer;
-  private final MetricRecorder metricRecorder;
+  private final Object metricRecorder;
   final Semaphore lock = new Semaphore(1);
 
   /**
    * Constructs a controller using the given executor for scheduling response stream chunks.
    */
-  public TestServiceImpl(ScheduledExecutorService executor, MetricRecorder metricRecorder) {
+  public TestServiceImpl(ScheduledExecutorService executor, Object metricRecorder) {
     this.executor = executor;
     this.compressableBuffer = ByteString.copyFrom(new byte[1024]);
     this.metricRecorder = metricRecorder;
   }
 
   public TestServiceImpl(ScheduledExecutorService executor) {
-    this(executor, MetricRecorder.newInstance());
+    this(executor, null);//MetricRecorder.newInstance());
   }
 
   @Override
@@ -139,25 +139,25 @@ public class TestServiceImpl implements io.grpc.BindableService, AsyncService {
     responseObserver.onCompleted();
   }
 
-  private static void echoCallMetricsFromPayload(TestOrcaReport report) {
-    CallMetricRecorder recorder = CallMetricRecorder.getCurrent()
-        .recordCpuUtilizationMetric(report.getCpuUtilization())
-        .recordMemoryUtilizationMetric(report.getMemoryUtilization());
-    for (Map.Entry<String, Double> entry : report.getUtilizationMap().entrySet()) {
-      recorder.recordUtilizationMetric(entry.getKey(), entry.getValue());
-    }
-    for (Map.Entry<String, Double> entry : report.getRequestCostMap().entrySet()) {
-      recorder.recordRequestCostMetric(entry.getKey(), entry.getValue());
-    }
+  private static void echoCallMetricsFromPayload(TestOrcaReport unused) {
+    //CallMetricRecorder recorder = CallMetricRecorder.getCurrent()
+    //    .recordCpuUtilizationMetric(report.getCpuUtilization())
+    //    .recordMemoryUtilizationMetric(report.getMemoryUtilization());
+    //for (Map.Entry<String, Double> entry : report.getUtilizationMap().entrySet()) {
+    //  recorder.recordUtilizationMetric(entry.getKey(), entry.getValue());
+    //}
+    //for (Map.Entry<String, Double> entry : report.getRequestCostMap().entrySet()) {
+    //  recorder.recordRequestCostMetric(entry.getKey(), entry.getValue());
+    //}
   }
 
   private void echoMetricsFromPayload(TestOrcaReport report) {
-    metricRecorder.setCpuUtilizationMetric(report.getCpuUtilization());
-    metricRecorder.setMemoryUtilizationMetric(report.getMemoryUtilization());
-    metricRecorder.setAllUtilizationMetrics(new HashMap<>());
-    for (Map.Entry<String, Double> entry : report.getUtilizationMap().entrySet()) {
-      metricRecorder.putUtilizationMetric(entry.getKey(), entry.getValue());
-    }
+    //metricRecorder.setCpuUtilizationMetric(report.getCpuUtilization());
+    //metricRecorder.setMemoryUtilizationMetric(report.getMemoryUtilization());
+    //metricRecorder.setAllUtilizationMetrics(new HashMap<>());
+    //for (Map.Entry<String, Double> entry : report.getUtilizationMap().entrySet()) {
+    //  metricRecorder.putUtilizationMetric(entry.getKey(), entry.getValue());
+    //}
   }
 
   /**

This simplifies R8 Full Mode's configuration when paired with R8
optimizations (which is made more difficult to avoid in AGP 9), as when
the optimization is triggered Full Mode will automatically keep the
constructor for the relevant classes.

android-interop-test doesn't currently enable R8 optimizations, so it
doesn't actually demonstrate the benefit, but I have manually confirmed
that enabling proguard-android-optimize.txt does cause the ServiceLoader
optimization in android-interop-test. Note that full mode is a separate
configuration and not necessary to get the ServiceLoader optimization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants