private String makeUnique(Set<String> knowAliases, ObjectIntMap<String> indexMap, String alias) { String uniqueAlias = alias; int index = indexMap.get(alias); if (index > 0) { uniqueAlias = alias + index++; } while (!knowAliases.add(uniqueAlias)) { uniqueAlias = alias + index++; } indexMap.put(alias, index); return uniqueAlias; } }
private void printStats(Renderer renderer, int totalSize) { if (!Boolean.parseBoolean(System.getProperty("teavm.js.stats", "false"))) { return; } System.out.println("Total output size: " + STATS_NUM_FORMAT.format(totalSize)); System.out.println("Metadata size: " + getSizeWithPercentage(renderer.getMetadataSize(), totalSize)); System.out.println("String pool size: " + getSizeWithPercentage(renderer.getStringPoolSize(), totalSize)); ObjectIntMap<String> packageSizeMap = new ObjectIntHashMap<>(); for (String className : renderer.getClassesInStats()) { String packageName = className.substring(0, className.lastIndexOf('.') + 1); int classSize = renderer.getClassSize(className); packageSizeMap.put(packageName, packageSizeMap.getOrDefault(packageName, 0) + classSize); } String[] packageNames = packageSizeMap.keys().toArray(String.class); Arrays.sort(packageNames, Comparator.comparing(p -> -packageSizeMap.getOrDefault(p, 0))); for (String packageName : packageNames) { System.out.println("Package '" + packageName + "' size: " + getSizeWithPercentage(packageSizeMap.get(packageName), totalSize)); } }
boolean processNodes() { boolean hasAny = false; for (DefaultCallGraphNode node : nodesToProcess.toArray(new DefaultCallGraphNode[0])) { int index = nodeToIndex.get(node); SerializableCallGraph.Node serializableNode = nodes.get(index); serializableNode.method = node.getMethod(); serializableNode.callSites = node.getCallSites().stream() .mapToInt(this::getCallSite) .toArray(); serializableNode.callerCallSites = node.getCallerCallSites().stream() .mapToInt(this::getCallSite) .toArray(); serializableNode.fieldAccessSites = node.getFieldAccessSites().stream() .mapToInt(this::getFieldAccess) .toArray(); hasAny = true; } nodesToProcess.clear(); return hasAny; }
boolean processFieldAccess() { boolean hasAny = false; for (DefaultFieldAccessSite accessSite : fieldAccessToProcess.toArray(new DefaultFieldAccessSite[0])) { int index = fieldAccessToIndex.get(accessSite); SerializableCallGraph.FieldAccess sfa = fieldAccessList.get(index); sfa.location = accessSite.getLocation(); sfa.field = accessSite.getField(); sfa.callee = getNode(accessSite.getCallee()); hasAny = true; } fieldAccessToProcess.clear(); return hasAny; }
boolean processCallSites() { boolean hasAny = false; for (DefaultCallSite callSite : callSitesToProcess.toArray(new DefaultCallSite[0])) { int index = callSiteToIndex.get(callSite); SerializableCallGraph.CallSite scs = callSites.get(index); scs.location = callSite.getLocation(); scs.caller = getNode(callSite.getCaller()); scs.callee = getNode(callSite.getCallee()); hasAny = true; } callSitesToProcess.clear(); return hasAny; }
public int getFieldOffset(FieldReference field) { ValueType type = ValueType.object(field.getClassName()); addClass(type); ClassBinaryData data = binaryDataMap.get(type); return data.fieldLayout.get(field.getFieldName()); }
private int generateEnumValues(ClassReader cls, ClassBinaryData binaryData) { FieldReader[] fields = cls.getFields().stream() .filter(field -> field.hasModifier(ElementModifier.ENUM)) .toArray(FieldReader[]::new); DataValue sizeValue = DataPrimitives.ADDRESS.createValue(); sizeValue.setAddress(0, fields.length); int valuesAddress = binaryWriter.append(sizeValue); for (FieldReader field : fields) { DataValue fieldRefValue = DataPrimitives.ADDRESS.createValue(); fieldRefValue.setAddress(0, binaryData.fieldLayout.get(field.getName())); binaryWriter.append(fieldRefValue); } return valuesAddress; }
for (FieldReference field : fields) { DataValue layoutElement = DataPrimitives.SHORT.createValue(); int offset = binaryData.fieldLayout.get(field.getFieldName()); layoutElement.setShort(0, (short) offset); binaryWriter.append(layoutElement); staticGcRoots.add(binaryData.fieldLayout.get(field.getFieldName()));
public int get(final T key) { return map.get(key); }
boolean processClassAccess() { boolean hasAny = false; for (DefaultClassAccessSite accessSite : classAccessToProcess.toArray(new DefaultClassAccessSite[0])) { int index = classAccessToIndex.get(accessSite); SerializableCallGraph.ClassAccess sca = classAccessList.get(index); sca.location = accessSite.getLocation(); sca.className = accessSite.getClassName(); sca.callee = getNode(accessSite.getCallee()); hasAny = true; } classAccessToProcess.clear(); return hasAny; }
boolean processFieldAccess() { boolean hasAny = false; for (DefaultFieldAccessSite accessSite : fieldAccessToProcess.toArray(new DefaultFieldAccessSite[0])) { int index = fieldAccessToIndex.get(accessSite); SerializableCallGraph.FieldAccess sfa = fieldAccessList.get(index); sfa.location = accessSite.getLocation(); sfa.field = accessSite.getField(); sfa.callee = getNode(accessSite.getCallee()); hasAny = true; } fieldAccessToProcess.clear(); return hasAny; }
boolean processNodes() { boolean hasAny = false; for (DefaultCallGraphNode node : nodesToProcess.toArray(new DefaultCallGraphNode[0])) { int index = nodeToIndex.get(node); SerializableCallGraph.Node serializableNode = nodes.get(index); serializableNode.method = node.getMethod(); serializableNode.callSites = node.getCallSites().stream() .mapToInt(this::getCallSite) .toArray(); serializableNode.callerCallSites = node.getCallerCallSites().stream() .mapToInt(this::getCallSite) .toArray(); serializableNode.fieldAccessSites = node.getFieldAccessSites().stream() .mapToInt(this::getFieldAccess) .toArray(); serializableNode.classAccessSites = node.getClassAccessSites().stream() .mapToInt(this::getClassAccess) .toArray(); hasAny = true; } nodesToProcess.clear(); return hasAny; }
boolean processCallSites() { boolean hasAny = false; for (DefaultCallSite callSite : callSitesToProcess.toArray(new DefaultCallSite[0])) { int index = callSiteToIndex.get(callSite); SerializableCallGraph.CallSite scs = callSites.get(index); scs.location = callSite.getLocation(); scs.caller = getNode(callSite.getCaller()); scs.callee = getNode(callSite.getCallee()); hasAny = true; } callSitesToProcess.clear(); return hasAny; }
@Override public VisitorBehavior visit(final AbstractPort<?> port) { IPipe<?> pipe = port.getPipe(); AbstractStage targetStage = pipe.getTargetPort().getOwningStage(); int targetColor = colors.containsKey(targetStage) ? colors.get(targetStage) : DEFAULT_COLOR; if (!threadableStages.contains(targetStage) || targetColor == color) { if (colors.containsKey(targetStage) && colors.get(targetStage) != color) { throw new IllegalStateException("1001 - Crossing threads in " + targetStage.getId()); // One stage is connected to a stage of another thread // (but not its "headstage") } colors.put(targetStage, color); return VisitorBehavior.CONTINUE_FORWARD; // NOPMD makes it clearer } return VisitorBehavior.STOP; }
@Override public VisitorBehavior visit(final AbstractPort<?> port) { IPipe<?> pipe = port.getPipe(); AbstractStage targetStage = pipe.getTargetPort().getOwningStage(); int targetColor = colors.containsKey(targetStage) ? colors.get(targetStage) : DEFAULT_COLOR; if (!threadableStages.contains(targetStage) || targetColor == color) { if (colors.containsKey(targetStage) && colors.get(targetStage) != color) { throw new IllegalStateException("1001 - Crossing threads in " + targetStage.getId()); // One stage is connected to a stage of another thread // (but not its "headstage") } colors.put(targetStage, color); return VisitorBehavior.CONTINUE_FORWARD; // NOPMD makes it clearer } return VisitorBehavior.STOP; }
public static Graph toGraph(List<MutableGraphNode> nodes) { ObjectIntMap<MutableGraphNode> map = new ObjectIntOpenHashMap<>(); for (int i = 0; i < nodes.size(); ++i) { map.put(nodes.get(i), i); } GraphBuilder builder = new GraphBuilder(nodes.size()); for (int i = 0; i < nodes.size(); ++i) { for (MutableGraphEdge edge : nodes.get(i).getEdges()) { int successor = map.get(edge.getSecond()); builder.addEdge(i, successor); } } return builder.build(); } }
public int getFieldOffset(FieldReference field) { ValueType type = ValueType.object(field.getClassName()); addClass(type); ClassBinaryData data = binaryDataMap.get(type); return data.fieldLayout.get(field.getFieldName()); }
private int generateEnumValues(ClassReader cls, ClassBinaryData binaryData) { FieldReader[] fields = cls.getFields().stream() .filter(field -> field.hasModifier(ElementModifier.ENUM)) .toArray(FieldReader[]::new); DataValue sizeValue = DataPrimitives.ADDRESS.createValue(); sizeValue.setAddress(0, fields.length); int valuesAddress = binaryWriter.append(sizeValue); for (FieldReader field : fields) { DataValue fieldRefValue = DataPrimitives.ADDRESS.createValue(); fieldRefValue.setAddress(0, binaryData.fieldLayout.get(field.getName())); binaryWriter.append(fieldRefValue); } return valuesAddress; }
for (FieldReference field : fields) { DataValue layoutElement = DataPrimitives.SHORT.createValue(); int offset = binaryData.fieldLayout.get(field.getFieldName()); layoutElement.setShort(0, (short) offset); binaryWriter.append(layoutElement); staticGcRoots.add(binaryData.fieldLayout.get(field.getFieldName()));