Skip to content
Permalink
Browse files

Remove duplicate source handling and unnecessary collection copying f…

…rom RTA.

PiperOrigin-RevId: 253813821
  • Loading branch information...
gkdn authored and Copybara-Service committed Jun 18, 2019
1 parent 34b6361 commit 9f441116524f3ece221d3ce0f86d65b91a3953dc
@@ -15,8 +15,6 @@
*/
package com.google.j2cl.tools.rta;

import static com.google.common.base.Preconditions.checkState;

import com.google.common.io.CharSink;
import com.google.common.io.Files;
import com.google.j2cl.libraryinfo.LibraryInfo;
@@ -26,9 +24,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
@@ -66,46 +63,23 @@ public static void main(String[] args) throws CmdLineException {
List<String> inputs = null;

private void run() {
LibraryInfo libraryInfo = mergeCallGraphFiles();
List<TypeInfo> typeInfos = collectTypeInfos();

RtaResult rtaResult = RapidTypeAnalyser.analyse(libraryInfo);
RtaResult rtaResult = RapidTypeAnalyser.analyse(typeInfos);

writeToFile(unusedTypesOutputFilePath, rtaResult.getUnusedTypes());
writeToFile(unusedMembersOutputFilePath, rtaResult.getUnusedMembers());
writeToFile(removalCodeInfoOutputFilePath, rtaResult.getCodeRemovalInfo());
}

private LibraryInfo mergeCallGraphFiles() {
private List<TypeInfo> collectTypeInfos() {
try {
Map<String, TypeInfo> typeInfosByName = new HashMap<>();

// TODO(b/112662982): improve performance by reading file contents in parallel.
List<TypeInfo> typeInfos = new ArrayList<>();
for (String callGraphPath : inputs) {
LibraryInfo libraryInfo = LibraryInfo.parseFrom(new FileInputStream(callGraphPath));

// Because J2CL proto emits duplicate sources (see b/36486919), we can see several TypeInfos
// with the same name. When we reach that case, check that the TypeInfo are the same and
// throw an exception if they are different.
// TODO(b/36486919): remove that logic when the bug is fixed.
for (TypeInfo typeInfo : libraryInfo.getTypeList()) {
String typeId = typeInfo.getTypeId();
if (typeInfosByName.containsKey(typeId)) {
TypeInfo existingTypeInfo = typeInfosByName.get(typeId);
checkState(
typeInfo.equals(existingTypeInfo),
"Got two different TypeInfo for the same type id.\n"
+ "TypeId: [%s]\n"
+ "Existing TypeInfo: [%s]\n"
+ "New TypeInfo: [%s]",
typeId,
existingTypeInfo,
typeInfo);
}
typeInfosByName.put(typeId, typeInfo);
}
typeInfos.addAll(libraryInfo.getTypeList());
}

return LibraryInfo.newBuilder().addAllType(typeInfosByName.values()).build();
return typeInfos;
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -19,15 +19,15 @@
import static java.util.stream.Collectors.toSet;

import com.google.j2cl.libraryinfo.InvocationKind;
import com.google.j2cl.libraryinfo.LibraryInfo;
import com.google.j2cl.libraryinfo.TypeInfo;
import java.util.Collection;
import java.util.List;
import java.util.Set;

final class RapidTypeAnalyser {

static RtaResult analyse(LibraryInfo libraryInfo) {
List<Type> types = TypeGraphBuilder.build(libraryInfo);
static RtaResult analyse(List<TypeInfo> typeInfos) {
List<Type> types = TypeGraphBuilder.build(typeInfos);

// Go over the entry points to start the traversal.
types.stream()
@@ -24,7 +24,6 @@
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;
import com.google.j2cl.libraryinfo.LibraryInfo;
import com.google.j2cl.libraryinfo.MemberInfo;
import com.google.j2cl.libraryinfo.MethodInvocation;
import com.google.j2cl.libraryinfo.TypeInfo;
@@ -38,8 +37,8 @@
/** Give information about inheritance relationships between types. */
public class TypeGraphBuilder {

static List<Type> build(LibraryInfo libraryInfo) {
List<Type> types = createTypes(libraryInfo);
static List<Type> build(List<TypeInfo> typeInfos) {
List<Type> types = createTypes(typeInfos);

LinkedHashMultiset<Type> typesInTopologicalOrder = sortTypesInTopologicalOrder(types);

@@ -50,17 +49,17 @@
return types;
}

private static List<Type> createTypes(LibraryInfo libraryInfo) {
private static List<Type> createTypes(List<TypeInfo> typeInfos) {
Map<String, Type> typesByName = new HashMap<>();

// Create all types and members.
for (TypeInfo typeInfo : libraryInfo.getTypeList()) {
for (TypeInfo typeInfo : typeInfos) {
Type type = Type.buildFrom(typeInfo);
typesByName.put(type.getName(), type);
}

// Build cross-references between types and members
for (TypeInfo typeInfo : libraryInfo.getTypeList()) {
for (TypeInfo typeInfo : typeInfos) {
Type type = typesByName.get(typeInfo.getTypeId());
type.setSuperTypes(
concat(Stream.of(typeInfo.getExtendsType()), typeInfo.getImplementsTypeList().stream())

0 comments on commit 9f44111

Please sign in to comment.
You can’t perform that action at this time.