@@ -73,7 +73,7 @@ public class CompatDexBuilder {
73
73
74
74
private static final long ONE_MEG = 1024 * 1024 ;
75
75
76
- private static class ContextConsumer implements SyntheticInfoConsumer {
76
+ public static class ContextConsumer implements SyntheticInfoConsumer {
77
77
78
78
// After compilation this will be non-null iff the compiled class is a D8 synthesized class.
79
79
ClassReference sythesizedPrimaryClass = null ;
@@ -113,7 +113,7 @@ public void finished() {
113
113
114
114
private static class DexConsumer implements DexIndexedConsumer {
115
115
116
- final ContextConsumer contextConsumer = new ContextConsumer ();
116
+ ContextConsumer contextConsumer = new ContextConsumer ();
117
117
byte [] bytes ;
118
118
119
119
@ Override
@@ -131,6 +131,10 @@ void setBytes(byte[] byteCode) {
131
131
this .bytes = byteCode ;
132
132
}
133
133
134
+ void setContextConsumer (ContextConsumer contextConsumer ) {
135
+ this .contextConsumer = contextConsumer ;
136
+ }
137
+
134
138
ContextConsumer getContextConsumer () {
135
139
return contextConsumer ;
136
140
}
@@ -148,17 +152,17 @@ public static void main(String[] args)
148
152
PrintStream realStdErr = System .err ;
149
153
150
154
// Set up dexer cache
151
- Cache <DexingKeyR8 , byte [] > dexCache =
155
+ Cache <DexingKeyR8 , DexingEntryR8 > dexCache =
152
156
CacheBuilder .newBuilder ()
153
157
// Use at most 200 MB for cache and leave at least 25 MB of heap space alone. For
154
158
// reference:
155
159
// .class & class.dex files are around 1-5 KB, so this fits ~30K-35K class-dex pairs.
156
160
.maximumWeight (min (Runtime .getRuntime ().maxMemory () - 25 * ONE_MEG , 200 * ONE_MEG ))
157
161
.weigher (
158
- new Weigher <DexingKeyR8 , byte [] >() {
162
+ new Weigher <DexingKeyR8 , DexingEntryR8 >() {
159
163
@ Override
160
- public int weigh (DexingKeyR8 key , byte [] value ) {
161
- return key .classfileContent ().length + value .length ;
164
+ public int weigh (DexingKeyR8 key , DexingEntryR8 value ) {
165
+ return key .classfileContent ().length + value .dexContent (). length ;
162
166
}
163
167
})
164
168
.build ();
@@ -187,7 +191,7 @@ public int weigh(DexingKeyR8 key, byte[] value) {
187
191
}
188
192
189
193
private int processRequest (
190
- @ Nullable Cache <DexingKeyR8 , byte [] > dexCache ,
194
+ @ Nullable Cache <DexingKeyR8 , DexingEntryR8 > dexCache ,
191
195
DiagnosticsHandler diagnosticsHandler ,
192
196
List <String > args ,
193
197
PrintWriter pw ) {
@@ -205,7 +209,7 @@ private int processRequest(
205
209
206
210
@ SuppressWarnings ("JdkObsolete" )
207
211
private void dexEntries (
208
- @ Nullable Cache <DexingKeyR8 , byte [] > dexCache ,
212
+ @ Nullable Cache <DexingKeyR8 , DexingEntryR8 > dexCache ,
209
213
List <String > args ,
210
214
DiagnosticsHandler dexDiagnosticsHandler )
211
215
throws IOException , InterruptedException , ExecutionException , OptionsParsingException {
@@ -332,7 +336,7 @@ private void dexEntries(
332
336
}
333
337
334
338
private DexConsumer dexEntry (
335
- @ Nullable Cache <DexingKeyR8 , byte [] > dexCache ,
339
+ @ Nullable Cache <DexingKeyR8 , DexingEntryR8 > dexCache ,
336
340
ZipFile zipFile ,
337
341
ZipEntry classEntry ,
338
342
CompilationMode mode ,
@@ -349,18 +353,20 @@ private DexConsumer dexEntry(
349
353
.setMinApiLevel (minSdkVersion )
350
354
.setDisableDesugaring (true )
351
355
.setIntermediate (true );
352
- byte [] cachedDexBytes = null ;
356
+
357
+ DexingEntryR8 cachedDexEntry = null ;
353
358
byte [] classFileBytes = null ;
354
359
try (InputStream stream = zipFile .getInputStream (classEntry )) {
355
360
classFileBytes = ByteStreams .toByteArray (stream );
356
361
if (dexCache != null ) {
357
362
// If the cache exists, check for cache validity.
358
- cachedDexBytes =
363
+ cachedDexEntry =
359
364
dexCache .getIfPresent (DexingKeyR8 .create (mode , minSdkVersion , classFileBytes ));
360
365
}
361
- if (cachedDexBytes != null ) {
366
+ if (cachedDexEntry != null ) {
362
367
// Cache hit: quit early and return the data
363
- consumer .setBytes (cachedDexBytes );
368
+ consumer .setBytes (cachedDexEntry .dexContent ());
369
+ consumer .setContextConsumer (cachedDexEntry .contextConsumer ());
364
370
return consumer ;
365
371
}
366
372
builder .addClassProgramData (
@@ -371,7 +377,9 @@ private DexConsumer dexEntry(
371
377
D8 .run (builder .build (), executor );
372
378
// After dexing finishes, store the dexed output into the cache.
373
379
if (dexCache != null ) {
374
- dexCache .put (DexingKeyR8 .create (mode , minSdkVersion , classFileBytes ), consumer .getBytes ());
380
+ dexCache .put (
381
+ DexingKeyR8 .create (mode , minSdkVersion , classFileBytes ),
382
+ DexingEntryR8 .create (consumer .getBytes (), consumer .getContextConsumer ()));
375
383
}
376
384
return consumer ;
377
385
}
@@ -396,6 +404,25 @@ public static DexingKeyR8 create(
396
404
public abstract byte [] classfileContent ();
397
405
}
398
406
407
+ /**
408
+ * Represents a cache entry in the dex cache.
409
+ */
410
+ @ AutoValue
411
+ public abstract static class DexingEntryR8 {
412
+ public static DexingEntryR8 create (
413
+ byte [] dexContent , ContextConsumer contextConsumer ) {
414
+ return new AutoValue_CompatDexBuilder_DexingEntryR8 (
415
+ dexContent , contextConsumer );
416
+ }
417
+
418
+ @ SuppressWarnings ("mutable" )
419
+ public abstract byte [] dexContent ();
420
+
421
+ @ SuppressWarnings ("mutable" )
422
+ public abstract ContextConsumer contextConsumer ();
423
+ }
424
+
425
+
399
426
/**
400
427
* Custom implementation of DiagnosticsHandler that writes the info/warning diagnostics messages
401
428
* to original System#err stream instead of the WorkerResponse output. This keeps the Bazel
0 commit comments