From adc56a59ea6cc6cd23a72534f5e7eea3fa3b0c61 Mon Sep 17 00:00:00 2001 From: Eugene Zhuravlev Date: Tue, 16 Apr 2024 21:42:01 +0200 Subject: [PATCH] JPS mappings for incremental compilation refactoring: introduce special 'File' nodes for sources without any classes GitOrigin-RevId: 22c8e037790b872011d01d762dd2be0d6f9e635a --- .../BackendCallbackToGraphDeltaAdapter.java | 13 ++- .../jps/dependency/java/FileNode.java | 110 ++++++++++++++++++ .../classBecameInterface/build.log | 1 + .../supertypesListChanged/build.log | 2 + 4 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 jps/jps-builders/src/org/jetbrains/jps/dependency/java/FileNode.java diff --git a/jps/jps-builders/src/org/jetbrains/jps/builders/java/BackendCallbackToGraphDeltaAdapter.java b/jps/jps-builders/src/org/jetbrains/jps/builders/java/BackendCallbackToGraphDeltaAdapter.java index a9a09fee894e..35b9aadebce4 100644 --- a/jps/jps-builders/src/org/jetbrains/jps/builders/java/BackendCallbackToGraphDeltaAdapter.java +++ b/jps/jps-builders/src/org/jetbrains/jps/builders/java/BackendCallbackToGraphDeltaAdapter.java @@ -4,10 +4,7 @@ package org.jetbrains.jps.builders.java; import com.intellij.openapi.util.Pair; import com.intellij.util.SmartList; import org.jetbrains.jps.builders.java.dependencyView.Callbacks; -import org.jetbrains.jps.dependency.GraphConfiguration; -import org.jetbrains.jps.dependency.Node; -import org.jetbrains.jps.dependency.NodeSource; -import org.jetbrains.jps.dependency.Usage; +import org.jetbrains.jps.dependency.*; import org.jetbrains.jps.dependency.java.*; import org.jetbrains.jps.javac.Iterators; import org.jetbrains.org.objectweb.asm.ClassReader; @@ -58,6 +55,14 @@ final class BackendCallbackToGraphDeltaAdapter implements Callbacks.Backend { public List, Iterable>> getNodes() { try { + NodeSourcePathMapper pathMapper = myGraphConfig.getPathMapper(); + for (NodeSource source : Iterators.flat(Iterators.map(myNodes, p -> p.getSecond()))) { + myPerSourceAdditionalUsages.remove(pathMapper.toPath(source)); + } + for (Map.Entry> entry : myPerSourceAdditionalUsages.entrySet()) { + NodeSource src = pathMapper.toNodeSource(entry.getKey()); + myNodes.add(new Pair<>(new FileNode(src.toString(), entry.getValue()), List.of(src))); + } return myNodes; } finally { diff --git a/jps/jps-builders/src/org/jetbrains/jps/dependency/java/FileNode.java b/jps/jps-builders/src/org/jetbrains/jps/dependency/java/FileNode.java new file mode 100644 index 000000000000..4b8472735779 --- /dev/null +++ b/jps/jps-builders/src/org/jetbrains/jps/dependency/java/FileNode.java @@ -0,0 +1,110 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.jps.dependency.java; + +import com.intellij.util.SmartList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jps.dependency.GraphDataInput; +import org.jetbrains.jps.dependency.GraphDataOutput; +import org.jetbrains.jps.dependency.Node; +import org.jetbrains.jps.dependency.Usage; +import org.jetbrains.jps.dependency.diff.DiffCapable; +import org.jetbrains.jps.dependency.diff.Difference; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + +public final class FileNode implements Node { + private final JvmNodeReferenceID myId; + private final Iterable myUsages; + + public FileNode(String name, @NotNull Iterable usages) { + myId = new JvmNodeReferenceID(name); + myUsages = usages; + } + + public FileNode(GraphDataInput in) throws IOException { + myId = new JvmNodeReferenceID(in); + + List usages = new SmartList<>(); + try { + int groupCount = in.readInt(); + while(groupCount-- > 0) { + in.readGraphElementCollection(usages); + } + } + finally { + myUsages = usages; + } + } + + @Override + public void write(GraphDataOutput out) throws IOException { + myId.write(out); + + Map, List> usageGroups = new HashMap<>(); + for (Usage usage : myUsages) { + usageGroups.computeIfAbsent(usage.getClass(), k -> new SmartList<>()).add(usage); + } + + out.writeInt(usageGroups.size()); + for (Map.Entry, List> entry : usageGroups.entrySet()) { + out.writeGraphElementCollection(entry.getKey(), entry.getValue()); + } + } + + @Override + public @NotNull JvmNodeReferenceID getReferenceID() { + return myId; + } + + public @NotNull String getName() { + return myId.getNodeName(); + } + + @Override + public Iterable getUsages() { + return myUsages; + } + + + @Override + public boolean isSame(DiffCapable other) { + if (!(other instanceof FileNode)) { + return false; + } + + FileNode that = (FileNode)other; + return myId.equals(that.myId); + } + + @Override + public int diffHashCode() { + return myId.hashCode(); + } + + @Override + public Diff difference(FileNode past) { + return new Diff(past); + } + + public class Diff implements Difference { + private final Supplier> myUsagesDiff; + + public Diff(FileNode past) { + myUsagesDiff = Utils.lazyValue(() -> Difference.diff(past.getUsages(), getUsages())); + } + + @Override + public boolean unchanged() { + return usages().unchanged(); + } + + public Specifier usages() { + return myUsagesDiff.get(); + } + } + +} diff --git a/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/classBecameInterface/build.log b/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/classBecameInterface/build.log index b3f2f0afbaf9..8aea615044e7 100644 --- a/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/classBecameInterface/build.log +++ b/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/classBecameInterface/build.log @@ -67,6 +67,7 @@ Cleaning output files: End of files Compiling files: src/classLiteral.kt + src/importA.kt End of files Exit code: OK ------------------------------------------ \ No newline at end of file diff --git a/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/supertypesListChanged/build.log b/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/supertypesListChanged/build.log index 7daea931809c..69e96b62f2fd 100644 --- a/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/supertypesListChanged/build.log +++ b/plugins/kotlin/jps/graphImplementationTests/testData/incremental/classHierarchyAffected/supertypesListChanged/build.log @@ -53,6 +53,8 @@ Compiling files: src/classLiteral.kt src/functionParameter.kt src/getA.kt + src/importA.kt + src/importAGrandChild.kt src/importStar.kt src/referencedByFqName.kt src/returnType.kt