-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make desugar dependencies deterministic,
desugar dependencies are added to the result jar file as metadata, this desugar dependency object contain few list that we found out the order of this list can be different in different build with same inputs, therefore final result will have a different hash and it cause cache miss in the builds solution is to make the lists in this object sorted. so for the same input we always get same output
- Loading branch information
1 parent
1af61b2
commit d383b5a
Showing
2 changed files
with
103 additions
and
24 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
.../com/google/devtools/build/android/desugar/dependencies/DesugarDependencyInfoWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package src.tools.android.java.com.google.devtools.build.android.desugar.dependencies; | ||
|
||
import com.google.devtools.build.android.desugar.proto.DesugarDeps; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
|
||
/** | ||
* a wrapper that is responsible to sort desugar dependency collector items | ||
* since this items will be add to the jar file as metadata it should be deterministic so it won't | ||
* cause cache misses | ||
*/ | ||
public class DesugarDependencyInfoWrapper { | ||
public final DesugarDeps.DesugarDepsInfo.Builder info = DesugarDeps.DesugarDepsInfo.newBuilder(); | ||
|
||
public ArrayList<DesugarDeps.Dependency> assume_present = new ArrayList<>(); | ||
public ArrayList<DesugarDeps.Dependency> missing_interface = new ArrayList<>(); | ||
public ArrayList<DesugarDeps.InterfaceDetails> interface_with_supertypes = new ArrayList<>(); | ||
public ArrayList<DesugarDeps.InterfaceWithCompanion> interface_with_companion = new ArrayList<>(); | ||
|
||
public void assumeCompanionClass(DesugarDeps.Dependency dependency) { | ||
assume_present.add(dependency); | ||
} | ||
|
||
public void missingImplementedInterface(DesugarDeps.Dependency dependency) { | ||
missing_interface.add(dependency); | ||
} | ||
|
||
public void recordExtendedInterfaces(DesugarDeps.InterfaceDetails interfaceDetails) { | ||
interface_with_supertypes.add(interfaceDetails); | ||
} | ||
|
||
public void recordDefaultMethods(DesugarDeps.InterfaceWithCompanion interfaceWithCompanion) { | ||
interface_with_companion.add(interfaceWithCompanion); | ||
} | ||
|
||
public byte[] toByteArray() { | ||
DesugarDeps.DesugarDepsInfo result = buildInfo(); | ||
return DesugarDeps.DesugarDepsInfo.getDefaultInstance().equals(result) ? null : result.toByteArray(); | ||
} | ||
|
||
private DesugarDeps.DesugarDepsInfo buildInfo() { | ||
|
||
assume_present.sort(dependencyComparator); | ||
missing_interface.sort(dependencyComparator); | ||
|
||
interface_with_supertypes.sort(interfaceDetailComparator); | ||
|
||
interface_with_companion.sort(interFaceWithCompanionComparator); | ||
|
||
info.addAllAssumePresent(assume_present); | ||
info.addAllMissingInterface(missing_interface); | ||
info.addAllInterfaceWithSupertypes(interface_with_supertypes); | ||
info.addAllInterfaceWithCompanion(interface_with_companion); | ||
return info.build(); | ||
} | ||
|
||
Comparator<? super DesugarDeps.Dependency> dependencyComparator = | ||
(Comparator<DesugarDeps.Dependency>) (o1, o2) -> { | ||
int diff = o1.getOrigin().getBinaryName().compareTo(o2.getOrigin().getBinaryName()); | ||
if (diff == 0) { | ||
return o1.getTarget().getBinaryName().compareTo(o2.getTarget().getBinaryName()); | ||
} | ||
return diff; | ||
}; | ||
Comparator<? super DesugarDeps.InterfaceDetails> interfaceDetailComparator = | ||
(Comparator<DesugarDeps.InterfaceDetails>) | ||
Comparator.comparing((DesugarDeps.InterfaceDetails o) -> | ||
o.getOrigin().getBinaryName()); | ||
Comparator<? super DesugarDeps.InterfaceWithCompanion> interFaceWithCompanionComparator = | ||
Comparator.comparing(o -> o.getOrigin().getBinaryName()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters