Skip to content

Commit bba7037

Browse files
committed
Documentation for pathnode provider optimization in inherit graph
1 parent 5307a24 commit bba7037

4 files changed

Lines changed: 50 additions & 18 deletions

File tree

recaf-core/src/main/java/software/coley/recaf/services/inheritance/ClassPathNodeProvider.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,30 @@
99
import java.util.Map;
1010
import java.util.stream.Stream;
1111

12+
/**
13+
* Provider of class path nodes.
14+
*
15+
* @author xDark
16+
*/
1217
sealed interface ClassPathNodeProvider {
13-
18+
/**
19+
* @param name
20+
* Class name to look up.
21+
*
22+
* @return Path node for the class with the given name, or {@code null} if no such class exists in the provider.
23+
*/
1424
@Nullable
1525
ClassPathNode getNode(@Nonnull String name);
1626

17-
static ClassPathNodeProvider.Cached cache(Workspace workspace) {
27+
/**
28+
* Create a cached provider that contains all nodes from the workspace at the time of creation.
29+
*
30+
* @param workspace
31+
* Workspace to cache nodes from.
32+
*
33+
* @return Provider that caches all nodes from the workspace at the time of creation.
34+
*/
35+
static ClassPathNodeProvider.Cached cache(@Nonnull Workspace workspace) {
1836
Stream<ClassPathNode> stream = workspace.classesStream();
1937
Map<String, ClassPathNode> nodes = new HashMap<>(4096);
2038
stream.forEach(classPathNode -> {
@@ -23,15 +41,31 @@ static ClassPathNodeProvider.Cached cache(Workspace workspace) {
2341
return new Cached(Map.copyOf(nodes));
2442
}
2543

26-
record FromWorkspace(@Nonnull Workspace workspace) implements ClassPathNodeProvider {
44+
/**
45+
* Provider that looks up nodes directly from the workspace.
46+
* This is not recommended for repeated lookups, but it is useful for one-off lookups or when the workspace is expected to be changing frequently.
47+
*
48+
* @param workspace
49+
* Workspace to look up nodes from.
50+
*/
51+
record Live(@Nonnull Workspace workspace) implements ClassPathNodeProvider {
2752
@Nullable
2853
@Override
2954
public ClassPathNode getNode(@Nonnull String name) {
3055
return workspace.findClass(name);
3156
}
3257
}
3358

34-
record Cached(Map<String, ClassPathNode> nodes) implements ClassPathNodeProvider {
59+
/**
60+
* Provider that caches all nodes from the workspace at the time of creation.
61+
* This is recommended for repeated lookups, but it is not suitable for workspaces that are expected to be changing frequently.
62+
*
63+
* @param nodes
64+
* Map of class names to their corresponding path nodes. This map is expected to be immutable.
65+
*
66+
* @see #cache(Workspace)
67+
*/
68+
record Cached(@Nonnull Map<String, ClassPathNode> nodes) implements ClassPathNodeProvider {
3569
int size() {
3670
return nodes.size();
3771
}

recaf-core/src/main/java/software/coley/recaf/services/inheritance/InheritanceGraph.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class InheritanceGraph {
5959
*/
6060
public InheritanceGraph(@Nonnull Workspace workspace) {
6161
this.workspace = workspace;
62-
this.workspaceNodeProvider = new ClassPathNodeProvider.FromWorkspace(workspace);
62+
this.workspaceNodeProvider = new ClassPathNodeProvider.Live(workspace);
6363

6464
// Populate map lookups with the initial capacity of the number of classes in the workspace plus a buffer.
6565
int classesInWorkspace = workspace.allResourcesStream(false /* dont count internal resource classes */)
@@ -115,9 +115,7 @@ private void refreshChildLookup() {
115115
// Repopulate
116116
ClassPathNodeProvider.Cached nodeProvider = ClassPathNodeProvider.cache(workspace);
117117
Set<ClassInfo> visited = Collections.newSetFromMap(new IdentityHashMap<>(nodeProvider.size() + 1024 /* leeway */));
118-
workspace.forEachClass(false, cls -> {
119-
populateParentToChildLookup(cls, visited, nodeProvider);
120-
});
118+
workspace.forEachClass(false, cls -> populateParentToChildLookup(cls, visited, nodeProvider));
121119
}
122120

123121
/**

recaf-core/src/main/java/software/coley/recaf/workspace/model/resource/AgentServerRemoteVmResource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ public Map<Integer, JvmClassBundle> getJvmClassloaderBundles() {
8686

8787
@Nonnull
8888
@Override
89-
public Stream<JvmClassBundle> jvmClassBundleStream() {
90-
return Stream.concat(super.jvmClassBundleStream(), new ArrayList<>(remoteBundleMap.values()).stream());
89+
public Iterable<JvmClassBundle> jvmClassBundles() {
90+
return Iterables.concat(super.jvmClassBundles(), new ArrayList<>(remoteBundleMap.values()));
9191
}
9292

9393
@Nonnull
9494
@Override
95-
public Iterable<JvmClassBundle> jvmClassBundles() {
96-
return Iterables.concat(super.jvmClassBundles(), new ArrayList<>(remoteBundleMap.values()));
95+
public Stream<JvmClassBundle> jvmClassBundleStream() {
96+
return Stream.concat(super.jvmClassBundleStream(), new ArrayList<>(remoteBundleMap.values()).stream());
9797
}
9898

9999
@Override

recaf-core/src/main/java/software/coley/recaf/workspace/model/resource/WorkspaceResource.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,19 @@ default boolean isEmbeddedResource() {
135135
}
136136

137137
/**
138-
* @return Stream of all immediate JVM class bundles in the resource.
138+
* @return Iterable of all immediate JVM class bundles in the resource.
139139
*/
140140
@Nonnull
141-
default Stream<JvmClassBundle> jvmClassBundleStream() {
142-
return of(getJvmClassBundle());
141+
default Iterable<JvmClassBundle> jvmClassBundles() {
142+
return List.of(getJvmClassBundle());
143143
}
144144

145145
/**
146-
* @return Iterable of all immediate JVM class bundles in the resource.
146+
* @return Stream of all immediate JVM class bundles in the resource.
147147
*/
148148
@Nonnull
149-
default Iterable<JvmClassBundle> jvmClassBundles() {
150-
return List.of(getJvmClassBundle());
149+
default Stream<JvmClassBundle> jvmClassBundleStream() {
150+
return of(getJvmClassBundle());
151151
}
152152

153153
/**

0 commit comments

Comments
 (0)